Filters and Frequencies

Shivansh Baveja

Project Description

Throughout this project we will discover and recreate image-related concepts and techniques. These include image derivatives, convolution, sharpening, blurring, gaussian stacks, lapacian pyramids, hybrid images, frequency manipulation, and multiresolution blending. Credits for the images used go to their respective owners.

Part 1: Fun with Filters

Part 1.1: Finite Difference Operator

We define image derivatives as convolutions that take a specific form. In the case of this assignment, we consider the following gradient kernels. $$ dx = [1, -1], \quad dy = \begin{bmatrix} 1 \\ -1 \end{bmatrix} $$ grayscale image with the dx kernel roughly gives the local pixel-wise derivative horizontally across the image. Let \(p(x,y)\) be the intensity of pixel \((x,y)\). Then, we see that convolving with the dx kernel gives $$\frac{p(x_0 + 1, y_0) - p(x_0, y_0)}{1} \approx \left. \frac{\partial p(x, y)}{\partial x} \right|_{(x_0, y_0)}$$ This applies analogously to the dy kernel. These image derivatives can be considered as the following gradient. $$\nabla p(x, y) = \left( \frac{\partial p}{\partial x}, \frac{\partial p}{\partial y} \right)$$ Taking the elementwise sum and using the L2 norm, we get an image \(p'(x,y)\) where $$ p'(x_0, y_0) = \sqrt{\left(\frac{\partial p(x, y)}{\partial x}\right)^2 + \left(\frac{\partial p(x, y)}{\partial y}\right)^2} $$ In images with defined boundaries, this computation followed by a qualitatively determined thresholding can serve as an edge detector.
Original image
Original Image
dx image
dx kernel
dy image
dy kernel
Gradient Magnitude image
Gradient Magnitude
Binarized Gradient Magnitude image
Binarized Gradient Magnitude

Part 1.2: Derivative of Gaussian (DoG) Filter

It is important to note that smoothing the image prior to taking derivatives can result in cleaner, less noisy edge detections. As such, these kernels are often convolved together, to provide a single multipurpose kernel.
Gaussian smoothing, then dx kernel
gaussian smoothing, then dx kernel
Gaussian smoothing, then dy kernel
gaussian smoothing, then dy kernel
dx and Gaussian combined kernel
dx and gaussian kernel
dy and Gaussian combined kernel
dy and gaussian kernel
Gaussian filter, then gradient magnitude calculation
gaussian filter, then gradient magnitude calculation
Gaussian filter, gradient magnitude calculation, then binarization
gaussian filter, gradient magnitude calculation, then binarization
Combined Gaussian and derivative kernels
combined gaussian and derivative kernels
Combined Gaussian and derivative kernels, binarized
combined gaussian and derivative kernels, binarized

Here, we can make two observations. First, notice that convolving with a gaussian smoothing kernel prior to taking the derivative produced less noisy detections. Even further, the edges themselves are thicker, brighter, and more prominent—represented by connected, smooth lines instead of small, fragmented pieces. This is because the gaussian filter has removed the high-frequency noise and allowed us to more accurately extract the desired low frequency edge information.

Second, notice that we have now done with one kernel what previously required two, and that both of these pairs of images are nearly identical. There may be discrepancies on the order of a few pixels, but these are likely floating point errors or slightly different rounding procedures resulting from the varied order of operations. For all intensive and visual purposes, the combined kernel produces identical results with half of the convolution.

Part 2: Fun with Frequencies!

Part 2.1: Image "Sharpening"

In this section, we derive and use the unsharp masking technique. This technique works by first using a gaussian filter to blur the image (lowpass filter), and then subtracting this lowpassed image from the original to obtain the details of the image (highpass filter). These details can then be scaled by some factors alpha and added back to the original image as sharpening.

Notice that we can once again use the commutativity and associativity of convolution to combine both of these steps into one. As, such we can derive a kernel that integrates alpha, the gaussian smoothing kernel, and the extracted details of the image. We derive it as follows, where Q is the original image, B is the blurred image, \(h_{gauss}\) is the gaussian smoothing filter, and \(h_{\delta}\) is the unit impulse identity.

$$ \begin{aligned} \text{Sharpened Image} &= Q + \alpha (Q - B) \\ &= Q + \alpha \left( Q - (Q * h_{\text{gauss}}) \right) \\ &= Q + \alpha \left( (Q * h_{\delta}) - (Q * h_{\text{gauss}}) \right) \\ &= Q * \left( h_{\delta} + \alpha (h_{\delta} - h_{\text{gauss}}) \right) \\ &= Q * \left( (1 + \alpha) h_{\delta} - \alpha h_{\text{gauss}} \right) \end{aligned} $$

The combined kernel derived above was used to sharpen the following images, allowing for faster computation. Note that the below images depicting "details" are exceedingly faint by nature and are more easily visible if opened in full in another window.

Original Image of Taj
Original Image
Blurred Image (Low Frequency) of Taj
Blurred Image (Low Frequency)
Image Details (High Frequency) of Taj
Image Details (High Frequency)
Taj Image with m = 1
m = 1
Taj Image with m = 2
m = 2
Taj Image with m = 3
m = 3
Taj Image with m = 4
m = 4
Original Image of Lebron
Original Image
Blurred Image (Low Frequency) of Lebron
Blurred Image (Low Frequency)
Image Details (High Frequency) of Lebron
Image Details (High Frequency)
Lebron Image with m = 1
m = 1
Lebron Image with m = 2
m = 2
Lebron Image with m = 3
m = 3
Lebron Image with m = 4
m = 4

To test the effectiveness of our frequency-based sharpening, we will first blur an image and then attempt to sharpen it again. This is depicted below.

Original Image of Fox
Original Image
Blurred Image of Fox
Blurred Image
Sharpened Image of Fox
Sharpened Image

Notice here that although we were able to restore some of the visual detail using sharpening, much of the actual detail is lost during blurring. Increasing alpha further causes many of the regions to become visually distorted. As such, it does a respectable job, although it is unable to reconstruct the detail present in the old image.

Part 2.2: Hybrid Images

We now continue on to the illusionary technique of hybrid images. Using our gaussian lowpass filter from before, we combine the low frequencies of one image with the higher frequencies of another image, to produce an image that looks like two separate things from close and from far away. The images are first aligned to allow for hybridization, and the cutoff frequencies for each filter and hand-picked.

Before creating all of my selected hybridizations, I will test with color on a singular example. Below is a comparison of retaining no color, low frequency color, and both frequencies color. Since I believe that the "both frequencies color" image looks the best, this same style will be applied to the rest of the images.
No Color Image
No Color
Low Frequency Image Color
Low Frequency Image Color
High Frequency Image Color
High Frequency Image Color
Both Frequencies Color
Both Frequencies Color

Note that the examples in the below table are for the most part visually appealing. However, the last row hybridizing a door with a tennis court was a failure. The structure and shape of the aligned too well, as to overlap. Further, the high-frequency image (the tennis court) was fairly smooth and didn't contain much detail, and so it is overpowered by the lower frequencies of the door.

Low Frequency Image High Frequency Image Hybrid Image (high frequency) Hybrid Image (low frequency)
Derek + Nutmeg
Derek Original Image
Nutmeg Original Image
Hybrid Image of Derek + Nutmeg (high frequency)
Hybrid Image of Derek + Nutmeg (low frequency)
Lebron + Steph
Lebron Original Image
Steph Original Image
Hybrid Image of Lebron + Steph (high frequency)
Hybrid Image of Lebron + Steph (low frequency)
Baby Expressions
Baby Expression 1
Baby Expression 2
Hybrid Image of Baby Expressions (high frequency)
Hybrid Image of Baby Expressions (low frequency)
Tennis Court + Door (Failure)
Tennis Court Original Image
Door Original Image
Hybrid Image of Tennis Court + Door (high frequency)
Hybrid Image of Tennis Court + Door (low frequency)
My favorite depiction of hybridization above is the one combining Lebron and Steph. As such, I will be providing the respective log-magnitude image fourier transform for the respective original, filtered, and hybrid images. Notice that the lowpass filter removes a lot of the lower frequencies near the original the highpass does the opposite. The hybridized image fft looks like a combined version of the filtered versions.
Original Derek
Original Lebron
Original Nutmeg
Original Steph
Filtered Derek
Filtered Lebron
Filtered Nutmeg
Filtered Steph
Hybrid Image
Hybrid Image

Part 2.3: Gaussian and Laplacian Stacks

In this section we will be creating our own laplacian and gaussian stacks to aid us in our next goal, image blending. A gaussian stack is created by recursively smoothing and downsampling, while a laplacian stack is created by taking the differences of consecutive gaussian stack layers. The following depicts the normalized versions of these procedures on an apple and an orange.

Level 0 Level 1 Level 2 Level 3 Level 4 Level 5 Level 6
Mask Gaussian Stack Image at m_00 Image at m_01 Image at m_02 Image at m_03 Image at m_04 Image at m_05 Image at m_06
Apple Gaussian Stack Image at 1g_10 Image at 1g_11 Image at 1g_12 Image at 1g_13 Image at 1g_14 Image at 1g_15 Image at 1g_16
Apple Laplacian Stack Image at 1l_20 Image at 1l_21 Image at 1l_22 Image at 1l_23 Image at 1l_24 Image at 1l_25 Image at 1l_26
Apple Masked Laplacian Stack Image at 1m_30 Image at 1m_31 Image at 1m_32 Image at 1m_33 Image at 1m_34 Image at 1m_35 Image at 1m_36
Orange Gaussian Stack Image at 2g_40 Image at 2g_41 Image at 2g_42 Image at 2g_43 Image at 2g_44 Image at 2g_45 Image at 2g_46
Orange Laplacian Stack Image at 2l_50 Image at 2l_51 Image at 2l_52 Image at 2l_53 Image at 2l_54 Image at 2l_55 Image at 2l_56
Orange Masked Laplacian Stack Image at 2m_60 Image at 2m_61 Image at 2m_62 Image at 2m_63 Image at 2m_64 Image at 2m_65 Image at 2m_66
Blended Laplacian Stacks Image at 2b_70 Image at 2b_71 Image at 2b_72 Image at 2b_73 Image at 2b_74 Image at 2b_75 Image at 2b_76

Part 2.4: Multiresolution Blending

In this section we will collapse the blended laplacian stacks above to construct a smoothly blended orapple (orange + apple)! I will also be applying this technique to a few other irregularly masked images as shown below.

Original Image 1 Original Image 2 Mask Blended Image
Orange + Apple Orange Original Image Apple Original Image Mask for Orange + Apple Blended Image of Orange + Apple
Space + Mountains Space Original Image Mountains Original Image Mask for Space + Mountains Blended Image of Space + Mountains
Devin Booker + Lantern Devin Booker Original Image Lantern Original Image Mask for Devin Booker + Lantern Blended Image of Devin Booker + Lantern

Below I have attached the gaussian and laplacian stacks of the images and masks for both of the other multiresolution blends.

Level 0 Level 1 Level 2 Level 3 Level 4 Level 5 Level 6
Mask Gaussian Stack Image at m_00 Image at m_01 Image at m_02 Image at m_03 Image at m_04 Image at m_05 Image at m_06
Mountains Gaussian Stack Image at 1g_10 Image at 1g_11 Image at 1g_12 Image at 1g_13 Image at 1g_14 Image at 1g_15 Image at 1g_16
Mountains Laplacian Stack Image at 1l_20 Image at 1l_21 Image at 1l_22 Image at 1l_23 Image at 1l_24 Image at 1l_25 Image at 1l_26
Mountains Masked Laplacian Stack Image at 1m_30 Image at 1m_31 Image at 1m_32 Image at 1m_33 Image at 1m_34 Image at 1m_35 Image at 1m_36
Space Gaussian Stack Image at 2g_40 Image at 2g_41 Image at 2g_42 Image at 2g_43 Image at 2g_44 Image at 2g_45 Image at 2g_46
Space Laplacian Stack Image at 2l_50 Image at 2l_51 Image at 2l_52 Image at 2l_53 Image at 2l_54 Image at 2l_55 Image at 2l_56
Space Masked Laplacian Stack Image at 2m_60 Image at 2m_61 Image at 2m_62 Image at 2m_63 Image at 2m_64 Image at 2m_65 Image at 2m_66
Blended Laplacian Stacks Image at 2b_70 Image at 2b_71 Image at 2b_72 Image at 2b_73 Image at 2b_74 Image at 2b_75 Image at 2b_76
Level 0 Level 1 Level 2 Level 3 Level 4 Level 5 Level 6
Mask Gaussian Stack Image at m_00 Image at m_01 Image at m_02 Image at m_03 Image at m_04 Image at m_05 Image at m_06
Booker Gaussian Stack Image at 1g_10 Image at 1g_11 Image at 1g_12 Image at 1g_13 Image at 1g_14 Image at 1g_15 Image at 1g_16
Booker Laplacian Stack Image at 1l_20 Image at 1l_21 Image at 1l_22 Image at 1l_23 Image at 1l_24 Image at 1l_25 Image at 1l_26
Booker Masked Laplacian Stack Image at 1m_30 Image at 1m_31 Image at 1m_32 Image at 1m_33 Image at 1m_34 Image at 1m_35 Image at 1m_36
Lantern Gaussian Stack Image at 2g_40 Image at 2g_41 Image at 2g_42 Image at 2g_43 Image at 2g_44 Image at 2g_45 Image at 2g_46
Lantern Laplacian Stack Image at 2l_50 Image at 2l_51 Image at 2l_52 Image at 2l_53 Image at 2l_54 Image at 2l_55 Image at 2l_56
Lantern Masked Laplacian Stack Image at 2m_60 Image at 2m_61 Image at 2m_62 Image at 2m_63 Image at 2m_64 Image at 2m_65 Image at 2m_66
Lantern Laplacian Stacks Image at 2b_70 Image at 2b_71 Image at 2b_72 Image at 2b_73 Image at 2b_74 Image at 2b_75 Image at 2b_76