Project 1: Image Filtering and Hybrid Images

Example of a sobel filtered image.

The method my_imfilter is intended to filter a given input image with any given odd height X odd width filter. The filtering algorithm uses 3 steps:

  1. Determining whether the image is greyscale or color
  2. Padding the input image according to filter dimensions so that output image is the same size as the input
  3. Calculating intensity values for each pixel of the output image

NOTE: Since a filter having even height/width is asymmetric and creates ambiguity regarding which element to choose as the center, the algorithm calculates the output only for odd height and odd width filters. Otherwise the output is an inout sized image with all pixel values set to zero.

Algorithm Description

The padsize for the input image should be such that each pixel of the input image aligns with the center pixel of the filter. Thus the padsize is set to (filter_height-1)/2 above and below the input image and (filter_weight-1)/2 on the left and right side. The parameter imax is assigned the value of the third dimension of the image; i.e. 1 for greyscale and 3 for color images. To calculate the output pixel values, a triple for loop runs on the padded image starting from the left top corner. The 2 inner loops multiply the corresponding positions of the input image and filter, sum the filter_height times filter_width number of pixel intensities and assign the result to the centre pixel of the current block of the output image. The outermost foor loop takes care of color and greyscale images. It runs thrice for each color component and only once if the given image is greyscale.

Example of code with highlighting


%example code
temp = padded_image(xcurr : xcurr+xfilter-1, ycurr : ycurr+yfilter-1, i); %current cross section of image to filter
                temp2 = temp.*filter; 
                output(xcurr, ycurr, i) = sum(temp2(:)); 

Results in a table

The first row of images show the original image and the effect of filtering using a 3X3 box filter, a large Gaussian filter, a sobel filter and laplacian filter respectively. As apparent from the images, the box and Gaussian filter creates a blurring effect, the sobel filter responds strongly to vertical edges and laplacian is a high pass filter. The rows second through eight show the ingredients for a hybrid image, the high frequency filtered and low frequency filtered image of the inputs respectively along with the hybrid image produced. Row 2 shows the effect of hybrid image creation for Emma Watson & Dr. Richard Dawkins and Row 3 shows Professor Mcgonagall's hybrid image with her animagus i.e. a cat.

Computer Man

Cat -> Mcgonagall

Cut-off frequency

The cut-off frequency is an important albeit subjective parameter. If it is too high, only the high frequency image is discernable and vice versa. For example, with a cutoff frequency of 25, the hybrid of Prof. Mcgonagall looks mostly like a cat from almost any distance whereas a cutoff frequency of 1 rids the hybrid of most of the cat's features. A cutoff frequency of 7 gives a hybrid with features from both of the source images.

Effect of decreasing cut-off frequency: a. cut-off frequency = 25 b. cut-off frequency = 7 c. cut-off frequency = 1

NOTE: The original alignment of some of the input image pairs did not coincide. Thus, they had to be manually re-aligned and re-sized to match the position and orientation of the features. The hybrid image is most effective when facial features of the original images coincide perfectly.