Project 1: Image Filtering and Hybrid Images

Example of a hybrid image.

The description of the implementation of 'my_imfilter' function is as follows. Given a filter and an input image, we need to pad the input image in order to run the filter everywhere on the input image including its borders. Assuming the filter has only odd dimensions, the way the image is padded is as follows:

  1. Pad both top and bottom of input image by floor(filter_height/2).
  2. Pad both left side and right side of input image by floor(filter_width/2).
  3. Use padarray() function with padval parameter equal to 'symmetric' to do the padding. This will pad the image with mirror reflections of itself.

Once the padded image is ready, three nested for loops are used in the following manner.

  1. The outer most loop iterates across the channels of the image. So if the image is grayscale it will iterate only once, but if it is an rgb image, this loop will run three times as the image has 3 channels (r,g and b).
  2. The next loop makes the filter run on each row of the image.
  3. The inner most loop makes the filter run on each column of the image.

For the actual filtering process, a sub matrix of the image is taken around the input pixel being processed. This matrix of pixels is of the same size as the kernel. Then, element wise matrix multiplication is done between this matrix and the kernel. The output matrix obtained is then summed to give a single value which is the output pixel value. Note that the filter does not compute any output for the pixels used for padding in the padded image. These pixels are used to calculate filter output for border pixels of the original image. The output image will be of the same size as the original input image without padding.

Generation of hybrid images

For generation of a hybrid image, we keep only the high frequencies of the first image and keep only the low frequencies of the second image. These two frequency filtered images are combined to generate the hybrid image. Gaussian blur is used to do the frequency filtering of the images. Gaussian blur keeps the low frequency components of an image and removes the high frequency components. The image which needs to be low pass filtered is created by running gaussian blur on it. The image which needs to be high pass filtered is created by subtracting the low pass filtered version of the image from the original. The cutoff frequencies for creating the low pass and high pass filtered images are kept independent, which helps in getting a more appealing hybrid image.

 

Sample outputs of 'my_imfilter' in the following order: Original image, Identity filter image, Blur image, Large blur image, Sobel image, Laplacian image, High pass filtered image

 

Intermediate and final output of hybrid image generation in the following order: High pass filtered image, Low pass filtered image, Hybrid image

 

Examples of hybrid images created