Look at the above image from a very close
distance and then from far away
I have implemented the algorithm for image filtering and further creating a hybrid image using two filtered images. Some of the key points covered in the implementation are:
The image on the right is a sample output of the code. It is a hybrid image of Albert Einstein and Marilyn Monroe. The image of Albert Einstein is passed through a high pass filter and the image of Marilyn Monroe is passed throgh a low pass filter. Hence when you look at the image from a close distance you see Albert Einstein but if you look at it from far away you see Marilyn Monroe.
The basic idea behind the algorithm is to get the dimensions of the input image in a 3-element vector. Color images have 3 dimensions where as gray-scale images have 2 dimensions. Since matlab returns 1 for the dimensions if the vector size has more dimensions than the array passed to the size function, the third element has value 3 for color images and value 1 for Gray-Scale images.
The code then loops from 1 to third dimension. I use the padarray() function provided by matlab to pad the sub array for a particular dimension with the reflected image content. The padding is done with dimensions equal to floor(half the filter size). So for example for a 3 x 3 filter, the padding is done with [1 1].
Then using every cell of the output image as the center, a window is formed around this cell with the size of the filter and a value is obtained by adding the product of the filter and the corresponding cells in the padded input sub array.
The ouput image has the same dimensions as the input image (before padding) and the above steps are implemented n number of times (where n is the value of the third dimension of the imput image).
Following code implements the image filtering algorithm described above:
%dim3 is the third dimension of input array
%h2 is half of the filter height (half the number of rows in filter array)
%w2 is half of the filter width (half the number of columns in filter array)
for i = 1:1:dim3
sub_image = image(:,:,i);
padded_array = padarray(sub_image,[h2 w2],'symmetric');
[d1,d2] = size(padded_array);
for j = 1+h2:1:d1-h2
for k = 1+w2:1:d2-w2
output_image(j - h2,k - w2,i) = sum(dot(padded_array((j-h2):(j+h2),...
(k-w2):(k+w2)), filter));
end
end
end
From top left in clockwise direction :
1) Original Image 2) Small Blur 3) Large Blur 4) High Pass Image 5) Discrete Laplacian Image 6) Sobel Filtered Image