Project 1: Image Filtering and Hybrid Images

Cat and Dog Hybrid Image.

This project is about hybrid Images via the usage of a high frequency image and a low frequency image. This is done so by applying a filter over a frame or a subsection of the original image for each pixel in the image. Here is an example of a hybrid image.

Code and Algorithm Discussion

Before I can even start my algorithm, I need to first define some variables that i'll be using throughout the entire code. Specifically, they are row_count and col_count, and they are used to define the number of rows and columns in the filter. By themselves, they don't serve much purpose, but they are used to calculate vertical_padding and horizontal_padding. Since the filter has been assumed to always be an 'm x n' filter, where m and n are both odd numbers, we can calculate the actual padding by dividing the number of rows and columns by two, taking the mathematical floor of them both, and allocate those values to vertical_padding and horizontal_padding, respectively.


% Variables
row_count = size(filter,1);
col_count = size(filter,2);
vertical_padding = floor(row_count/2); %number of rows
horizontal_padding = floor(col_count/2); %number of columns

After defining those values, I need to create two more images. One of them is the padded_image, where I use Matlab's padarray method to create a padded version of my original image to allow for the filter's dimensions. Specifically, I used the symmetric option since that caused the padding to affect the convolution of the padded image the least. Other options, such as padding the image with 0's, would leave a shadow towards the edges of the image due to the dot-product taking in those values as part of the convolution algorithm. The second image that I created was a new image, that mocked the dimensions of the original image, regardless of it being grayscale (1-dimension) or color (3-dimensions). I also padded it with 0's as an initialized value, which would ultimately get replaced later on in the convolution algorithm.


% Image Setup
padded_image = padarray(image,[vertical_padding,horizontal_padding],'symmetric'); %creates a symmetrically padded array
new_image = zeros(size(image)); %creates an array filled with zeroes of the same size

Below is the core of my algorithm. There are three for-loops to allow the three dimensions of a color image: height, width, and the three rgb color channels. However, grayscale also works with the same code due to the fact that the color channel for-loop is dependent on the size of length(size(image)), which tells you how many color channels there are. One thing to note is that even though I am using the padded array, I am indexing across the unpadded, or the original image. This means that I'm indexing and making all of my calculations relative to the center of each frame. I thought that the code would be cleaner in the end,specifically when i have to calculate the coordinates of the new image's corresponding pixel and place a value in there. Next, I define the frame in which the dot-product would be performed on. This fram would match the size of the filter. After that, I calculate the dot-product of the two submatrices, and allocate that value to the cell at the same coodinates of the original image. I do this for every pixel in the image.


%Convolution Algorithm
for row = ((vertical_padding + 1):(vertical_padding + size(image,1)))
    for col = ((horizontal_padding + 1):(horizontal_padding + size(image,2)))
        for color_layer = 1:length(size(image))
            frame = padded_image(row - vertical_padding: row + vertical_padding,
                                 col - horizontal_padding: col + horizontal_padding,
                                 color_layer);
            dot_product = frame .* filter(:,:);
            blurred_value = sum(dot_product(:));
            new_image(row - vertical_padding ,col - horizontal_padding,color_layer) = blurred_value;
        end
    end
end

Application of Different Filters

Original Image

Identity Filter

Small Blur with a Box Filter

Large Blur

Sobel Filter

High Pass Filter (Laplacian Filter)

High Pass "Filter" Alternative

I applied many different filters onto the original image. The first of which is the identity matrix. This essentially replicated the original image, hence the name, identity function. The second applied filter was a box filter with a small blur. This filter collected the color channel values equally from each pixel within the frame, averaged it, and allocated the value to the correct spot in the new image, thus leaving a slight blur. The next image is of a larger blur. Normally, this can be done with just a larger filter, in this case a 25x25 filter. However, since this would take a long time, we expedited the process by performing a Gaussian blur with respect to the height and the width of the filter. This image is a lot blurrier than the previous image due to the fact that there was a larger filter and the values were corresponding to a gaussian matrix, meaning that the nearer the pixel is to the center of the filter, the more impact it has. Next is the Sobel filter, which, in this case, specifically highlights the horizontal gradients. Normally, this would result in a black image with colorful lines highlighting the horizontal features of the image, but the color has been offset by 0.5 to show more features. This next filter is the laplacian filter or a high pass filter, which is good at finding edges in the image. This resulted in an image that wasn't as detailed or colorful as the sobel filter applied image, but still was able to detail the edges very well. Finally we have the high pass filter alternative, which tries to accomplish the same results as the high pass filter. This is done so by simply subtracting the low frequecy content from the high frequency content. This produced similar results as the laplacian filter, but the edges weren't as stronly detected, which thus led to a less detailed version of the previous image.

Hybrid Image Generation

Original Cat Image

Original Dog Image

High Frequencies Image

Low Frequencies Filter

Hybrid Image of Varying Sizes

Original Einstein Image

Original Marilyn Image

High Frequencies Image

Low Frequencies Filter

Hybrid Image of Varying Sizes

Above is an example of a hybrid image generation. We have two unmodified images, of a cat and a dog. We will be extracting the high frequencies from the cat image and the low frequencies from the dog image. After that we will combine the two images together, which will yield in a hybrid image. We can see the cat up close, and the dog from afar. I also included a grayscale image to show that it was also compatable with that.