How to sharpen an image in Python using OpenCV

In this tutorial, we will see the concept of image sharpening and contrasting in Python using OpenCV. Here, we will be able to enhance and strengthen the edges of the image. We will also understand the importance of image sharpening in this tutorial. So, read this tutorial very carefully to understand all the concepts completely.

To sharpen an image in Python using OpenCV, you can use the cv2.filter2D() function. This function applies a linear filter to the image, which can be used to sharpen or blur the image.

Here’s an example of how you can sharpen an image in Python using OpenCV:

import cv2
import numpy as np

# Load the input image
image = cv2.imread('input.jpg')

# Create the sharpening kernel
kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])

# Apply the sharpening kernel to the image using filter2D
sharpened = cv2.filter2D(image, -1, kernel)

# Save the output image
cv2.imwrite('sharpened.jpg', sharpened)

The sharpening kernel used in this example is a simple 3×3 kernel that enhances the sharpness of the image by increasing the contrast between adjacent pixels.

You can also use other kernels to achieve different sharpening effects. For example, you can use a Laplacian kernel to sharpen the edges in the image, or a Gaussian kernel to blur the image.

Image Sharpening in Python

The sharpness of an image is the reverse process of the image blurring. It is the factor of two combinations.

  • Resolution: Resolution is the size, in pixels of the image. The more the pixels image has, the higher the resolution will be.
  • Acutance: Acutance measures the contrast at an edge. Edges that have more contrast appear to have more defined edges to the human vision.

Sharpness, therefore, defines the details in an image. Image sharpening is a technique for increasing the print sharpness of an image or strengthening or enhancing the edges of an image.

Importance

There are three major reasons to sharpen your image.

  • To reduce the blurriness of the images.
  • To highlight particular areas of the image.
  • To boost the legibility of the image.

Example

We will use the image below to sharpen it using OpenCv in Python programming.

Program

# import the two modules
import cv2
import numpy as np
# load path of the image
original_image=cv2.imread("C:/Users/kovid/Downloads/sample (2).jpg")
cv2.imshow('original image',original_image)
cv2.waitKey(0)
# create a sharpening kernel
sharpen_filter=np.array([[-1,-1,-1],
                 [-1,9,-1],
                [-1,-1,-1]])
# applying kernels to the input image to get the sharpened image
sharp_image=cv2.filter2D(original_image,-1,sharpen_filter)
cv2.imshow('Required image',sharp_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

This is the original image:
sharpen an image in Python using OpenCV

This is the image after sharpening it:
sharpen an image in Python using OpenCV

Leave a Reply

Your email address will not be published. Required fields are marked *