Project 1: Image Filtering and Hybrid Images

Example of a hybrid image.

The goal of this project is to make an image filter processing function and use it to create hybrid images by combining two different images by applying low and high frequency filters respectively on each image and adding them together. If this hybrid image is viewed up close, the high frequency image is visible. However, as you move further away, the low frequency image gets clearer. An example of such a picture can be seen to the right. From up close, the cat is prominent but as you move further away the dog becomes more prevalent. The function I coded works with both greyscale and color images. However it only works on filters with odd sized dimensions to avoid ambiguity in selecting the center (most dominant) pixel.

Algorithms

Building the Filter

  1. Pad the image with a black border to be able to apply the filter even on the edges of the picture.
    
    padded_image = zeros(height, width);
    
    for i = 1:image_height
        for j = 1:image_width
            padded_image(i + middle_height - 1, j + middle_width - 1) = image(i, j, k);
        end
    end
    		
  2. Build a function to apply the filter to a pixel appropriately.
    
    function filtered = filter_func(i, j)
        start_height = i - middle_height;
        start_width = j - middle_width;
    
        filtered = 0;
    
        for x = 1:filter_height
            for y = 1:filter_width
                filtered = filtered + padded_image(x + start_height, y + start_width)*filter(x, y);
            end
        end
    end
    		
  3. Pass each pixel of the image into this function.
    
    for i = 1:image_height
        for j = 1:image_width
            output(i, j, k) = filter_func(i + middle_height - 1, j + middle_width - 1);
        end
    end
    		

You can see this function at work with various filters

Original Image Blur Image Large Blur Image
Identity Image Laplacian Image Sobel Image


Creating the Hybrid Image

  1. Create a Gaussian filter with an appropriate cutoff frequency (based on the images to be blended).
    
    cutoff_frequency = 7;
    
    filter = fspecial('Gaussian', cutoff_frequency*4+1, cutoff_frequency);
    		
  2. Apply the filter on the image you want only the low frequencies of. The Gaussian filter will blur out all the high frequencies.
    
    low_frequencies = my_imfilter(image1, filter);
    		

  3. Apply the filter on the second image. However, since you want only the high frequencies of this image, subtract the filtered image from the original image to remove all the low frequencies and only be left with the high frequencies.
    
    high_frequencies = image2 - my_imfilter(image2, filter);
    		

  4. Add these two processed imaged together to get a hybrid image.
    
    hybrid_image = low_frequencies + high_frequencies;
    		


Effect of Cutoff Frequency

Different cutoff frequencies were tried and applied to each set of images in order to get the best possible hybrid image. The results displayed here are the outcomes of this experiment. The cutoff frequencies tried for each set of images were based on guesses made by following intuition. As the cutoff frequency is increased, the low frequency image gets blurrier and as the cutoff frequency is decreased, the high frequency image gets sharper.

Experimenting with cutoff frequencies on Marilyn and Einstein:


Cutoff Frequency = 1

Cutoff Frequency = 12



Images

Results of Other Hybrid Images

The following images are the outcomes of the best guessed cutoff frequncy

Marilyn and Einstein




Submarine and Fish




Bird and Plane




Motorcycle and Bicycle




Justin Bieber and Miley Cyrus