CS 143 / Project 1 / Image Filtering and Hybrid Images

Image of a bicycle filtered through a laplacian filter.

The purpose of the project was to implement a simplified version of the imfilter() function in Matlab, called my_imfilter(), to generate hybrid images. The filter was supposed to conform to the following specification:

  1. Must support both grayscale and color images.
  2. Need only support arbitrary shaped filters with odd dimensions.
  3. Pad the input image with zeros or reflected content.
  4. Must return an image of the same resolution as the input image.

To go about making such a filter, linear image filtering was studied. Basically, an image filter makes a new image in which each pixel of the new image is equal to the sum of all elements of a matrix; this matrix is the result of multiplying 2 matrices: the filter and a patch of the original image that is centered on by the filter. It is worth mentioning that the multiplication is not a matrix multiplication, but rather an element-by-element multiplication.

Boundary Cases

Making that filter is all well and good, but what happens at the edges? You can't center a 11X11 filter on the boundary of an image! Therefore, one solution is to pad the edges with 0s, but that wouldn't be ideal because the edges would appear black and we would lose content. Therefore, instead, we use only part of the filter at the edges, conforming to how much of the filter we can center at the particular boundary.

Hybrid Images

We can construct hybrid images using the sum of 2 aligned images, one of which has been passed through a high pass filter and the other through a low pass filter. The aligned part is important because it affects perceptual grouping. The code below shows how to create a hybrid image. The filter used is a Gaussian filter with a particular cutoff frequency.


%Low pass one image
low_frequencies = my_imfilter(image1, filter);

%High pass the other
high_frequencies = image2 - my_imfilter(image2, filter);

%Now combine them
hybrid_image = low_frequencies + high_frequencies;

Some Hybrid Image Examples

Above, the first image in each row is the high passed version of a photo. The second image in each row is the low passed version of another photo. The third larger photo is the hybrid image, and the subsequent images are scaled down versions of the hybrid image to see the effect of the hybridization. As we can see clearly, at small distances, the high frequencies dominate, however, at large distances, low frequencies dominate.