Project 1: Image Filtering and Hybrid Images

Image filtering is a technique which creates some type of effect or enhancement on an image. The exact effect depends on what filter is used. Some filters can blur an image while others can enhance the lines, causing the image to look sharper. The image below on the left depicts the use of a discrete Laplacian filter on center picture of a cat, while the one on the right applies a Sobel operator to the same image.

A Discrete Laplacian applied to the cat photo

A Sobel Operator applied to the cat photo

Your friendly neighborhood cat photo

Both of these types of filters perform their own unique operations on the pixels. More specifically, the filter is applied to each pixel in the form of a matrix applied to not only the target pixel, but the surrounding pixels. This method brings up the issue of applying the matrix to all the pixels near the edge of the picture. For example, when the filter matrix is applied to a corner pixel, only one quarter of the matrix has any pixels to operate on. In order to make sure the matrix is applied properly, we can pad the edges of the image with extra pixels. We'll still return an image of the original size, these extra pixels will just be to give the matrix something to operate on. For my implementation, I decided to flip the original image over each edge and corner and attach it there. I used this technique as opposed to simply padding with zeros as it still only uses nearby pixels, even if they are repeated. This keeps the edges looking in place with the rest of the image. Using zeros will usually create a dark border around the image as zeros essentially create a black border around the image. I also operated on each color channel individually to keep the code simple and easy to debug. This can be done as the pixels from each color channel only affect other pixels in the same color channel. Below is the code I used to actually apply the filter to each pixel. The dimensions of the original image are x and y, so I need to make sure i only apply the filter to the middle pixels. a and b are the radii of the filter.

Filter application code


%Cycle through each pixel and calculate it's filtered value
for m = x+1-a:2*x-a
    for n = y+1-b:2*y-b
        temp = padded(m:m+2*a,n:n+2*b);
        filtered(m-x+a,n-y+b) = sum(sum(temp .* filter));
    end
end

Hybrid Images

Hybrid images are a fusion of two images that have each had a particular filter applied to them. One image must be filtered of its low frequencies while the other is filtered of its high frequencies. In essence, this causes one image to become blurry while the other loses color and keeps sharper features instead. For example, take a look at the fish submarine below. The low filtered fish on the left retains its color but loses its definition while the exact opposite happens to the high filtered submarine on the right. The image underneath is a result of their addition.

Low pass fish

High pass submarine

Fishmarine

I also played around a little to see what switching the images would do. How different is the hybrid image when the two images swap filters? I had to adjust cutoff frequencies each time to make sure the animals were properly balanced. I judged that going by the middle photo of each fusion. It is difficult to identify one animal in both of them so they both are at a good average. It makes sense that swapping the filters would create vastly different hybrids as the whole mixing process is essentially the color scheme of one and the outlined edges of the other.

Low pass cat, high pass dog

Low pass dog, high pass cat

Below are some more hybrids. I played around with the cutoff frequencies to help keep the two photos in balance for each hybrid. Sometimes I used the same one for both images, other times I had to use different ones. This is because each photo has its own leanings from the start. Some images start off with lots of colors, or lots of defined edges. The cutoff frequencies have to be adjusted to account for this.

Example Hybrids