Project 1: Image Filtering and Hybrid Images

Example of a hybrid image.

The biggest part of this project was image filtering by implementing my_imfilter.m. The way I went about it was first understanding how filtering worked. Filtering iterates through all of the pixels in the image and uses the filter to affect the center pixel amongst its neighbors. Knowing this, I went into the implementation side. I padded the image with 0's so that the center of the filter would first iterate through the first pixel of the image. Allow m to be the row size of the filter and n to be the column size of the filter. I padded the image by the floor of m/2 and the floor of n/2 to the beginning and end of the rows and columns, respectively. This centered the image inside a pad of 0's, making its size, row + floor(m/2) x column + floor(n/2). The algorithm iterates through each pixel and filters each color (red, green, and blue) at a time using element-wise multiplication on a submatrix of the image the size of the filter and the filter. The sum is calculated and stored as the pixel of the output image.

Hybrid Images

Creating hybrid images are not too hard once my_imfilter.m is implemented. In this project, a Gaussian filter with a cutoff frequency of 7 pixels is given to us. Using this filter, the low frequency can be found by applying it with the first image using my_imfilter. The high frequency can easily be found by subtracting the low frequency of the second image from itself. Element-wise addition results in the hybrid image. Some things to do before creating these hybrid images are centering the images to each other to create a better result.

Exampe Code


function output = my_imfilter(image, filter)

m = size(filter,1); % gets row size of filter
n = size(filter,2); % gets column size of filter

padded = padarray(image, [floor(m/2), floor(n/2)]); % pads image with 0's
output = zeros(size(image,1),size(image,2),size(image,3));

% iterates through each submatrices of image by RGB matrices and filters
for i = 1:size(image,1)
    for j = 1:size(image,2)
        sub = padded(i:i+m-1,j:j+n-1,:);
    	for k = 1:size(image,3)
            matrix = sub(:,:,k) .* filter;
            output(i,j,k) = sum(matrix(:));
        end
    end
end

Results in a table

The first row is the results from running my_imfilter.m on a picture of a cat. The images are filtered as follows: identity, small blur with a box filter, large blur, sobel operator, discrete laplacian, and high pass, respectively from left to right. The other rows are hybrid images in the following order: low frequency, high frequency, hybrid, and hybrid scale, respectively from left to right. The last row has a picture of my friend and Bruce Lee combined. The algorithm works as intended, resulting in hybrid images, combining low and high frequency images.