Project 1: Image Filtering and Hybrid Images

Example hybrid image from my code.

The main idea of this project is to take two images and see what it would look like if they were combined together. The current state of the code assumes that the two input images are of the same size. The following describes my approach to creating hybrid images:

I begin by creating my own generalized image filter function in MATLAB, which takes in two matrices: one to represent a given image and one to represent a filter to apply to the image. Using the first link below, I split the given image into three color channels and established three output images, each starting as a zero matrix and one for each color channel. I create two variables for my filter matrix to represent the center pixel. Then come the for loops. Checking whether my current pixel lies on the edge of the image, I either copy the value of the current pixel if it is or apply my filter otherwise. I use a boolean to keep track of whether my pixel is on the edge (please see code snippet below). If I apply the filter, I calculate the upper and lower bounds of my nxn filter to an nxn neighborhood of pixels in my image (please see code snippet below) and take another pair of for loops. The code uses the same filtering forumula from the class slides to accumulate the total red, green, and blue value of the filtered center pixel of my output image. Once the inner for loop pair ends, I save my new rgb values to the three output images. Once I loop through all the pixels in this manner, I use the link below to concactenate the red, green, and blue channels of my new image into one filtered image, which is my output.

Once I finished the image filter, I used Professor James Hays' provided Gaussian blur filter to create the hybrid image (Thank you James!):

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

Since it is a Gaussian filter, which creates a blurring effect in images, we can create a high frequency image by subtracting off the Gaussian filtered version of an image from the original image. According to the project description, the low frequencies of one image plus the high frequencies of another image equals a hybrid image of the two, and that's the project. Code from the following URL taught me the correct MATLAB code to achieve my desired result:

  1. Here is where I learned to split an image into separate color channels or to combine separate images into one.

What I would have done for extra credit

I did not plan sufficient time to attempt extra credit for this project. I apologize. But if I did attempt extra credit, I would have handled the case where the filter dimensions are even. In this case, I would have taken the four values that could correspond to the center pixel and averaged them together for the value to apply to the center pixel. I would have repeated this step for each other entry in the filter matrix, and in this manner, I would have shrunk the nxn filter matrix into an odd (n-1)x(n-1) filter matrix and applied it.

Code snippets from project


%checking for fringe value
fringeValue = false;
if(i < centerRowFilter || i > (size(image, 1) - centerRowFilter + 1) || ...
            j < centerColumnFilter || j > (size(image, 2) - centerColumnFilter + 1))
	%handling fringe value as is for now  
        outputRed(i, j) = inputRed(i, j); 
        outputGreen(i, j) = inputGreen(i, j);
        outputBlue(i, j) = inputBlue(i, j);
	fringeValue = true;
end

%loop through the pixels corresponding to the relative filter locations and accumulate the color
%of the filtered pixel.  These are all the pixels to which we wish to apply a filter.  
%Please refer to my source code.
if(fringeValue == false)
	lowerBoundCurrentRow = i - (size(filter, 1) - 1) / 2;
        lowerBoundCurrentColumn = j - (size(filter, 2) - 1) / 2;
        upperBoundCurrentRow = i + (size(filter, 1) - 1) / 2;
        upperBoundCurrentColumn = j + (size(filter, 2) - 1) / 2;
end

Results of my code on several hybrid images

The first image is the original first image, the second image is the low frequency version, the third image is the original second image, the fourth image is the high frequency version, and the fifth image is the hybrid between the two.