Project 1: Image Filtering and Hybrid Images

For this project, I implemented an algorithm to do image filtering which shares similiar behavior with matlab's built in function imfilter(). After that I also applied the my_imfilter function to make hybrid images.

Algorithm Explanation

The most tricky part of the filter algorithm is the image boundary, since a filtered image will always lose resolution without padding. At first I tried to apply filtering to original image first, and then pad the filtered image with 0s, but that does not look good, so I changed the order and padded the original image first with 0s, and then applied the filter. This way the filtered image looks more natural. In my algorithm, I looped through each small matrix (each of same size as the filter) in all RGB chanels, and sum up the dot product of the matrix and the filter, and assign the value to corresponding pixel in result matrix.

Example of code


%my_imfilter
s = size(image);
row_num = s(1);
col_num = s(2);
result = zeros(row_num, col_num, s(3));
k = size(filter, 1);
l = size(filter, 2);
image = padarray(image, [floor(k/2), floor(l/2)]);

for a=1:s(3)
    for i=1:row_num
        for j=1:col_num
            result(i, j, a) = sum(dot(image(i:i+k-1, j:j+l-1,a), filter));
        end
    end
end

output = result;

Filtering Results

These images are the results after applying my_imfilter function on the original image cat.bmp. From left to right are images produced by different filter: identity filter, blur filter, large blur filter, sobel filter, high pass filter and laplacian filter.

Hybrid Results

For hybrid result, I used two photos from the internet, the first one is a Chinese actor Yishan Zhang in silver suit, and the second one is a Chinese pingpong champion Jike Zhang in red jacket. I first cutted the images so that they are of same size and the figures' faces are at similiar position in the photo. After modifying the cutoff frequency, I got a high frequency image and a low frequency image, and an interesting hybrid image. We can see that when the image is of normal size, Jike Zhang with red jacket is shown, but with the image size getting smaller, we can see Yishan Zhang instead.