Project 1: Image Filtering and Hybrid Images

Example of a hybrid image

This project involved writing an image filtering function and using it to create hybrid images. At a very basic level, there are two methodologies that are used here:

  1. Image Filtering
  2. Hybrid Images

Image Filtering

In order to filter an image, we take the filter and the image as the input. The first step that we need to do is to identify if the image is a colored image or not. We do this by checking the size of the image matrix.


if size(image,3) == 3
    red = image(:,:,1);
    green = image(:,:,2);
    blue = image(:,:,3);
    output2 = cat(3,myfilter(red,filter),myfilter(green,filter),myfilter(blue,filter));
else
    output2=myfilter(image,filter);

We then use our filter function to apply the filter. The first step in doing that is to pad the image. To do this, we look at the size of the filter matrix, and based on that decide how much padding is needed.


pad_rows = size(filter,1)-1;
pad_cols = size(filter,2)-1;
new_image = zeros(size(image,1) + pad_rows,size(image,2)+pad_cols);

The next step is to add padding to the image. I ran a for loop to do this. The code basically copies values from the image matrix to the new image matrix after adjusting for the padding.


m=pad_rows/2;
n=pad_cols/2;
for i=1:size(image,1)
  for j=1:size(image,2)
    new_image(i+m,j+n)=image(i,j);
  end
end

After this, we iterate over the original image, and for each pixel, apply the filter formula to perform the filter operation. We basically go to each pixel, and then each point iterate on the filter pixels to apply the filter operation, and then sum up the individual values from these operations to get the new value for each cell.


for i=1:size(image,1)
  for j=1:size(image,2)
    sum=0;
    for x=(-1*m):m
      for y=(-1*n):n
        sum=sum+(new_image(i+x+m,j+y+n)*filter(x+m+1 ,y+n+1));
      end
    end
    output(i,j)=sum;
  end
end

This output matrix is the result of the function

Hybrid Images

Hybrid images involve taking two images and combining them. In order to combine them, we remove high frequencies from one image and low frequencies from the other image. In order to do this, we used the formulas described in the SIGGRAPH 2006 paper. One image is filtered with a low pass filter to remove the high frequencies.

low_frequencies = my_imfilter(image1,filter);

From the other image, we remove the low frequencies by subtracting a low pass filtered version of this image from the original image

high_frequencies = image2 - my_imfilter(image2,filter);

We then combine high frequencies and low frequencies

hybrid_image = low_frequencies + high_frequencies;

Results

Intermediate Images

Intermediate Images here shows the low frequency and high frequency image.

I generated hybrid images for the five sample image pairs. In addition to that, I tried to generate hybrid image for two more event pairs

The first were images of Hillary Clinton and Donald Trump. These images were taken from CNN, who basically shot these as a part of the promotions for there debates. I assumed that this would help me get good hybrid images. However, the results were not really striking.

The second image pair was Arc de Triumph in Paris and India gate in New Delhi. These are similar monuments, so I hoped that good hybrid images could be derived from them, and that did happen as you can see in the results.