Project 1: Image Filtering and Hybrid Images

Implementation of my_imfilter

First, we use size() to get the size of image and filter. Then we pad the image with zeros. The padding above and below are (image_height-1)/2 in rows. The padding on the left and right are (image_width-1)/2 in columns. Then for every pixel of output image, we do the select the subimage of the same size of filter from the padded image, do dot product with filter and sum up all entries.

Computation looping over all pixels

ch is the number of channels. i_h is the the height of image.i_w is the the width of image.


for k = 1:ch
    for i = 1:i_h
        for j = 1:i_w
            output(i,j,k) = sum(sum(filter .* image_pad(i:i+f_h-1,j:j+f_w-1,k)));
        end
    end
end

Results of the test script

The above table shows the results of identity filter(the same as original image), box filter, Gaussian filter, Sobel filter, Laplacian filter and high-pass fitler by subtracting box filter.

Hybrid Images

A hybrid image of image 1 and 2 is the sum of a low-pass filtered version of image 1 and a high-pass filtered version of image 2. We use Gaussian filter for low-pass filter. For high-pass filtering, we simply subtract the original image with low-pass filtered version of it.

Implementation using separability of Gaussian filter

Since Gaussion filter is seperable. To make the filtering more efficienct, we first filter the image rowwise by 1-D Gaussion filter and filter the intermediate result columnwise by the transpose of the 1-D Gaussian filter.


filter_1D = fspecial('Gaussian', [cutoff_frequency*4+1,1], cutoff_frequency);

% Remove the high frequencies from image1 by blurring it.
low_frequencies = my_imfilter(image1,filter_1D);
low_frequencies = my_imfilter(low_frequencies,filter_1D');

% Remove the low frequencies from image2.
image2_blur = my_imfilter(image2,filter_1D);
image2_blur = my_imfilter(image2_blur,filter_1D');
high_frequencies = image2 - image2_blur;

% Combine the high frequencies and low frequencies
hybrid_image = low_frequencies + high_frequencies;

Results

The first row is a hybrid image of cat and dog with cutoff_frequency = 7. The second row is a hybrid image of Einstein and Marilyn with cutoff_frequency = 4. The third row is a hybrid image of bike and motorcycle with cutoff_frequency = 6. The fourth row is a hybrid image of fish and submarine with cutoff_frequency = 5.The fifth row is a hybrid image of bird and plane with cutoff_frequency = 5.

From the result, we can see that the color information is contained by the low-pass version of image. In order for the hybrid image to look "nature", the two component image must have similar shapes.

Original Images

The original images that are component of the hybrid images are listed below in order.