Project 1: Image Filtering and Hybrid Images

Ah, the first Automobile! Now take a few steps back, and have a look! Wait, is that a GLA?

The aim of this project is to create a hybrid image by combining two images, and filtering them for low and high frequencies. The high frequency image dominates the view when the hybrid image is looked at from a close distance, and the low frequency image dominates when the viewer moves far from the image. By combining these two filtered images, a hybrid image is obtained.

To perform the filtering of images, Matlab's inbuilt command 'imfilter' has been replicated in this assignment. The overall process followed to create a hybrid image:

  1. Padding the input images
  2. Calculating the low and high frequency images
  3. Adding the above two images to form the hybrid image

Algorithm

Code


%implementation of my_filter function
function output = my_imfilter(image, filter,type)
% Handles the cases where the filter sizes are not given as odd numbers
if (~mod(size(filter, 1), 2) || ~mod(size(filter, 2), 2))
    error('The row and column size of the filter have to be in odd numbers.');
end

% The default option is padding with zeros
if (nargin < 3)
    type = 'zero';
end

%Calclate the row and column margin, to pad the input image
row_margin = (size(filter, 1) - 1) / 2;
col_margin = (size(filter, 2) - 1) / 2;
output  = zeros(size(image));

%processing the input image
for ii = 1:size(image,3)    
	% pad the image array with zeros
    if strcmp(type, 'zero')
        input(:,:,ii) = padarray(image(:, :, ii), [row_margin col_margin]);
    %pad the image array using the value of the nearby pixel
    elseif strcmp(type, 'replicate')
        input(:,:,ii) = padarray(image(:, :, ii), [row_margin col_margin], 'replicate');
    end
end

%processing the output image
for ii = 1:size(image,3)
    for jj = 1:size(input,1)-size(filter,1)+1
        for kk = 1:size(input,2)-size(filter,2)+1
         %Calculate the output by convoluting the image and the filter
         output(jj,kk,ii) = sum(sum(filter.*input(jj:jj+size(filter,1)-1,kk:kk+size(filter,2)-1,ii)));
        end
    end 
end
end

Results

The example of a hybrid image, formed by merging a cat and a dog has been taken to illustrate the response to different filters, and the intermediate steps in the formation of a hybrid image.
For the rest of the image pairs, the input and output images alone are shown.

Results in a table

proj1_test_filtering


Identity Filter

Small Blur

Large Blur

Sobel Filter

Laplacian Filter

High pass filter

Dog (Low_Freq) and Cat (High_Freq) (Cutoff Freq = 7)


'Dog' - Removing the high frequencies

'Cat' - Removing the low frequencies

Hybrid Image

Time taken to run the program


Time taken to run my_imfilter

Time taken to run imfilter

There is a clear difference in the execution time between the inbuilt imfilter function and the looping statements written to replicate the function. One reason for this could be that, imfilter calls conv2 for convolution, which is a built=in matlab function, and is already pre compiled with the software. To minimize the looping statements, im2col and col2im functions were used. This reduced the two nested for loopes which were used previously to one. Surprisingly, the execution time results obtained with these commands show a even larger execution time.


More examples of Hybrid Images

Motorcycle (Low_Freq) and Bicycle (High_Freq) ( Freq1 = 8, Freq2 = 3)


'Motorcycle' - Removing the high frequencies

'Bicycle' - Removing the low frequencies

Hybrid Image

Bird (Low_Freq) and Plane (High_Freq) ( Cutoff Freq = 7)


'Bird' - Removing the high frequencies

'Plane' - Removing the low frequencies

Hybrid Image

Einstein (Low_Freq) and Marilyn (High_Freq) (Freq1 = 6, freq2 =5)


'Einstein' - Removing the high frequencies

'Marilyn' - Removing the low frequencies

Hybrid Image

Below is an example of the hybrid images formed when different the images are filtered alternatively for high and low frequencies.

Fish (Low_Freq) and Submarine (High_Freq) (Freq1 = 6, freq2 =3)


'Fish' - Removing the high frequencies

'Submarine' - Removing the low frequencies

Hybrid Image

Submarine (Low_Freq) and Fish (High_Freq) (Freq1 = 6, freq2 =3)


'Submarine' - Removing the high frequencies

'Fish' - Removing the low frequencies

Hybrid Image

To try creating a hybrid image from a different set of images apart from the image pairs given for the assignment, the first car ever made and the Mercedes GLA were merged. Indeed, Mercedes has come a long way!!

Mercedes-Benz GLA (Low_Freq) and Three Wheeled Motorwagen (High_Freq) (Freq1 = 7, freq2 =4)


'Mercedes-Benz GLA' - Removing the high frequencies

'Three Wheeled Motorwagen' - Removing the low frequencies

Hybrid Image