CS 194 Project 2: Fun with Filters and Frequencies!

By Sainan Chen

1 Overview

In this project, we are going to explore edge detector, image sharpening, hybrid images and image blending. All of these functions are achieved by appling filters on images using convolution.

2 Edge Detector (gradient filter)

As we know, a clear edge means there's a great difference between adjacent pixels, which also means there's a great gradient. To detect edges, we convolute the image matrix by x_gradient matrix [1 -1] and y_gradient matrix [1; -1], and the result would be the gradient on x direction at all pixels and the gradient on y direction at all pixels (2nd & 3rd image below). To view the overall edge/gradient, we calculate the magnitude of the combined gradient vector, which is (x_gradient^2+y_gradient^2)^0.5.

original image
gradient on x direction (vertical edges)
gradient on y direction (horizontal edges)
overall gradient magnitude (edges)

2.1 Improvement: Use Gaussian Filter (low frequency filter) first to reduce noise

We notice that in the result of last part, the edges are not smooth. To reduce the noise, we can convolute the image with Gaussian filter first, then convolute the result with the gradient filters. From the images below, we can see Gaussian filter did reduce the noise and make edges smoother. Another way to do this is by applying the gradient filter on Gaussian filter, then apply the combined filter onto the image. The two methods produce the same result because of the property of convolution.

Applied Gaussian filter to the image first, then applied gradient filter to the image
Applied gradient filter to Gaussian filter, the apply the combined filter to the image

.

2.2 Application: Image Straightening

One application to utilize edges is straightening an image. By maximizing the number of vertical and horizontal edges, we can rotate the image to be straight. Below are some examples with original image, its orientation histogram, straightening image and its orientation image. To reduce the effects caused by black edges produced during the rotation process, we crop the result image and only consider the center part. During the practice, however, the number of 0 angles is not accurate, because for a large area with an identical color, the angles for all pixels would be 0 due to 0 y-direction gradient. Therefore, we want to focus more on number of 90 degree angles. Furthermore, we realized that besides the exact number, the peak is also a good indicator.

original
straightened(-5)
original
straightened(-12)
original
straightened(10)

Failure Case:

original
straightened(-15)

As we mentioned before, a large area with same color might influence the straighthening result unpredictably. For this example, the input above has few lines in it, and the edge plot shows few white lines while most of the pixels are being just black. Those black pixels dominate and have unpredictable influences on the angle distribution.

edge (magnitude) plot

.
.

3 Image Sharpening (high frequency filter)

In this part, instead of bluring an image, we are going to use Gaussian filter to sharpen an image. If we subtract the filtered image from original image, the part with highest frequencies (sharpest part) will be left. By adding this sharpest part back to the image, we can gain an a sharpened image.

original
sharpened
sharpened
original
original
sharpened
sharpened
original

3.1 Blur then Sharpen

If we blur an image using a Gaussian filter first then try to recover it using sharpening method mentioned before, we notice that there is still some information lost during the process.

original
blurred
original
recovered from blured version (lose info)

4 Hybrid Image (combined high & low frequency filters)

By combining high frequency filtered image and another low frequency filtered image, we can produce an hybrid image which will show high-frequency image when audience is close to the screen and loww-frequency image when audience is far away.

original 1
original 2
hybrid
original 1
original 2
hybrid

For next example, we are going to do frequency analysis. As we can see, although the frequencies of the two input images are similar, only low frequencies are left for filtered input 1 and only high frequencies are left for filtered input 2. Tshere is no ringing effect on the result frequency plot, which means the result image is smooth.

original 1
original 2
hybrid
log magnitude of the Fourier transform of input 1
log magnitude of the Fourier transform of input 2
log magnitude of the Fourier transform of hybrid output
log magnitude of the Fourier transform of filtered input 1
log magnitude of the Fourier transform of filtered input 2

Failure Case:
In the following example, because the cat in the second image has overall more black tone compared to the dog in the first image, its high-frequency filtered result has strong and clear edges which dominate the hybrid image over the very blurred dog image. Therefore, even if we stay far away from the hybrid image, we may still only see the cat instead of the low-frequency dog.

original 1
original 2
hybrid

5 Gaussian and Laplacian Stacks

Now we want to look at Gaussian stacks with increasing sigma for Gaussian filters and corresponding Laplacian stacks. For Gaussian layer 1, 2, 3 and 4, the sigmas for the Gaussian filters are 2, 2^2, 2^3, 2^4 reqpectively. Laplacian layers are calculated by subtracting the Gaussian layer in the same level from the Gaussian layer in the previous level. Higher the level, lower the frequency that the Laplacian layer contains. Here we have examples of an interesting painting as well as the hybrid image we created in last section.

original image
Gaussian layer 1
Gaussian layer 2
Gaussian layer 4
Gaussian layer 3
Laplacian layer 1
Laplacian layer 2
Laplacian layer 4
Laplacian layer 3
original image
Gaussian layer 1
Gaussian layer 2
Gaussian layer 4
Gaussian layer 3
Laplacian layer 1
Laplacian layer 2
Laplacian layer 4
Laplacian layer 3

6 Multi-resolution Blending

Now we are ready to blend two iamges together smoothly using Gaussian and Laplacian stacks! Instead of just put half and half iamges together and having a clear boundary between the images, we separate each image into Laplacian layers and one final Gaussian layer (so the sum of all these layers will be the original image) to better blend the two images. The Laplacian stacks make sure we preserve details and clear edges, while the base Gaussian layer makes the blending smooth and looks natural. We also use Gaussian stacks for masks. The second step is to apply masks onto images in every layer using LA*GM + LB*(1-GM), where LA is Laplacian layer for image A, LB is Laplacian layer for image B, GM is Gaussian layer for mask, and teh result is a combined image for that layer (last row on the graph). The last step is to add up combined images for all layers (don't forget last layer, which is Gaussian stack for both images!).

Example 1: Oraple
Example 2: Fish in the sky!
Example 3: City day & night

7 Conclusion

In this project, we explored many interesting photo filters, and actually all these functions are achieved based on convolution and Gaussian filters! The main idea is preserving/strengthening details using high frequency filter (original image - gaussian filtered image) and reduce rapid random noise using low frequency filter (gaussian filter). What I learned most from the project is that instead of manipulating the image directly, it will always be beneficial to consider the image as a compound of low frequency part and high frequency part and then deal with the two parts respectively according to their properties.



Photo source:
Section 4 young & old woman: https://www.thisiscolossal.com/2020/04/covid-19-getty-recreations/
Section 4 girl & painting: https://www.insider.com/young-vs-old-photo-portrait-comparison-100-years-2019-11
Section 5 painting: https://www.pinterest.com/pin/191966002836217780/