Project 1: Image Filtering and Hybrid Images

Project 1 entails two tasks, one which requires implementing our own version of Matlab's imfilter and hybridization of images.

my_imfilter algorithm

For the sake of optimization, I used as many matlab functions as I could as opposed to implementing my own versions of its functions

  1. First I obtained the size of the filter and divided it by 2 and labelled as the padding size for the image
  2. Created a filter vector by reshaping the filter matrix into a 1 x numElements(filter) vector so that I can use regular multiplication as opposed to a piecewise operation which would require me to sum the products
  3. Since the image is a 3 dimensional matrix (3rd dim is for color), I iterate over each color channel and pad the extracted image by the padSize using the padarray function for the edges. The padarray function uses the symmetric flag to indicate that the pixels across the edge will be mirrored and this worked best in juxtaposition to other boundary handling methods like zeroing.
  4. Next I used im2col to extract the neighborhoods in the form of columns and used the sliding property to remove padding with zeros
  5. Now just multiply the filter vector by the block and reshape the matrix to the dimensions of size(image(1)) x size(image(2))

%Apply the same logic for all color channels%
extractedImage = image(:,:,color);
paddedImage = padarray(extractedImage, padSize, 'symmetric');
% No padding for the  (m*n)-by-((mm-m+1)*(nn-n+1)) block. Basically gets all the neighborhoods%
neighborhoods = im2col(paddedImage, size(filter), 'sliding');
%Doing regular matrix multiplication instead of element-wise multiplication% 
%since we have 1*filtersize x filtersize*neighborhoods%
filteredBlock = reshape(filterVector * neighborhoods, size(image, 1), size(image, 2));

Results from my_imfilter

Hybrid Images

To construct the hybrid images I first created the low spatial frequencies matrix by convolving image1 with my filter then created the high frequencies matrix by subtracting my_imfilter(image2, filter) from image2. Afterwards, I added the low frequencies and high frequencies to obtain the hybrid image. For each image pair, I tried different cutoff frequencies to achieve the best results

Low Frequencies High Frequencies Hybrid Image Scaled Hybrid Images Cutoff Frequency
7
7
3
5
6
6

Extra Credit

Low Frequencies High Frequencies Hybrid Image Scaled Hybrid Images Cutoff Frequency
9