Project 1: Image Filtering and Hybrid Images

Example of hybrid image of a cat and a dog

For this assignment I created hybrid images. A hybrid image contains two images. The image that is seen at a certain time depends on how far the viewer is from the photo and how large the photo is to the viewer. This phenomenon happens because one of the images contains only low frequencies while the other image contains only high frequencies. The main part of this assignment was implementing a function filter and image given a filter. This involved making the function:

  • Work for both black and white images as well as color images
  • Work for filters of any odd width and height
  • Pad the boundary of the image in an aesthetically nice way


  • The algorithm of my_imfilter

    The important part of the my_imfilter function is below.
    
    s = size(image);
    f = size(filter);
    filtered_im = zeros(s);
    
    m_space = (f(1)-1)/2;
    n_space = (f(2)-1)/2;
    
    for m = (1+m_space):s(1)-m_space
        for n = (1+n_space):s(2)-n_space
            neighborhood = image(m-m_space:m+m_space,n-n_space:n+n_space) .* filter;
            filtered_im(m,n) = sum(neighborhood(:));
        end
    end
    filtered = filtered_im;
    
    

    For making the images work for both black and white and color images I simply made the function do the filter on each channel individually and then add the channels at the end. To make the filters work for any height and width I simply took the size of the filter into account when selecting the neighborhoods to apply the filter to. In other words, I made it the parameters of when to start and stop the for loops in the filter function.

    To pad the image where the filter was not applied I mirrored the pixels across the padding boundary. I found the mirroring technique of padding to be the most aesthetically pleasing way of padding the image. I did the padding on the two filtered images before they were combined into a hybrid image.

    Results

    The filters I used to create the hybrid images were Gaussian filters. For the low frequency image, I used a Gaussian filter created with the fspecial function. For the high frequency image, I took the difference between the image and the image filtered with a Gaussian filter. The creation of the Gaussian filter was passed a cut off frequency parameter which determined the strength of the Gaussian filter and how much of the high frequencies were removed during filtering.

    I was given a set of 5 pairs of images that aligned well together and I made a hybrid image for each of these pairs of images. The images are shown in the results table. The first two rows of results show the low frequency photo, the high frequency photo and the hybrid of the two photos. The next 5 rows are the hybrid images at different sizes.

    For each pair of images I had to slightly tweak both the low frequency cut off and the high frequency cutoff in order to create the best looking hybrid image. Besides changing these parameters I did not change anything in the code between pairs of photos.