Project 1: Image Filtering and Hybrid Images

Look at the above image from a very close
distance and then from far away

I have implemented the algorithm for image filtering and further creating a hybrid image using two filtered images. Some of the key points covered in the implementation are:

  1. The code supports both grayscale and color images
  2. It supports arbitrary shaped filters(with both dimensions odd)
  3. It pads the input image with reflected image content
  4. Returns a filtered image with the same resolution as the input image

The image on the right is a sample output of the code. It is a hybrid image of Albert Einstein and Marilyn Monroe. The image of Albert Einstein is passed through a high pass filter and the image of Marilyn Monroe is passed throgh a low pass filter. Hence when you look at the image from a close distance you see Albert Einstein but if you look at it from far away you see Marilyn Monroe.

Image Filtering

The basic idea behind the algorithm is to get the dimensions of the input image in a 3-element vector. Color images have 3 dimensions where as gray-scale images have 2 dimensions. Since matlab returns 1 for the dimensions if the vector size has more dimensions than the array passed to the size function, the third element has value 3 for color images and value 1 for Gray-Scale images.

The code then loops from 1 to third dimension. I use the padarray() function provided by matlab to pad the sub array for a particular dimension with the reflected image content. The padding is done with dimensions equal to floor(half the filter size). So for example for a 3 x 3 filter, the padding is done with [1 1].

Then using every cell of the output image as the center, a window is formed around this cell with the size of the filter and a value is obtained by adding the product of the filter and the corresponding cells in the padded input sub array.

The ouput image has the same dimensions as the input image (before padding) and the above steps are implemented n number of times (where n is the value of the third dimension of the imput image).

Image Filtering Code

Following code implements the image filtering algorithm described above:


%dim3 is the third dimension of input array
%h2 is half of the filter height (half the number of rows in filter array)
%w2 is half of the filter width (half the number of columns in filter array)
	
for i = 1:1:dim3
        sub_image = image(:,:,i);
        padded_array = padarray(sub_image,[h2 w2],'symmetric');
        [d1,d2] = size(padded_array);
        for j = 1+h2:1:d1-h2
            for k = 1+w2:1:d2-w2
                output_image(j - h2,k - w2,i) = sum(dot(padded_array((j-h2):(j+h2),...
                                                             (k-w2):(k+w2)), filter));                                                
            end
        end        
    end

Different filters on an image

From top left in clockwise direction :
1) Original Image 2) Small Blur 3) Large Blur 4) High Pass Image 5) Discrete Laplacian Image 6) Sobel Filtered Image

Hybrid Images

For obtaining hybrid images, I first get a Gaussian Blur by using two different cutoff frequencies. Then the low frequency image 1 is obtained by passing it to the above filtering function using the gaussian filter.

For image 2, the low frequency image obtained by passing image 2 to the above filtering function is subtracted from the original image, resulting in a high frequency image 2.

Later the low frequency image 1 is combined with high frequency image 2 to obtain a hybrid image.

Hybrid Images Code

Following code implements the algorithm for generating hybrid images descriped above:


filter = fspecial('Gaussian', cutoff_frequency_1, cutoff_frequency_2);

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

hybrid_image = low_frequencies + high_frequencies;

Results in a table

Following are the outputs of some other images used to create hybrid images

From top left in clockwise direction:
1) Original image 1 2) Low frequency image 1 3) Original image 2 4) High frequency image 2 5) Hybrid images in different scales

A dog and a cat

A bird and a plane

Marilyn Monroe and Albert Einstein

A motorcycle and a bicycle

A fish and a submarine