Project 1: Image Filtering and Hybrid Images

A hybrid image of a dog and cat.
From a close distance, it will resemble a cat and from a large distance, it will resemble a dog.

In this project, I created an image filtering function in matlab and use it to create hybrid images. It follows the 2006 paper by Oliviaq, Toprralba and Schyns. Filtering(convolution) can be used to create a high-pass filtered version of an image and a low pass filtered of an image, which when merged in a specific way, can be used to create hybrid images that can be perceived differently depending on the viewing distance.

This project can hence be divided into two parts:

  1. Image Filtering
  2. Hybrid Images

Image Filtering

Filtering is a method where a "filter matrix is applied on to an image in such a way that the original image matrix. The resultant matrix will be a modification of the original image.

Applying the filter

To apply the filter, we iterate through each of the pixel's values in the matrix (all three channels and all rows and all columns) and calculate the resultant value by considering it and the surrounding pixel's values and weighing them using the filter matrix.

Below is a code snippet that illustrates how this is done in matlab. "output" is the resultant image matrix.


%filtering code
output = zeros(size(image));
dimensions = (size(image));
filterSize = size(filter);
filterUpDown = floor(filterSize(1)/2);
filterLeftRight  =floor(filterSize(2)/2);
image2 = padarray(image,[filterUpDown filterLeftRight 0],0);
for layer = 1:dimensions(3)
    for i = 1+filterUpDown:dimensions(1)+filterUpDown
        for j=1+filterLeftRight:dimensions(2)+filterLeftRight
            value = image2(i-filterUpDown:i+filterUpDown,j-filterLeftRight:j+filterLeftRight,layer).*filter;
            output(i-filterUpDown,j-filterLeftRight,layer) = sum(value(:));
        end
    end
end

Hybrid Images

Using filters, we can create a high-pass version of an image and a low-pass version of an image. The low-pass version of the image will be more visible from larger distances as it lets frequencies lower than a defined cutoff to pass through. The high-pass version of the image will be more visible from up-close as it lets frequencies greater than a defined cutoff to pass through. We can then combine the two images by just adding up the pixel value at each location. For hybrid images to work, they need to be of the same size and have to be aligned well to crete the hybrid effect. The cut-off frequency also needs to be tuned for each image pair (examples below).

Creating the hybrid image

We first create a filter (gaussian in this case) with the predifined cutoff frequency that acts as the distribution's standard deviation and also contributes to the filter's size. Then, we use it to create the low-pass version of the image and the high-pass version of the image (by subtracting the low-pass version from itself). We then simply add up the two images to create the final hybrid image.

The code for the same is provided below.


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

low_frequencies = my_imfilter(image1,filter);

intermediate = my_imfilter(image2,filter);
high_frequencies = image2 - intermediate;

hybrid_image = low_frequencies + high_frequencies;

Hybrid Images

Below are some examples of hybrid images. The cutoff frequencies had to be varied to achieve the ideal results. The same cut-off frequency was used to create the high-pass filter and low-pass filter although two different filters could be created to fine-tune the hybrid images even further. In my tests, the results were minimally better and so one single cut-off frequency was used for every pair of images below.


Cut-off frequency = 3. Near image = Bicycle. Distance Image = Motorbike

Cut-off frequency = 3. Near image = Plane. Distance Image = Bird

Cut-off frequency = 5. Near image = Cat. Distance Image = Dog

Cut-off frequency = 3. Near image = Einstein. Distance Image = Marilyn

Cut-off frequency = 5. Near image = fish. Distance Image = Submarine

Cut-off frequency = 3. Near image = Smiling . Distance Image = Sad

Cut-off frequency = 6. Near image = Laughing . Distance Image = Angry