Project 1: Image Filtering and Hybrid Images

Hybrid image

Image filtering is used for the purpose of enhancing images, extracting information from images, detecting patterns and other applications of Computer Vision. This first project implements a number of filters as will be displayed. The following code is in the filtering function my_filter

Padding of image with zeroes

The first step to take is to pad the input image with zeros along the sides. This is because the new filtered values cannot be calculated for pixels along the sides. The width of the padding is the floor of the dimensions of the filter divided by 2 as seen below.


% Padding of image with zeroes
input = image;
[filter_row, filter_col] = size(filter);
paddedImage = padarray(input, [floor(filter_row / 2), floor(filter_col / 2)]);

Implementing the filter of the image

The filtering formula below is implemented in code in order to compute the dot product of the image and filter matrices

The code for this is displayed below


% Calculate dot product of filter and image matrices

[row, column, layer] = size(image);
output = [];

for l = 1: layer
    for c = 1: column
        for r = 1: row
            filteredValue = sum(sum(paddedImage(r : (r + filter_row - 1), c : (c + filter_col - 1), l) .* filter));
            output(r, c, l) = filteredValue;
        end
    end
end

Filtered image results

Identity image filter

Large blur image filter

Sobel image filter

Laplacian image filter

High pass image filter

Creating a hybrid image

The hybrid image is created by combining the low frequency portions of the first image and the high frequency portions of the second image by creating high pass anf low pass filters. The my_filter function is used to create the image with low frequencies below a cutoff frequency. The second image that preserves the high frequencies does so by subtracting the low frequencies from the image. The code for this is shown below.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Remove the high frequencies from image1 by blurring it. The amount of
% blur that works best will vary with different image pairs
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

low_frequencies = my_imfilter(image1, filter);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Remove the low frequencies from image2. The easiest way to do this is to
% subtract a blurred version of image2 from the original version of image2.
% This will give you an image centered at zero with negative values.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

high_frequencies = image2 - my_imfilter(image2, filter);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Combine the high frequencies and low frequencies
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

hybrid_image = high_frequencies + low_frequencies;

The hybrid image and intermediate steps are shown below.

Hybrid image of submarine and fish. At low frequencies, you see the fish and at high frequencies, you see the submarine.

Original image of fish

Original image of submarine

High frequencies of submarine

Low frequencies of fish

Other hybrid images

Hybrid image of marilyn monroe and einstein (Albert Monroe :P). At low frequencies, you see monroe and at high frequencies, you see einstein.

Hybrid image of a bird and plane. At low frequencies, you see a bird and at high frequencies, you see a plane.

Conclusion & Discussion

The padding strategy employed in this project was simply to pad the image with zeroes or black pixels. While this was sufficiently accurate in the case of this small filter of 29 X 29, it may be a better idea to employ the strategy of reflecting across edges in the case of a big filter. In addition, using an image with edges that could prove to be sharper and more distinct as far as having high frequencies is concerned - would be mioe suitable for the high frequency image. Since the hybrid image does not retain the color of the low frequency image, having a low frequency image with fewer color blocks may be a good idea.