In the previous blog, we discussed Gaussian Blurring that uses Gaussian kernels for image smoothing. This is a low pass filtering technique that blocks high frequencies (like edges, noise, etc.). In this blog, we will see how we can use this Gaussian Blurring to highlight certain high-frequency parts in an image. Isn’t that interesting? So, let’s get started.
In Gaussian Blurring, we discussed how the standard deviation of the Gaussian affects the degree of smoothing. Roughly speaking, larger the standard deviation more will be the blurring or in other words more high frequency components will be suppressed.
Thus if we take 2 Gaussian kernels with different standard deviations, apply separately on the same image and subtract their corresponding responses, we will get an output that highlights certain high-frequency components based on the standard deviations used.
The logic is by blurring we remove some high-frequency components that represent noise, and by subtracting we remove some low-frequency components that correspond to the homogeneous areas in the image. All the remaining frequency components are assumed to be associated with the edges in the images. Thus, the Difference of Gaussian acts like a bandpass filter. Let’s take an example to understand this.
Suppose we have an image as shown below
Suppose we have 2 Gaussian kernels with standard deviation (σ1 > σ2). The kernel (with σ1), when convolved with an image, will blur the high-frequency components more as compared to the other kernel. Subtracting these, we can recover the information that lies between the frequency range which is not suppressed or blurred.
Now, that we saw how this works, let’s also discuss where this is useful(its pros and cons) as compared to other edge detection methods we have discussed.
All the edge detection kernels which we discussed till now are quite good in edge detection but one downside is that they are highly susceptible to noise. Thus if the image contains a high degree of noise, Difference of Gaussian is the way to go. This is because we are actually doing blurring which reduces the effect of noise to a great extent.
One downside of this method is that the edges are not enhanced much as compared to other methods. The output image formed has lower contrast.
OpenCV-Python
Let’s see how to do this using OpenCV-Python
1 2 3 4 5 6 7 8 9 |
import cv2 img = cv2.imread('D:/downloads/opencv_logo.PNG') # Apply 3x3 and 7x7 Gaussian blur low_sigma = cv2.GaussianBlur(img,(3,3),0) high_sigma = cv2.GaussianBlur(img,(5,5),0) # Calculate the DoG by subtracting dog = low_sigma - high_sigma |
Note: Using the linear separable property of the Gaussian kernel we can speed up the entire algorithm.
Hope you enjoy reading.
If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.