Project 1: Image Filtering and Hybrid Images

Bird/jet hybrid.

A hybrid image is an image that appears to be one thing up close, but from far away (or zoomed out in your web browser) appears to be something else. For this class project, I created hybrid images by combining two images. One of the images was filtered so that only the low frequencies were left, and the other image was filtered so that only high frequencies remained. The images were then added together to create the hybrid image. I also wrote the function (my_imfilter) that did the filtering using convolution.

my_imfilter Details

Everything was implemented in Matlab. My_imfilter first rotates the input filter 180 degrees. Next, my_imfilter pads the input image with black bars based on the size of the rotated input filter. This is the code for doing the padding:

[num_filter_rows, num_filter_cols] = size(filter_rotated);
row_padding = floor(num_filter_rows / 2);
col_padding = floor(num_filter_cols / 2);

The input image is padded because the filter needs to be applied along the edge pixels of the image.

Next, the rotated input filter is applied to every pixel. Each color channel is done separately. The filter application is done by centering the filter on a given pixel and then doing elementwise multiplication and summation for every pixel under the filter. The sum is used as the new value of the current pixel. This is done for every pixel that isn't part of the padding and every color channel. The code for that looks like this:


%Apply the rotated filter
[num_im_rows, num_im_cols, num_im_channels] = size(im_padded);
im_padded_new = zeros(size(im_padded));

for color_channel = 1:num_im_channels
    for row = 1:(num_im_rows - num_filter_rows + 1)
        for col = 1:(num_im_cols - num_filter_cols + 1)
            im_subsection = im_padded(row:(row + num_filter_rows - 1), col:(col + num_filter_cols - 1), color_channel);
            im_subsection = im_subsection .* filter_rotated;
            subsection_sum = sum(im_subsection(:));
            im_padded_new(row + row_padding, col + col_padding, color_channel) = subsection_sum;
        end
    end
end

In the above code sample, array indexing is used to quickly slice out the relevant section of the 3d array representing the image, apply the filter, and place the pixel value given by the summation of the elementwise multiplication in a new 3d array.

Hybrid Image Creation Details

For the first image, the low frequencies are isolated by applying a guassian blur filter to the image, which removes the high frequencies of the image. For the second image, the low frequencies are removed by subtracting the blurred image from the original. The two images are then combined. The code for those three steps is the following three lines of matlab.


low_frequencies = my_imfilter(image1, filter);
high_frequencies = image2 - my_imfilter(image2, filter);
hybrid_image = low_frequencies + high_frequencies;

Given these two input images:

The high frequency image looks like this:

And the low frequency image looks like this:

Combined they look like this:

I had to tweak the standard deviation of the guassian blur per image pair to get good hybrid image results. This is because the distribution of high and low frequencies varies by image.

Hybrid Image Results

Here are some examples of the hybrid images I created. All of the low frequency images were blurred with a guassian of std. deviation 7 except for the einstein/marilyn one where I used a std. deviation of 4.