Sharpening An Image using OpenCV Library in Python
This article was published as a part of the Data Science Blogathon
From our previous article, we have learned about how to blur an image using a kernel, and we have also learned exactly what a kernel is- It simply refers to the matrix involved in the image manipulation process. For the task of blurring an image, we created a kernel to average the pixel values. At this point in our OpenCV tutorial, we have obtained a good understanding of the OpenCV package in the Python programming language.
If you have not read through my previous articles and would like to do so, kindly navigate to the following hyperlinks:
- Article 1: An Introduction to Computer Vision With OpenCV.
- Article 2: Performing Computer Vision Task With OpenCV And Python.
- Article 3: Some Advanced OpenCV Operations For Your Computer vision Project.
- Article 4: Some Advanced OpenCV Functions For Computer Vision Project Continued.
- Article 5: Beginner’s Guide to Python OpenCV Operation: Rotation.
- Article 6: Advanced OpenCV and NumPy Operations: Cropping, Copying, And Pasting.
- Article 7: Advanced OpenCV: Blurring An Image using the Renowned OpenCV Library.
We will now look at the process of sharpening an image, we will make use of a kernel to highlight each particular pixel and enhance the color that it emits. It is very similar to the process of blurring, except that now, instead of creating a kernel to average each pixel intensity, we are creating a kernel that will cause the pixel intensities to be higher and therefore more prominent to the human eye.
To facilitate this learning experience we shall make use of the same image as in our previous article. The image may be downloaded from this link or saved from below.
Understanding The Backend Of The Process.
It is good to know that kernels are used for the process of blurring images, sharpening images, embossing, edge detection, and much more. A kernel is known by other names such as:
- Convolution Matrix.
The process of blurring, sharpening, embossing, edge detection, and others, require that a kernel be applied to the image pixels, which is also why this process is also referred to as Convolution- i.e, the process during which the kernel is applied to the image.
There is a fixed/standard general formula for convolutions (blurring, sharpening, etc). This formula is as follows:
The crucial point to know when you are working with image processing tasks is that the formula does not differ greatly, and the type of kernel that you use affects the operation you are performing on the image. Because it is the kernel that brings about a change in pixel formation and intensity when multiplied with the pixels in the original image.
So essentially what I am saying is that multiplying by a particular kernel (array of values) will bring about a specific change in the image presentation.
A few of the commonly used kernels are as follows:
I highly recommend that you visit this Wikipedia page to obtain a higher level of understanding and insight into the discussion.
Now that we understand how kernels function, we may proceed to sharpen an image using the Python Programming Language.
To sharpen an image in Python, we are required to make use of the filter2D() method. This method takes in several arguments, 3 of which are very important. The arguments to be passed in are as follows:
- src: This is the source image, i.e., the image that will undergo sharpening.
- ddepth: This is an integer value representing the expected depth of the output image. We will set this value to be equal to -1. In doing this, we tell the compiler that the output image will have the same depth as the input image.
- kernel: This is the kernel we will use to conduct our desired process. Since we wish to sharpen an image our kernel will be as follows:
Before we attempt to sharpen our image as follows, we must first import the necessary packages into our script:
# load the required packages import cv2 import numpy as np
Next, we will proceed to load the image into our system memory. We will load the image in standard, i.e., Color format.
# load the image into system memory image = cv2.imread('C:/Users/Shivek/Pictures/images.jpg', flags=cv2.IMREAD_COLOR)
We will thereafter display the contents of the variable storing the image, to the screen:
# display the image to the screen cv2.imshow('AV CV- Winter Wonder', image) cv2.waitKey() cv2.destroyAllWindows()
By this point in our OpenCV learning experience, we should be familiar with the explanation of the above code blocks. Essentially what we have done is as follows:
- We have imported the necessary packages into our python script.
- We have loaded the image into system memory using the imread() method while passing the file name/path, and the appropriate color flag.
- We proceeded to utilize the imshow() method to display the image window, which will be terminated upon user action.
Output to the above code will be seen as follows:
Below is the Python code we will use for sharpening the image:
kernel = np.array([[0, -1, 0], [-1, 5,-1], [0, -1, 0]]) image_sharp = cv2.filter2D(src=image, ddepth=-1, kernel=kernel) cv2.imshow('AV CV- Winter Wonder Sharpened', image_sharp) cv2.waitKey() cv2.destroyAllWindows()
Output to the above code block will be seen as follows:
And immediately, one can see that the glare, and luminance of our sharpened image, are much more noticeable and striking to the eye, than the original image. If one looks closely at the image, one will notice that the edges/outlines of objects in the image have been highlighted and made to look more prominent.
Thus we have successfully sharpened an image using the OpenCV package in Python Programming Language. I do hope that you enjoyed reading through this article, and have new takeaways of OpenCV Operations in Python.
Thank you for your time.