Project 1: Image Filtering and Hybrid Images

Example of Hybrid Image.

In Project #1, I was asked to implement a linear filter, in this case a box filter, that would preform a number of convolutions over a set of test images. The function, my_imfilter, was supposed to be a general linear filter model so that a number of different filters and images could be passed in, following the same algorithmic idea. The steps of the algorithm are listed below, and they are also found in the book.

  1. Find filter length and width, create hFuncMatrix which is the size of the image matrix (without padding).
  2. Pad input image with filter length and width.
  3. Iterate through padded image, find the dot product of the filter and submatrix overlapped by the filter, and then set the corresponding cell in the hFuncMatrix to the dot product result.

The algorithm, as described in the book, involves 4 seperate for loops that preform the convolution. While this can be computationally expensive, it is fairly clear and easy to understand. The padded portion of the algorithm is necessary since the convolution naturally makes the output image smaller. Padding allows all of the pertinent pixel values to be included in the filtered image. The code also had to work for a color image, which meant iterating through the 3D color matrix.

Example of Algorithm

Below is a snippet of the code from my project. This part of the code preforms the iterative convolution to calculate the dot product of each filter area and output the correct hFuncMatrix.

Example of code with highlighting

The javascript in the highlighting folder is configured to do syntax highlighting in code blocks such as the one below.


%example code
for i = 1:(img_row_length - filter_row_length + 1)
        for j = 1:(img_col_length - filter_col_length + 1)
            %Create Sub-Matrix from Img
            sum = 0;
            for k = 0:filter_row_length - 1
                for l = 0:filter_col_length - 1
                    sum = sum + ...
                    paddedMatrix(i + k, j + l, color) * filter(k + 1, l + 1);
                end
            end
            hFuncMatrix(i, j, color) = sum;
        end
end

Results in a table

Above, you can see the output images from the filtering operation. The first row consists of the test images for the filter convolution, including laplacian and sobel filter. The second row is the breakdown of a hybrid image. The last row consists of 4 other hybrid images created by the same algorithm.