Project 1: Image Filtering and Hybrid Images

My implementation of my_imfilter is straightforward and naive. It simply loops over each pixel of the input image, and computes the convolution of the image with the filter centered on that pixel. Here's the code:


row_loop_bound = (size(filter,1)-1)/2;
col_loop_bound = (size(filter,2)-1)/2;
filter_mid_row = row_loop_bound+1;
filter_mid_col = col_loop_bound+1;
output = zeros(size(image));
height = size(image,1);
width = size(image,2);

% loop over rgb panes
for rgb = 1:3
    % loop over rows and columns of the input image
    for row=1:height
        for col = 1:width
            % loop over each element of the input filter
            for i = -row_loop_bound:row_loop_bound
                for j = -col_loop_bound:col_loop_bound
                    % this conditional takes care of zero padding
                    if row+i >= 1 && row+i <= height && col+j >= 1 && col+j <= width
                        output(row,col,rgb) = output(row,col,rgb) ...
                        + image(row+i,col+j,rgb)*filter(filter_mid_row+i,filter_mid_col+j);
                    end
                end 
            end
        end
    end
end	

The zero padding is handled by the if conditional deep inside the nested for loops. If the filter has F elements and the image P pixels, then the code above requires approximately 3FP additions. The actual number is slightly less, given that some additions will be omitted near the edge of the image.

This implementation does not use any MATLAB tricks (because I do not know any); therefore, it may be slower on some machines. One might also say that it is ugly. However, it has the advantage of being easily understood and edited.

My implementation of proj1 followed the guidelines and hints mentioned in the comments exactly. I calculate low frequencies (the Gaussian blurred image) as follows:

low_frequencies = my_imfilter(image1, filter);

I calculate high frequencies by subtracting a Gaussian blurred image from the original image, as follows:

high_frequencies = image2-my_imfilter(image2, filter);

The resulting image high_frequencies has zero mean. We combine the two filtered images by simply adding them. Since the mean color intensity of low_frequencies is that of image1, the hybrid image will have similar color intensity to that of image1.

hybrid_image = low_frequencies+high_frequencies;

Example Results

Cat-Dog

Cat-dog does a good job of illustrating this visual illusion.

Cat and Tripcat

Dog and Foggydog

Cat becomes dog

Subgull

A submarine with a seagull is not such a great illustration. The reason for this is that, in the blurred image of the seagull, the bird is still readily apparent. Moreover, the features of the submarine with the gull do not clearly line up, as they do with cat-dog. For example, the cat's eyes line up clearly with the dog's eyes, as do the outlines of their faces, and their noses. No such alignment exists for Subgull. Also, the only obvious low-frequencies features of the sub is are various shades of blue, so the sharpened image is more or less just a decolorized sub. The submarine does clearly disappear in the downsampled image, however, as we expect.

You will also notice that the blurred gull is slightly smaller than the original gull. This was done through the MATLAB function imresize. imresize is called before the Gaussian blur is applied.

Sub and sharpsub

Gull and Foggygull

Sub disappears