Image processing refers to the conversion of an image to a digital format followed by its manipulation using various algorithms. Through such techniques, we can blur, smooth, sharpen, enhance, restore, and do much more with images.
Note: Computer vision deals with performing complex tasks on media such as images and videos and interpreting results from the new found information.
Sharpening and smoothing images are classic image processing tasks and precisely what we'll learn in this Answer. Let's dive straight ahead!
Understanding the concept of sharpened and smooth images requires us to know what pixels are. Pixels are the most fundamental unit of an image that a machine can display. We can consider them as the building blocks of digital media.
Image filtering takes in the original pixels of an image as input. It modifies them according to the problem at hand and outputs a set of new pixels representing the altered image. The mechanism behind sharpening and smoothing images works similarly.
Sharpness refers to the clarity and prominence of edges within an image. The pixels are intensified in the image so that the edges become highlighted and more attention is paid to the details. The contrast between the pixels increases.
Smoothness refers to lesser pronounced noise and edginess within an image. The pixels are de-intensified here by focusing on reducing the noise or details of the image. This preserves the main content of the image, but the finer details are less focused on.
We will be implementing such a code that takes in an image and applies a smoothing and sharpening filter on the same image and finally displays it. You can find the explanation for the code below and can experiment with it too! Click "Run" to see the code in action.
import cv2 import numpy as np import matplotlib.pyplot as plt def main(): image_path = 'sample2.png' original_image = cv2.imread(image_path) smoothed_image = cv2.GaussianBlur(original_image.copy(), (5, 5), 0) kernel_sharpening = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]]) sharpened_image = cv2.filter2D(original_image.copy(), -1, kernel_sharpening) original_rgb = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB) smoothed_rgb = cv2.cvtColor(smoothed_image, cv2.COLOR_BGR2RGB) sharpened_rgb = cv2.cvtColor(sharpened_image, cv2.COLOR_BGR2RGB) aspect_ratio = original_image.shape[1] / original_image.shape[0] fig, axes = plt.subplots(1, 3, figsize=(15, 5)) for ax in axes: ax.set_aspect(aspect_ratio) axes[0].imshow(original_rgb) axes[0].set_title('Original Image') axes[0].axis('on') axes[0].set_facecolor('black') axes[1].imshow(smoothed_rgb) axes[1].set_title('Smoothed Image') axes[1].axis('on') axes[1].set_facecolor('black') axes[2].imshow(sharpened_rgb) axes[2].set_title('Sharpened Image') axes[2].axis('on') axes[2].set_facecolor('black') plt.show() if __name__ == "__main__": main()
Lines 1–3: We start by importing the necessary libraries for our code.
cv2
for image manipulation
numpy
as np
for numerical computations
matplotlib.pyplot
as plt
for creating plots
Line 5: We define the main function in which the processing will be carried out.
Lines 7–8: The variable image_path
holds the file path of the image "sample2.png." We read this image using the imread
function, and we store it in original_image
.
Line 10: Next, we apply a Gaussian blur to the original_image
through the cv2.GaussianBlur
function and save it in smoothed_image
. This helps to reduce noise and smooth out the image.
Line 12: We define an array called kernel_sharpening
that represents a kernel for image sharpening.
Line 13: Here, we apply the image sharpening kernel i.e. kernel_sharpening
to the original_image
using the cv2.filter2D
function and store it in sharpened_image
.
Lines 15–17: To visualize the images using Matplotlib, we convert the color space of each image from BGR to RGB and save our images in original_rgb
, smoothed_rgb
, and sharpened_rgb
.
Line 20: We calculate the aspect_ratio
of our image (width divided by height).
Line 22: Now, we create a 1 x 3 grid of subplots using plt.subplots
i.e. three subplots arranged horizontally. The figsize
argument sets the size of the figure to 15 units wide and 5 units high.
Lines 24–25: We set the aspect ratio of our plots to match the original_image
aspect ratio.
Lines 27–40: After a few plot customizations, our images are ready to be displayed.
Line 42: Finally, we display the entire figure with the three subplots using plt.show()
. We can see the original image, the smoothed image, and the sharpened image side by side and can compare them.
Lines 44–45: Our main
function is executed when we run the code.
Note: The aspect ratio helps to maintain the correct image proportions when displaying the images in the subplots, so that they are not distorted and appear in their original aspect ratio.
Feature extraction
Object detection
Segmentation
Classification
Noise reduction
Preprocessing of images
Object recognition
In certain cases, the removal of finer details can aid to object detection and feature extraction of the main focus of the image.
Conclusively, sharp and smooth images are crucial in computer vision tasks like image classification, object detection, segmentation, etc. They enable more accurate feature extraction and can alter images as needed, therefore, leading to improved results in computer vision applications.
Test your knowledge!
What does the function cv2.GaussianBlur
do to an image?
Free Resources