Project 1: Image Filtering and Hybrid Images

Hybrid Image generated from two images, one of me and one of a wizard.

In this assignment we write an image filtering function and use it to create hybrid images.













Implementation of my_imfilter

The code below is my implementation of the my_imfilter function. Explanation of the code is included as comments in the syntax highlighted block below.

Basically, we use the size of the filter to calculate the amount of padding required for the input image. Thereafter we the input image using the padarray function and the above calculated value. We then convolve the filter and each channel(if more than one) to obtain the output value for that channel. The padding takes care of boundary issues. We then return return this image


function output = my_imfilter(image, filter)
    % We use the size of filter to calculate how many zeros we need to pad the input image with on each side
    % This is half the dimension of the filter. We floor it to obtain an integer value
    fsize = size(filter);
    pad = floor(fsize/2);
    imsize = size(image);

    % We pad the image using the calculated value and the padarray function

    paddedImg = padarray(image,pad);

    % We initialize an empty output image
    outImg = zeros(imsize,'single');

    % In convolution we basically flip one of the signal before element wise multiplication with the other 
    % function and then add the result up
    % Hence in the case of image/matrix this flip corresponds to a 180 degree rotation. We use rot90 twice.
    rotfilt = rot90(rot90(filter));

    % We iterate over each pixel of the image and do element wise multiplication. The padding takes care of 
    % the boundary cases
    % We sum over the element wise multiplied matrix to obtain the value for that pixel. We repeat the 
    % process for as many channels as the image may have
    for i = pad(1)+1:pad(1)+imsize(1)
       for j = pad(2)+1:pad(2)+imsize(2)
           if size(image,3) > 1
               for k = 1:imsize(3)
                   outImg(i-pad(1),j-pad(2),k) = sum(sum(paddedImg(i-pad(1):i+pad(1),j-pad(2):j+pad(2),k).*rotfilt));
               end
           elseif size(image,3) == 1
               outImg(i-pad(1),j-pad(2)) = sum(sum(paddedImg(i-pad(1):i+pad(1),j-pad(2):j+pad(2)).*rotfilt));
           end
       end
    end

    % We return the image
    output = outImg;


Testing
my_imfilter

The following images are respectively:
Input ImageIdentity Image Small blur with box filterLarge Gaussian blur
Sobel filtered imageDiscrete Laplacian filtered imageHigh pass "filter"

Generating hybrid images

Thereafter, to generate hybrid images, we obtain the high frequency component of one image and the low frequency component of the other input image and add these together to obtain the hybrid image. In the code snippet below: filter is a Gaussian blur filter. We obtain low frequencies by applying this filter to an input image. We obtain high frequencies by blurring an image and subtracting the blurred image from the original. After that, we add the image pixel wise.

low_frequencies = my_imfilter(image1,filter);

high_frequencies = image2 - my_imfilter(image2,filter);

hybrid_image = low_frequencies + high_frequencies;

Hybrid images obtained for various inputs

The images that follow will adhere to the format below.

Input | Input | High Frequencies | Low Frequencies
Hybrid Image at different scales

Dog and Cat

Me and Harry Potter. Harry's image was obtained from here and appropriately cropped. Positioning was a challenge, as you can see.

Bicycle that aspires to be a motorcyle

The Tuna Sub (not sure if this fish is actually Tuna. All is fair in the search of funny titles though)

Its a plane, no it's a bird... (but certainly not Superman)

If you look closely, you can see Ms. Monroe donning a tie