Project 1: Image Filtering and Hybrid Images

I implemented my filtering algorithm using the following steps:

  1. Pad the input image with extra rows/columns of zeros whose sizes were based on the height/width of the filter. The number of rows/columns was determined by the number of "steps" it takes to get to the top/side of the filter. This allowed it to pad for any size filter.
  2. Create a matrix of zeros with the same resolution as the input image. Height, width, and number of colors needed to be copied.
  3. Using a triple for loop (over height, width, and number of colors) apply the filter to the neighborhood of each pixel. This was done by doing element wise multiplication of the matrices (the filter and the neighborhood), summing the result, and storing that sum as the corresponding value in the new image matrix.

My filter function code was as follows:


%filter function
function output = my_imfilter(image, filter)

vert = floor(size(filter, 1)/2);
horz = floor(size(filter, 2)/2);
res = size(image);
 
padded(:,:,:) = padarray(image(:,:,:), [vert horz]);
new = zeros(res);
 
for i=vert+1:res(1)+vert
    for j=horz+1:res(2)+horz
        for k=1:res(3)
            dotted = padded(i-vert:i+vert, j-horz:j+horz, k) .* filter;
            new(i-vert,j-horz,k) = sum(dotted(:));
        end
    end
end
 
output = new;

My implementation did not run as quickly as the standard Matlab standard implentation, but the run time was still acceptable. It was able to apply the six filters in the test script in around 25 seconds, making it around 5 times slower than the Matlab standard implementation. Here are the outputs from the test script:

I constructed the hybrid image with the following steps:

  1. Apply a Gaussian blur to the first image to get the low frequencies.
  2. Get the high frequencies of the second image by subtracting the result of the Gaussian blurred image from itself.
  3. Apply element wise addition to the resulting image matrices to get the hybrid image

The code was as follows:


%construct hybrid image
low_frequencies = my_imfilter(image1, filter); 
high_frequencies = image2 - my_imfilter(image2, filter);
hybrid_image = low_frequencies + high_frequencies;

I ran this with a few of the image pairs to see the results. I found that tinkering with the standard deviation of the Gaussian blur filter had a great effect on the image. When the blur is smaller, the blurred (lower frequency image) shows more clearly. When the blur is larger, the high frequency image is clearer. This is an intuitive result, but it was still interesting to play with it and see the effects. The results are below: