Project 1: Image Filtering and Hybrid Images

Example of a hybrid image (Dog and Cat).

The purpose of this project is to write an image filtering function and use it to create hybrid images. The basic idea of hybrid images is that high frequency tends to dominate perception when it is available, but, when distance gets longer, only the low frequency part of an image is visible. A built-in function in Matlab, 'imfilter' function, works perfectly to hybrid images. However, the goal of the project is to design own filter algorithms to get similar results of the Matlab’s function. An example of a hybrid image is shown on the right. From a relatively near viewing angle, the image shows a cat. As moving further away from this image, the image “Cat?fades away and a low-pass filtered image “Dog?can be seen.

Code and Algorithms

Hybrid Images

The code shown below creates the low frequency and high frequency filtered images and then combine them to create hybrid image. First of all, to get the low frequency image, the filter is applied to remove the high frequencies from image 1 by blurring it. Then, the high frequency image is obtained by removing the low frequencies from image 2. The process is simply subtracting a blurred version of image 2 from the original version if image 2. Finally, the hybrid image is a sum of a low-pass filtered image and a high-pass filtered image.



low_frequencies = my_imfilter(image1,filter);

high_frequencies = image2 - my_imfilter(image2, filter);

hybrid_image = low_frequencies + high_frequencies;

The code shown below is the parameter can be tuned to get the best results for every image pair. This parameter, cutoff frequency, is the standard deviation of the Gaussian blur that can remove the high frequencies from image.



cutoff_frequency = 8;

filter = fspecial('Gaussian', cutoff_frequency*4+1, cutoff_frequency);

my_imfilter() Function

For the image filtering part, a function, my_imfilter(), is required to implement which imitates the default behavior of the build in function imfilter(). The detailed requirements are listed as follows:

  1. Support grayscale and color images
  2. Support arbitrary shaped filters, as long as both dimensions are odd
  3. Pad the input image with zeros or reflected image content
  4. Return a filtered image which is the same resolution as the input image

The function my_imfilter() is shown below:



function output = my_imfilter(image, filter)

[image_row,image_column,image_channel]=size(image);% read the image size
[filter_row,filter_column]=size(filter);           % read the filter size

zero_padding=padarray(image,[floor(filter_row/2),floor(filter_column/2)]);                   % zero-padding
% symmetric_padding=padarray(image,[floor(filter_row/2),floor(filter_column/2)],'symmetric');% symmetric padding

for z=1:image_channel
    for x=1:image_row
        for y=1:image_column
                output(x,y,z)= sum(dot(zero_padding(x:x+filter_row-1,y:y-1+filter_column,z),filter));       % dot product
                %output(x,y,z)= sum(dot(symmetric_padding(x:x+filter_row-1,y:y-1+filter_column,z),filter));
        end
    end
end
end

Image Boundary Problem

In the image padding part, as suggested in the homework description, padarrray() function is implemented in my_imfilter() function.



zero_padding=padarray(image,[floor(filter_row/2),floor(filter_column/2)]);                   % zero-padding
% symmetric_padding=padarray(image,[floor(filter_row/2),floor(filter_column/2)],'symmetric');% symmetric padding

Two methods are implemented here to pad the image. In the first method of handling boundaries, the original image is padded with zeros. In the second method of handling boundaries, the original image is padded symmetrically. The images below show an example of how these two methods work.


Original Image

Zero Padding

Pad Symmetrically

As shown above, these two methods have different advantages and disadvantages. If the target image is just a small window on the world, and the values outside the boundary are supposed to be just like the values around the original image, then the symmetrical padding method probably makes more sense. However, when the target image has very high resolution, and the calculating time is limited, the zero padding method may be more useful.

Images Filtering

The code below illustrates the process of doing dot product. A Matlab’s build in function dot is used to implement this operation.



for z=1:image_channel
    for x=1:image_row
        for y=1:image_column
                output(x,y,z)= sum(dot(zero_padding(x:x+filter_row-1,y:y-1+filter_column,z),filter));       % dot product
                %output(x,y,z)= sum(dot(symmetric_padding(x:x+filter_row-1,y:y-1+filter_column,z),filter));
        end
    end
end

Results and Discussion

Test my_imfilter() Results


Identiy Image

Small Blur Image

Large Blur Image

Sobel Image

High Pass Image Laplacian Image

High Pass Image

Dog and Cat (cf = 5)


Dog Image (low frequencies)

Cat Image (high frequencies)

Hybrid Image

Marilyn and Einstein (cf=3.5)


Marilyn Image (low frequencies)

Einstein Image (high frequencies)

Hybrid Image

Bird and Plane (cf=3)


Bird Image (low frequencies)

Plane Image (high frequencies)

Hybrid Image

Fish and Submarine (cf=6)


Fish Image (low frequencies)

Submarine Image (high frequencies)

Hybrid Image

Motorcycle and Bicycle (cf=4.5)


Motorcycle Image (low frequencies)

Bicycle Image (high frequencies)

Hybrid Image

Some Extra Works

Garen and Darius (cf=8)


Garen Image (low frequencies)

Darius Image (high frequencies)

Hybrid Image

In this part, Garen and Darius, who are the two characters in League of Legends, are found on the Internet. The algorithm and image filtering process are implemented on these two images. Proper image reducing method are also implemented here to increase the processing speed. See README.TXT for more details.

Discussions

In this project, an image filtering function used to create hybrid images is developed. A parameter, cut-off frequency, has been tuned for each pair of images shown above. Every pair of images has a unique value of cut-off frequency to make sure the best results are produced. As shown above, the my_imfilter() function works perfectly for all kind of images from the images given by instructor and the images I found on internet. During the process, the symmetrical padding method is used instead of zero padding method to increase the precision and accuracy of the filtering process, though zero padding method results in fast response time.