Project 1: Image Filtering and Hybrid Images

The following report describes implementing a custom version of MATLAB's imfilter function. That function is then used to create and explore hybrid images.

Implementing my_imfilter

Like MATLAB's imfilter function my_imfilter supports both grayscale and multi-layered 2D images. The filtering is done separately for each layer.

The blurred images shows the effect of
padding edges with zeros.

In my implementation of my_imfilter I chose the naive approach to handling the image edges, that is padding the image with zeros. This produces slightly worse result in the edges compared to relfecting the image. One example is when using a Gaussian filter since it then blurs the "black padding" into the output image as a black border.

Below is the main part of the algorithm. The output is calculated by multiplying the filter like a sliding window over the image matrix.


for i = 1:image_size(3)
    work_layer = zeros(image_size(1:2) + 2 * padding);
    work_layer(1 + padding(1):padding(1)+image_size(1), 1 + padding(2):padding(2)+image_size(2)) = image(:,:,i);

    out_layer = zeros(image_size(1:2));
    for x = 1:image_size(1)
        for y = 1:image_size(2)
            sub_matrix = work_layer(x:x + filter_size(1) - 1, y:y + filter_size(2) - 1);
            out_layer(x, y) = sum(sum(sub_matrix .* filter));
        end
    end
    out_image(:, :, i) = out_layer(:,:);
end

Generating hybrid images

Using my_imfilter and Gaussian filters on two different images we get two low frequency images. One of these are then subtracted to the original image, thus creating a high frequency image.

By tuning the cutoff_frequency, that is the standard deviation in pixels of the Gaussian filter, we can select which frequencies we take from each image.


filter = fspecial('Gaussian', cutoff_frequency*4+1, cutoff_frequency);
low_frequencies = my_imfilter(image1, filter);
high_frequencies = image2 - my_imfilter(image2, filter);
hybrid_image = low_frequencies + high_frequencies;
	

Below is an hybrid image of Einstein and Monroe. First the low frequency version, next the high frequency version and then the hybrid image. As seen from the hybrid images the image of Monroe is clear at a close distance and Einstein becomes more prominent at a distance.

Hybrid image results

To get the best hybrid images the cut-off frequency has to be tuned. Below is the best versions of five different hybrid image pairs.
Cut-off frequency Result
4
8
8
8
4