CS 143 / Project 1 / Image Filtering and Hybrid Images

Example of a hybrid image.

Objectives

The first objective of this assignment was to create an algorithm that applies a box filter to an image. The algorithm should work on both black-and-white and color images.

The second objective was to combine two different images into one hybrid image. The resulting image should look like the first image when viewed up close, and the second image when viewed from a distance.

Implementation of my_imfilter

In order to handle color images, I loop through the color channels (the third dimension of the image array) and apply the filter to each channel separately.

The filter is applied to a "window" around each pixel to determine the value at that pixel, so I used Matlab's im2col function to divide the image into these windows, storing each window in a column of a new matrix. It's then easy to take the dot product of each of these windows with the filter, storing the results is the output matrix.

There isn't such a window around the pixels on the edges of the image, so I padded the image with zeros. The padding on the top and bottom is equal to the number of pixels above (and below) the center pixel of the filter; the same function is used for the padding on the sides. It is possible to know these values because the assignment specified filters with odd dimensions, meaning that there is a unique center point.

My implementation of this algorithm is below.


for i=1:size(image, 3)
  imageBlocks = im2col(padarray(image(:,:, i), padding), size(filter));
  output(:,:,i) = reshape(dot(imageBlocks,repmat(filter(:),1,size(imageBlocks,2))),size(image,1),size(image,2)); 
				

Results of my_imfilter() using various filters

Hybrid Images

To create the hybrid images, I first create high- and low-pass filtered versions of the image. The low-pass image is obtained by filtering the image with a Gaussian filter, and the high-pass by subtracting the low-pass version from the original image. The hybrid image is the sum of the low-pass version of one image and the high-pass version of the other. Some examples are below.

Original Filtered Hybrid