CS 143 / Project 1 / Image Filtering and Hybrid Images

Implementation

My filtering algorithm took in an image and a filter and applied the filter to each color layer of the image and then recombined them to construct the final image. First the image was padded symmetrically by half of the filter width minus one ((w-1)/2) in the x direction and half of the filter height minus one ((h-1)/2) in the y direction (see figure 2). The image was then separated into its RGB component matrices.

Each matrix was converted using im2col which rearranges image blocks into columns using sliding and the filter dimensions. This produces a matrix where each column i contains the pixels used to calculate the resulting image at index i in the image array (flattened matrix). To perform the filtering, the filter was reshaped into a row vector. This vector multiplied by im2col matrix performs the calculation for each pixel and results in the convolution of the filter with the image. Once the filtered RGB matrices are computed, they are recombined to form the layered image.

PsudoCode


my_imfilter(image, filter):
	padded = pad image with floor half of the filter dimensions

	//Extract the layers and create sliding representation
	R = im2col(red layer of padded, filter dimensions, 'sliding');
	G = im2col(green layer of padded, filter dimensions, 'sliding');
	B = im2col(blue layer of padded, filter dimensions, 'sliding');

	flatFilter = filter as row vector

	//convolve layer with filter and reshape resulting array
	R' = reshape flatFilter*R to original image dimensions
	G' = reshape flatFilter*G to original image dimensions
	B' = reshape flatFilter*B to original image dimensions

	return [R,G,B]

Filtering

Below are the results of applying various filters to the an image using my implementation of filter.

Top row from left to right

  1. Original image with no filtering.
  2. Image with 30 pixels of symmetric padding in both directions. These padded pixels are used for calculating the neighborhood around edge pixels of the original image.
  3. Image filtered by the identity filter. This filter has a 1 at the center pixels and is zeroes everywhere else so it reproduces the original image. Identity, get it?
  4. Image filtered using the Sobel Operator which responds to horizontal gradients.

Bottom row from left to right

  1. Image blurred with a small box filter. This blurring removes some of the high frequencies.
  2. Image blurred with a large gaussian filter. Due to the separable nature of Gaussian blurs, this blur is achieved by blurring horizontally and then blurring that blurred image vertically.
  3. Image filtered using a discrete laplacian to achieve a high pass filter. This removes the low frequencies and keeps the high frequencies.
  4. High pass achieved by blurring (low pass) and subtracting the result from the original.

Image Results of Filtering

From left to right: (Top) original image, padded image, identity filter, sobel filter. (Bottom) small box blur, gaussian blur, laplacian high pass, high pass by subtracting low frequencies.

Hybrid Images

Hybrid images are obtained by extracting the low frequencies of one image and the high frequencies of another image and combining the two. In this case the low frequencies were obtained using a gaussian blur. The high frequency image was obtained by subtracting the low frequencies, achieved with gaussian blur, from the original image. Then the two images were combined.

Below are the original, intermediate, and resulting images for creating a hybrid image.

Top row from left to right

  1. Original Marilyn image.
  2. Low pass Marilyn image.
  3. Original Einstein image.
  4. High pass Einstein image.

Bottom row from left to right

  1. Hybrid image. Addition of low pass Marilyn to high pass Einstein image.
  2. Hybrid image at various scales. At smaller scale, the low pass image is more apparent. At a larger size the high pass image is more apparent.