Project 1: Image Filtering and Hybrid Images

Zoom out.

"Hybrid images are static images that change in interpretation as a function of the viewing distance. The basic idea is that high frequency tends to dominate perception when it is available, but, at a distance, only the low frequency (smooth) part of the signal can be seen. By blending the high frequency portion of one image with the low-frequency portion of another, you get a hybrid image that leads to different interpretations at different distances." - James Hays

It's a bird...It's a plane...

It's a Hybrid Image

To create the hybrid images, I first removed the high frequencies of the first image by blurring it with a gaussian filter. For the second image, in order to remove the low frequencies, I subtracted the values of its blurred version from the original. Finally to combine them, I simply summed the values of the two filtered images, cropping if needed.


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

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

h = min(size(low_frequencies,1), size(high_frequencies, 1));
w = min(size(low_frequencies,2), size(high_frequencies, 2));
hybrid_image = low_frequencies(1:h,1:w,:) + high_frequencies(1:h,1:w,:);


Image Filtering

In order to create the gaussian blurred image, I had to implement an image filter function. To do this, I padded the original image with an extension of the outer pixels, so that the filter would be applied to each pixel in the original image rather than excluding the outer pixels. Then I applied the filter to the neighborhood surrounding each pixel by taking the dot product of the neighborhood and the filter to achieve the value for the filtered pixel.


image = padarray(image, [pad_h, pad_w], 'replicate');

%image dimensions
M = size(image, 1);
N = size(image, 2);
%number of channels
channels = size(image, 3);

output = zeros(M, N, channels);

for c = 1:channels  
    for m = pad_h + 1 : M - pad_h
        for n = pad_w + 1 : N - pad_w                
            %neighborhood dot product
            product = image(m-pad_h : m+pad_h, n-pad_w : n+pad_w, c).* filter;
            %sum all the elements in product matrix
            output(m,n,c) = sum(product(:));            
            
        end
    end
end

Images of a dog with high frequencies removed by applying a gaussian blur and a cat with low frequencies removed by subtracting gaussian blur from original

I tried to create my own with an old picture of my cat when he was a kitten combined with a more recent photo, but it is less effective when the two photographs are not lined up exactly.

A kitten, a catten, and a kitcat

Some other examples with varying degrees of cuttoff frequency for the gaussian filter.