Project 1: Image Filtering and Hybrid Images

In this project, I implemented the following:

  1. My own version of image filter function as imfilter() in matlab
  2. Image blending to create hybrid image using Gaussian blurring (low frequency image A + high frequency image B)
  3. Examples of image blending

Image Filter Implementation

Since all images in this project are RGB image, I assume image inputs have 3 channels. This can be easily modified into 1-channel version. (Or we can simply copy grey image into three same channels).

For simplicity, the image is padded sufficient number of zeros. Filter is centered at each pixel.


padded_image = zeros(filter_size(1) + image_size(1) - 1, filter_size(2) + image_size(2) - 1, image_size(3));
padded_image(0.5*filter_size(1)+0.5:0.5*filter_size(1)+0.5+image_size(1)-1, ...
	0.5*filter_size(2)+0.5:0.5*filter_size(2)+0.5+image_size(2)-1, :) ...
	= image;
for zz=1:image_size(3)
  for ii=1:image_size(1)
    parfor jj=1:image_size(2)
      output(ii, jj, zz) = ...
        sum(sum(padded_image(ii:ii+filter_size(1)-1, jj:jj+filter_size(2)-1, zz) .* filter));
    end
  end
end

Hybrid Image Implementation

The low frequency image is obtained by filtering the original image with Gaussian filter. The high frequency image is obtained by subtracting its corresponding low frequency image.


cutoff_frequency = 5;

filter = fspecial('Gaussian', cutoff_frequency*4+1, cutoff_frequency);

low_frequencies = my_imfilter(image1, filter);

high_frequencies = image2 - my_imfilter(image2, filter);

hybrid_image = low_frequencies + high_frequencies;

Results

The first example is from the default image.

The second example is a first try to blend Einstein and cat image. Notice that since the face of Einstein and cat are not aligned, the hybrid image looks bad. Also, it looks that directly blending grey image and color image does not work well.

The third example is a second try to blend Einstein and cat image. This time we make the cat image smaller and locate the cat face to align with Einstein (by padding with some values appropriately, will explain later). Besides, cat image is transformed into grey image (by simply averaging each channel).

The values in Einstein image are scaled by 0.9 to make the lightness in both images closer.

The padded values in cat image are set to be the same values in the Einstein image (instead of padding by zeros) to prevent both sharp trainsition and undesirable looking in the outer region. More detailedly:


image2 = imresize(image2, [300, 300]) * 0.9;
image1_tmp = imresize(image1, [170, 170]);
image1 = image2;
image1(35:35+170-1, 65:65+170-1, :) = image1_tmp;
image_tmp = mean(image1, 3);
image1(:,:,1) = image_tmp;
image1(:,:,2) = image_tmp;
image1(:,:,3) = image_tmp;

The fourth example is a third try to blend Einstein and cat image. (I keep all the modification in the last example.) This time, instead of using the same filter for both low and high frequency images, we use Gaussian filter with higher sigma for high-freq image. This would leave high-freq image with more information and the sharp transition between cat and outer region of Einstein image will be less visible.

The modification is shown below


cutoff_frequency = 5;
filter = fspecial('Gaussian', cutoff_frequency*4+1, cutoff_frequency);

high_freq_cutoff_frequency = 30;
filter2 = fspecial('Gaussian', cutoff_frequency*4+1, high_freq_cutoff_frequency);

low_frequencies = my_imfilter(image1, filter);
high_frequencies = image2 - my_imfilter(image2, filter2);