Project 1: Image Filtering and Hybrid Images

A hybrid image of a cat and a dog.

This project contains two parts as the name suggests, image filtering and constructing hybrid images. Image filtering is normally done using the im_filter() function in matlab. However, for this project, I created my own my_imfilter() method which takes in an image and a filter and applies the filter to the image. The second part involves applying a gaussian filter on the two images to retrieve low frequencies for one image and high frequences from another to construct a hybrid image.

Tasks:
  1. Implement my_imfilter(image, filter)
  2. Construct hybrid images

my_imfilter()

The goal of the my_imfilter function is to mimick the behavior of the built in imfilter method which applies a filter to an image. In order to mimick this behavior, the filter must be applied to all three color channel (rgb) matrices. In order to handle edge cases, I used the padarray function to pad the edges of the image with a reflection of the image itself. This works better than simply padding with zeroes because the colors of the image get preserved and you don't see a faded black edge around the image when the filter is applied.


% filtering loop
for lay=1:3
    layer = newImage(:,:,lay);
    newRows = [];
    for row=1+rowOffset:iRows-rowOffset
        newCols = [];
        for col=1+colOffset:iColumns-colOffset
            newCols = [newCols, sum(dot(layer(row-rowOffset:row+rowOffset, col-colOffset:col+colOffset), filter))];
        end
        newRows = [newRows; newCols];
    end
    output = cat(3,output,newRows);
end

This is the loop that applies the filter to the image. The outermost for loop iterates through all three color channels of the image. The 2nd for loop iterates through the rows of the image and the innermost for loop iterates through the columns of the image. The dot method is use dot compute the dot product and the sum method is used to sum up all the dot product results.

This is the loop that applies the filter to the image. The outermost for loop iterates through all three color channels of the image. The 2nd for loop iterates through the rows of the image and the innermost for loop iterates through the columns of the image. The dot method is use dot compute the dot product and the sum method is used to sum up all the dot product results. This algorithm runs rather naively with basically 4 loops (the summation of dot products counts as the 4th loop).

Hybrid Images

In order to construct a hybrid image, the cutoff frequency for each pair of images had to be changed. I started off by taking the submarine/fish image pair and applied all cuttoff frequencies from 1 to 10. I began to develop this intuition that the image that should keep low frequncies should be the image which is easier to recognize when blurry. Generally, the blurred images ended being the ones that had some sort of unique shape. The submarine with both low and high frequencies preserved are shown below for cutoff frequencies [2, 4, 6, 8, 10]

Frequencies

Results

Fish/Sub

Original Images

Cutoff frequency: 5

For this image, the image to extract high frequncies from is the fish. The reason I chose this as the high freqnucy image is because it's harder to recognize a fish when it's blurred than a submarine.

Cat/Dog

Original Images

Cutoff frequency: 7

Einstein/Marilyn

Original Images

Cutoff frequency: 4

For this image, it doesn't really matter whether you choose einstein or marilyn as the image to extract high frequencies from. This particular one in this page has einstein as the high frequency image which is why you can see the details of einstein's hair when you're close to the picture.

Motorcycle/Bicycle

Original Images

Cutoff frequency: 8

In this image, the bicycle is more recognizable when blurred than the motorcycle is because of the complexity of the engine in the motorcycle. Preserving details of the engine helps distinguish between the motorcycle and bicycle at a close distance.

Plane/Bird

Original Images

Cutoff frequency: 5

For this image, preserving the high frequencies from the plane ended up looking slightly more convincing than preserving high frequencies from the bird.