Project 1: Image Filtering and Hybrid Images

Example of a right floating element.

When we look at the image, its a cat but as we go few meters away it starts looking like a dog. Also if we enlarge the same image it looks like a cat, whereas when we reduce the size its a dog. What is it basically? What is happening is that we are cheating our eyes by passing particular frequencies at a particular distance. It’s basically hybrid image of both dog and Cat.

Thus we can say that we are superimposing some parameters of first image with some of the other, forming an image with characteristics of both the images which gets highlightened with change in location. Thus to produce such image, we need to carefully choose the parameters of the image. If we observe, one image is sharpened and the other is blurred. In technical terms, we are allowing high frequencies to pass by using a high pass filter whereas the other image is processed by low pass filter.

A filter is basically a tool used on an image for performing processes like sharpening, smoothing etc. There is a need to specify the cut-off frequency for the filter to pass. The cutoff frequency will set a limit for the filter to allow particular frequencies on images. There are different types of filters to act on an image. In the assignment we have used Gaussian filter to blur the image. The other image is sharpened by simply subtracting the blurred image from its image.

Let us consider a simple 3*3 filter on 5*5 image.We can get blur image by taking convolution multiplication of filter with 3*3 box of the image and assign the sum of the values to the middle pixel of image. Qualitatively, the pixels neighbouring the middle pixel are causing the values to change.

If we take a closer look at the process, we will observe that the pixels at the edge of the image are untouched. This problem can be solved by adding additional pixels at the edge of the image. This method is called as padding. Padding can be done in different ways. We can just add zeros at the end of image. The result is blackness at the corner which can be sometimes undesirable. Some other types of padding are replication of the neighbouring pixels called as replication.Or replicating the opposite edge pixels called as symmetric padding. If we summarize all the previous points we can form an hybrid image. Following steps need to be taken for creating hybrid images:

  1. Setting the cutoff frequency for the filter.
  2. Padding of image1.
  3. Filtering of image 1 to produce blur_image1
  4. Padding of image2
  5. Filtering of image2 to produce blur_image2
  6. Sharp_image2= image2-blur_image2
  7. hybrid_image=Superimposition of sharp_image2 and blur_image1

The steps can be used to get a hybrid image from the given two images.

Implementation of given test case

There are two code snippets given below. One is the my_filter function to filter the images sent to it. Second is the given hybrid image function. Here the hybrid image forms when the cut-off frequency is set to 4.

Code used for creating hybrid image

The chief content of the the code is the actual convolution multiplication carried out by 5 for loops. First for loop is for iterating fir color channels. The next two are for iterating for first image.

The third and forth for loop does the operation of iterating as well as creating the box for image to multiply.


%my_filter code
imshow(image);
imwrite(image, 'image_1.jpg', 'quality', 95);
[i,j]=size(filter);
pad_height=double(int8(floor((i/2))));
pad_width=double(int8(floor((j/2))));
padsize=[pad_height,pad_width];
padded_image = padarray(image,padsize,'replicate');
[m,n,p]=size(image);
imshow(padded_image);
imwrite(padded_image, 'padded_image.jpg', 'quality', 95);
output=zeros;
for z=1:3
    for x=pad_height+1:m+pad_height
        for y=pad_width+1:n+pad_width  
            temp=0;
            for a=-(pad_height):(pad_height)
                for b=-(pad_width):(pad_width)                    
                    temp=temp+(padded_image(x+a,y+b,z)*filter((pad_height+1+a),(pad_width+1+b)));
                end
            end
            output(x-pad_height,y-pad_width,z)=temp;
        end
        
    end
end
imshow(output);
end

%Hybrid image
image1 = im2single(imread('../data/dog.bmp')); %blur
image2 = im2single(imread('../data/cat.bmp'));   %sharp
cutoff_frequency = 4; 
filter = fspecial('Gaussian', cutoff_frequency*4+1, cutoff_frequency);
image3=my_imfilter(image1,filter);
image3=my_imfilter(image3,filter');
imshow(image3)
imwrite(image3, 'blurred_image.jpg', 'quality', 95);
low_frequencies = image3;
image4=my_imfilter(image2,filter);
image4=my_imfilter(image4,filter');
imshow(image4)
imwrite(image4, 'blurred_image.jpg', 'quality', 95);
high_frequencies =image2 - image4; 
imshow(high_frequencies)
imwrite(high_frequencies+0.5, 'sharp_image.jpg', 'quality', 95);

Results in a table

The above images are the images taken while creating hybrid of cat and dog.

  1. The first two images are the given test images..
  2. Second two images are the padded images of the test images.
  3. Third pair is the blured image.
  4. In cat we are sharpening the image of cat.
  5. We are adding the sharpened image of cat with blur image of the dog to get the hybrid image of the dog.
  6. Last image below is the hybrid image scaled to visualize the hybrid nature of the image.

Conclusion

While carrying out the assignment it was observed that different images have different cut-off frequencies. The cut-off frequency for the dog cat pair was found to be 4 by trial and error approach. The high frequency containing image is sharpened again and the low frequency image is blurred then the results are incorrect.