CS 143 / Project 1 / Image Filtering and Hybrid Images

Overview

The goal of this project was to implement an image filtering algorithm such that a filter could be applied to an image. Once this algorithm was implemented, it could be used to isolate the high frequencies or the low frequencies from an image. By combining the high frequencies from one image with the low frequenceis from another, "hybrid images" can be formed, which contain details from both images that change depending on the size at which the image is displayed.

The Algorithm

The algorithm for applying filters is relatively straightforward. Images in MATLAB are represented as a 3-dimensional array; the first two dimensions represent the image's Y and X coordinates respectively, and the third dimension represents the color channels (the red, blue, and green channels respectively). Each color channel is operated on independently of the others.

The Code

filter_y = size(filter,1);
filter_x = size(filter,2);
pad_y = filter_y/2 - .5;
pad_x = filter_x/2 - .5;
filter_vec = reshape(filter, 1, numel(filter));
for i = 1:size(image,3)
    image_channel = image(:,:,i);
    image_channel = padarray(image_channel, ...
                    [pad_y, pad_x], ...
                    'symmetric');
    channel_vecs = im2col(image_channel, [filter_y, filter_x], 'sliding');
    channel_vals = filter_vec * channel_vecs;
    new_channel = reshape(channel_vals, size(image,1), size(image,2));
    output(:,:,i) = new_channel;
end
After calculating the size of the the filter and the ammount of padding necessary for the image, the code does the following for each channel:
  1. It pads the edges of the images such that the middle of the filter will be able to reach each pixel of the original image.
  2. It creates a matrix where each column represents a cut-out of the padded image the same size as the filter
  3. .
  4. It multiplies the filter (similarly converted into a row) by the matrix described above; this effectively multiplies the filter's values with the corresponding image values in each column/cut-out and sums the results.
  5. It reshapes the resultant vector into a matrix the size of the original image.
The algorithm was written such that it would use as many built-in MATLAB functions as possible. The reasoning for this was two-fold. First, It makes the code concise, readable, and easy to follow. Second, because built-in functions are generally highly-optimized, it makes the resultant code much faster than if I were to implement the same functionality myself.

Hybrid Images

The above are some examples of hybrid images created using the image filtering algorithm described above. Low frequencies were obtained by filtering the images using a Gaussian filter, and high frequencies were obtained by filtering the image using a Laplacian filter (i.e. subtracting the filtered image from the original). When combined, they tend to have the overall form, colors, etc. of the low-frequency image but have the details of the high-frequency image. An interesting phenomenon is that when images are scaled down to smaller sizes or viewed from further away, they appear to resemble the low-frequency image more than when viewed at a larger size or from closer.