Project 1: Image Filtering and Hybrid Images

The objective of this project is twofold. Firstly, we are to write a image filtering function that implements the linear filtering algorithm, which is named as such because the convolution operator is a linear function. The code for the filtering algorithm is contained in my_imfilter.m. Secondly, we are to use this filter to generate hybrid images, which are the sums of a low-pass filtered image and a high-pass filtered second image. For the extra credit portion of this project, the separable filtering algorithm is also implemented, and the CPU times required for both algorithms are compared. A full list of MATLAB user-defined function are given below, along with their corresponding descriptions.

  1. my_imfilter.m : This is the implementation of the linear filtering algorithm.
  2. my_sep_imfilter.m : This is the implementation of the separable filtering algorithm.
  3. proj1_test_filtering.m : This is the code used to test the implementation of the linear filtering algorithm.
  4. proj1_test_sep_filtering.m : This is the code used to test the implementation of the separable filtering algorithm.
  5. vis_hybrid_image.m : This is the code used to concatenate hybrid images together for visualization purposes.
  6. proj1.m : This is the code used to create hybrid images.
  7. compute_cpu_time.m : This is the code used to compute the CPU time used by our filtering algorithms.

Code for linear filtering

The code for our implementation is given below. The key feature of this implementation is that at each iteration, a k*l matrix, corresponding to the dimension being masked by the filter, is selected from the image matrix at each iteration, where convolution between the it and the filter matrix generates an entry in the output matrix corresponding to the image matrix after filtering.


%% linear filtering algorithm
function output = my_imfilter(image, filter)
[m,n,s]=size(image);
[k,l]=size(filter);

output=zeros(m,n,s);
image=padarray(image,[(k-1)/2,(l-1)/2]);

for color=1:s
    for row=1:m
        for col=1:n
            A=image(row:row+k-1,col:col+l-1,color);
            output(row,col,color)=sum(sum(A.*filter));
        end
    end
end

Generating hybrid images

To generate a hybrid image using two aligned images, we apply linear filtering on both of them to get a high frequency and low frequency filtered image respectively. The hybrid image is then formed by summing both of these filtered images, and is visualized using the user-defined function vis_hybrid_image.m. The steps involved in this process are shown in the table below, for the case of the cat/dog hybrid. Both the high and low frequency images are shown, along with the result of their sum.

Hybrid images

The table below displays six hybrid images generated using the linear filtering algorithm, five of which originated from the images given in the original project folder.

Comparison with separable filtering

Separable filtering was implemented as part of the extra credit portion of this project, which involves decomposing the filter matrix using singular value decomposition, before replacing the original filter matrix (named 'filter' in code) with the decomposition. This may ideally decrease the computational time needed to filter larger images. However, the cpu time required to filter a ~200*200 and a ~1200*1200 matrix using both normal linear filtering and separable filtering are computed for 100 iterations, but the cpu required for separable filtering is in fact found to be higher. This may be attributed to the fact that these two matrices are still too small for separable filtering to be more computationally feasible.