Project 1: Image Filtering and Hybrid Images

Example of a hybrid image result.

Introduction

  1. Basic algorithm description
  2. images with different filters
  3. hydrid image
  4. more examples

In this project, there are several filters applied on the example image. Those filters include identify filter, blur filter, large blur filter, sobel filter, laplacian filter and high pass filter. By using these filters, we can combine two different images into a hybrid one.

Basic algorithm description

The basic algorithm for filters using in the project is convolution. The algorithm takes in image as input and calculate the output image by getting the product of each pixel in the original image with the filter by the equation:

Here the output image matrix is indicated by h, and the input image is given by f. The filter is represented by g.

Example codes

This portion of code is for implementing the formula to getting the product from matrix multiplication. When dealing with padding edges, the value of those out of bound pixels near the sides would be just zero.


[height, width, layer] = size(image);
[fheight, fwidth] = size(filter);
for color = 1:layer
    for i = 1:height
        for j = 1:width
            current = 0;
            ycnt = 1;
            for y = -floor(fwidth/2):1:floor(fwidth/2)
                xcnt = 1;
                for x = -floor(fheight/2):1:floor(fheight/2)
                    if i + x > 0 && i + x <= height && j + y > 0 && j + y <= width
                        current = current + image(i + x, j + y, color) * filter(xcnt, ycnt);
                        xcnt = xcnt + 1;
                    end
                end
                ycnt = ycnt + 1;
            end
            output(i, j, color) = current;
        end
    end
end

Images with different filters

identity image blur image large blur image
sobel image high pass image laplacian image

Hybrid image

The hybrid images are made by adding low-frequencies image with high-frequencies image. The filter used to produce those two images are high-pass filter and low-pass filter correspondingly. There is a need to adjust the cutoff frequency for different pairs of images. The low frequency image can be produced by removing frequencies higher than the threshold, while the high frequency image can be obtained by subtracting its low-frequency part from the original image. After getting the two images, we can combine them together by simply addition to get a hybrid image. The image with low frequency can be recognized in large distance, while the image with high frequency can be clearly seen in short distance.

Example codes

The following codes are simply reflecting the algorithm.

cutoff_frequency = 5;
filter = fspecial('Gaussian', cutoff_frequency*4+1, cutoff_frequency);
low_frequencies = my_imfilter(image1, filter);
high_frequencies = image2 - my_imfilter(image2, filter);
hybrid_image = low_frequencies + high_frequencies;

Image processing

original image1 original image2
high frequency image1 low frequency image2

Hybrid result

Hybrid image
Visual effects of distance

More examples