In this blog, we will learn how we can sharpen an image or perform edge enhancement using a smoothing filter. Let’s see how this is done
- First, we blur the image. We know by smoothing an image we suppress most of the high-frequency components.
- Then, we subtract this smoothed image from the original image(the resulting difference is known as a mask). Thus, the output image will have most of the high-frequency components that are blocked by the smoothing filter.
- Adding this mask back to the original will enhance the high-frequency components.
Because we are using a blurred or unsharp image to create a mask this technique is known as Unsharp Masking.
Thus, unsharp masking first produces a mask m(x,y) as
where, f(x,y) is the original image and fb(x,y) is the blurred version of the original image.
Then this mask is added back to the original image which results in enhancing the high-frequency components.
where k specifies what portion of the mask to be added. When k= 1 this is known as Unsharp masking. For k>1 we call this as high-boost filtering because we are boosting the high-frequency components by giving more weight to the masked (edge) image.
We can also write the above two equations into one as the weighted average of the original and the blurred image.
Note: Instead of subtracting the blurred image from the original, we can directly use a negative Laplacian filter to obtain the mask.
If the image contains noise, this method will not produce satisfactory results, like most of the other sharpening filters.
Let’s see how to do this using OpenCV-Python
OpenCV-Python
Since in the last equation we described unsharp masking as the weighted average of the original and the input image, we will simply use OpenCV cv2.addWeighted() function.
1 2 3 4 5 6 7 8 |
import cv2 # Load the image image = cv2.imread("D:/downloads/kang.jpg") # Blur the image gauss = cv2.GaussianBlur(image, (7,7), 0) # Apply Unsharp masking unsharp_image = cv2.addWeighted(image, 2, gauss, -1, 0) |
The output is shown below
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.