Project 2, Fun with Filters and Frequencies!

Part 1, fun with filters

Part 1.1, Finite Difference Operator

The first part of the assignment is about using difference operators to detect edges on a grayscale image. We have two difference operators, D_x and D_y. D_x is the matrix [-1, 1] and D_y is the matrix [-1, 1]^T. When we convolve an image with D_x, we get a value of the difference between a pixel and the pixel to the left of it. This essentially tells the derivative in the x-direction. A high derivative in the x-direction means that the pixel is likely a part of a vertical edge. Similarly, when we convolve an image with D_y, we get a value that tells the derivative in the y-direction. A high derivative in the y-direction means that the pixel is likely a part of a horizontal edge.

We can combine these two operators to get the magnitude of the gradient of the image. This is done by taking the square root of the sum of the squares of the two operators. This gives us a value that tells us how likely it is that a pixel is a part of an edge. We can then threshold this value to get a binary image that shows the edges of the original image. The threshold was found by trial and error, and was set to 0.26. The result of convolving with D_x, D_y, the magnitude of the gradient, and the binarized image are shown below.

edge_dx
Image convolved with dx.
edge_dy
Image convolved with dy.
edge_gradient
Image convolved with the gradient.
edge_gradient_binary
Image convolved with the gradient, binarized.

Part 1.2, Derivative of Gaussian (DoG) Filter

To avoid detecting noise as edges, we can convolve with a Gaussian filter to smooth the image before convolving with the difference operators and the gradient. The Guassian filter was used with a standard deviation of 1, and a kernel size of 6x6. The threshold was now set to 0.104. The result of convolving the smoothed image with D_x, D_y, the magnitude of the gradient, and the binarized image are shown below.

edge_dx
Smoothed image convolved with dx.
edge_dy
Smoothed image convolved with dy.
edge_gradient
Smoothed image convolved with the gradient.
edge_gradient_binary
Smoothed image convolved with the gradient, binarized.

As we can see, the edges are much clearer in the smoothed image. This is becuase the edges are now thicker becuase of the smoothing effect of the Gaussian filter. Also, we can see that there are much less noise in the binarized image. This is becuase the noise was smoothed out by the Gaussian filter.

Instead of convolving the image with two different filters to obtain the edges in one direction, we can convolve the image with a single filter that is the derivative in one direction of the Gaussian filter. In other words, we can convolve the Guassian filter with the difference operator, and then convolve the image with this new filter. This yields the same result as convolving the image with the Gaussian filter and then the difference operator. The result convolving the image with the difference of the Gaussian filter is shown below.

edge_dx
Image convolved with the DoG in the x-direction.
edge_dy
Image convolved with the DoG in the y-direction.
edge_gradient
Image convolved with the gradient of the DoG.
edge_gradient_binary
Image convolved with the gradient of the DoG, binarized.

As we can see, the result is more or less the same as convolving the image with the Gaussian filter and then the difference operator.

Part 2, Fun with Frequencies!

Part 2.1, Image Sharpening

The first part of the second task is about "sharpening" an image. This is done by adding more of the high frequencies to the image. The high frequencies is found by talking the original image and subtracting the smoothed image (low-pass filtered image).

Here is the result of sharpening Taj Mahal with a sharpening factor of 1.6:

edge_dx
Origianl image of Taj.
edge_dy
Sharpened image of Taj.

Here is the result of sharpening an old photo of me with a sharpening factor of 4:

edge_dx
Origianl image of me.
edge_dy
Sharpened image of me.

Here is the result of blurring an image of the Berkeley tower, then resharpening it again:

edge_dx
Origianl image of Berkeley tower.
edge_dy
Blurred image of Berkeley tower.
edge_dy
Resharpened image of Berkeley tower.

Part 2.2, Hybrid Images

The second part of the second task is about creating hybrid images. This is done by combining the low frequencies of one image with the high frequencies of another image. The low frequencies is found by taking the smoothed image of the first image, and the high frequencies is found by taking the original image and subtracting the smoothed image. The hybrid image is then created by adding the low frequencies of the first image with the high frequencies of the second image. Here are the result on three different hybrid images:

Favourite hybrid, Messi and Ronaldo.

What I liked most about this hybrid is that not only does Ronaldo change into Messi, but the Real Madrid logo and the Adidas logo on the shirt also changes into the Barcelona logo and the Nike logo.

edge_dx
Image of Ronaldo.
edge_dy
Image of Messi.
edge_gradient
Hybrid of Messi and Ronaldo.

Frequency analysis of the Ronaldo and Messi images:

edge_dx
Frequency of Ronaldo.
edge_dy
Frequncy of high pass filtered Ronaldo.
edge_gradient
Frequency of Messi.
edge_gradient
Frequency of low pass filtered Messi.

Another hybrid, an egg and a porcupine.

edge_dx
Image of an egg.
edge_dy
Image of a porcupine.
edge_gradient
Hybrid of egg and porcupine.

Bad hybrid, football and basketball

This hybrid did not work very well, as the football is always by far the most visible. I think this is because the black pacthes, which have a low frequency, is so dominant that even when you look closely, you can't see the basketball very well. However, you can see the contours of the basketball when looking closely, while it is not visible from a distance, so it was not a complete failure.

edge_dx
Image of football.
edge_dy
Image of basketball.
edge_gradient
Hybrid of football and basketball.

Part 2.3, Gaussian and Laplacian Stacks

The third part of the second task is about creating Gaussian and Laplacian stacks. This is done by creating a stack of images where each image is the result of convolving the last image with a Guassian. The Laplacian stack is then created by taking the difference between the Gaussian stack and the stack shifted one step down. Here are the results of the Gaussian and Laplacian stacks of an apple and an orange (sigma = 3 for all stacks, levels = 5 for all stacks):

Gaussian stack of the apple:

edge_dx
edge_dy
edge_gradient
edge_gradient
edge_gradient

Gaussian stack of the orange:

edge_dx
edge_dy
edge_gradient
edge_gradient
edge_gradient

Laplacian stack of the apple:

edge_dx
edge_dy
edge_gradient
edge_gradient
edge_gradient

Laplacian stack of the orange:

edge_dx
edge_dy
edge_gradient
edge_gradient
edge_gradient

The laplacian stack work as a band pass filter, where the first image contains the highest frequencies, and the last image contains the lowest frequencies. We can use these band pass fitlers to combine two images. Each layer in the Laplacian stack of the two images is combined by taking a weighted average of the two images around the center of the image. Then we add all the layers together. The result of combining the apple and the orange Laplace stack is shown below:

edge_gradient
Combining two images using blending.
edge_gradient
Combining two images without blending.

Part 2.4, Multiresolution Blending

The fourth part of the second task is about blending two images using the laplacian stacks together with a Guassian stack of a blending mask. The blending mask could have any shape, but for the first two images, it is a straight line on the center of the image. Here is the result on the orapple from the last task (sigma = 3 for the Laplacian stacks, sigma = 20 for the Gaussian stack, 5 levels for both):

edge_gradient
Combining orange and apple images using blending, different approach.

Then, I used the blending technique on two images of the same forrest. One is taken during the summer, the other is taken during the fall. Some of the image is black becaused I used the allignemnt function to properly aligne the two images. The result of the bledning on two pictures of a forrest, one taking during summer and the other during automn is shown here (sigma = 3 for the Laplacian stacks, sigma = 20 for the Gaussian stack, 5 levels for both):

edge_gradient
Combining two images of a forrest using blending.

The result looks much better than simply combining them on the center without doing any blending:

edge_gradient
Combining two images without using blending.

The multiresolution blending approach does not require the mask to be a step function in the center of the image. It can also be an irregular mask, so that you can blend in f.ex. a small figure into another image. Here is an example of blending Lionel Messi's face into the head of a goat (becuase he is the goat - greatest of all time), along with the mask that was used (sigma = 3 for the Laplacian stacks, sigma = 5 for the Gaussian stack, 5 levels for both):

edge_gradient
Messi blended into the head of a goat.
edge_gradient
Mask that was used to blend in Messi.

The result is much better than simply pasting Messi's face on top of the goat's head by using the mask only one time, without using the Guassian stack of the mask and the Laplacian stacks of the images.

edge_gradient
Messi head onto thes head of a goat, no blending.