How to Sharpen an Image with OpenCV

A step by step guide to sharpening an image with OpenCV. Start sharpening images in minutes.

Updated March 19, 2023


Hey! If you love Computer Vision and AI, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

Welcome to the exciting world of OpenCV and computer vision! Today, we’re going to be exploring one of the most popular image processing techniques: sharpening an image. Sharpening an image is the process of increasing the contrast between adjacent pixels to enhance the details in an image, and it’s a simple but powerful technique that can be used to improve the quality of an image. In this tutorial, we’re going to show you how to sharpen an image with OpenCV.

Before we dive into the code, let’s take a moment to understand what sharpening an image is and why it’s useful.

What is Sharpening an Image?

Sharpening an image is the process of increasing the contrast between adjacent pixels to enhance the details in an image. This can be useful for a number of different image processing tasks, such as improving the clarity of an image, restoring details that have been lost due to blurring, or making an image appear more vivid and crisp.

Sharpening an image in OpenCV can be done using a simple convolution operation. A convolution operation involves taking a small region of the image and multiplying each pixel value by a corresponding weight in a filter matrix. The filter matrix is used to modify the pixel values in the image, and the result of the convolution operation is a new image with enhanced details.

Now that we have a basic understanding of sharpening an image, let’s see how we can do it in OpenCV.

The first step is to load an image into OpenCV. You can do this using the imread() function, like this:

import cv2
import numpy as np

# Load an image
img = cv2.imread('image.jpg')

Next, we’re going to create a filter matrix that will be used to sharpen the image. A simple way to sharpen an image is to use a filter matrix that subtracts a blurred version of the image from the original image. This can be done using the following filter matrix:

# Create a filter matrix
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])

In this example, the filter matrix is a 3x3 matrix that subtracts a blurred version of the image from the original image. The center value of the matrix (5) is used to increase the contrast between adjacent pixels, and the other values (-1) are used to reduce the intensity of pixels that are not on an edge.

Finally, we’re going to apply the filter matrix to the image using a convolution operation. To do this, we’ll use the filter2D() function, like this:

# Apply the filter matrix to the image
sharpened = cv2.filter2D(img, -1, kernel)

The first argument to the filter2D() function is the image we want to sharpen, and the second argument is the depth of the output image. The third argument is the filter matrix we created earlier. The filter2D() function applies the filter matrix to the image using a convolution operation, and the result is a new image with enhanced details that has been sharpened.

In conclusion, sharpening an image is a simple but powerful technique that can be used to improve the quality of an image. Whether you’re working with color images or black and white images, understanding how to sharpen an image with OpenCV will help you develop more effective computer vision applications. So go ahead and start exploring the world of OpenCV and computer vision today!