Just like we normalize data points in machine learning as a pre-processing task, in the same way, we have to normalize pixels in the image for making images more appealing. To do so there is a function in OpenCV and it is cv2 normalize. In this entire tutorial, you will know how to normalize an image or picture and make it more appealing using the cv2.normalize() method.
Before going to the demonstration part first let’s know the syntax for the normalize() method.
cv2.normalize(source_array, destination_array, alpha, beta, normType )
source_array: It is the input image you want to normalize.
destination_array: The name for the output image after normalization.
alpha: norm value to normalize to or the lower range boundary in case of the range normalization.
beta : upper range boundary in case of the range normalization; it is not used for the norm normalization.
normType: Type for the normalization of the image.
The first and basic step is to import all the required libraries that I am implementing in this entire tutorial. I am only using NumPy and OpenCV python packages. Let’s import them.
import cv2
import numpy as np
The second step is to read the image I want to normalize. In OpenCV, you can read an image using the cv2.imread() method. The following image is what I am reading. Use the following lines of code to read it.
img = cv2.imread("bird_low_quality.jpg")
After reading the image let’s apply cv2.normalize() method. Execute the below lines of code.
Full Code
import cv2
import numpy as np
img = cv2.imread("bird_low_quality.jpg")
# print(img.shape)
norm = np.zeros((800,800))
norm_image = cv2.normalize(img,norm,0,255,cv2.NORM_MINMAX)
cv2.imshow("Low Quality Image",img)
cv2.imshow("Normalized Image",norm_image)
cv2.waitKey(0)
Explanation of the code.
Here you can see I am first reading the low-quality image using the imread() method. After that, for normalization, I am creating a blank image using the NumPy module np.zeroes((800,800)). After I am passing the input image, blank image, alpha, beta, and normalization type. In this example, I have used cv2.NORM_MINMAX.
To show the image you have to use imshow() method.
When you will run the code you will get the following output.
You can easily see the quality difference between the two images.
If you want to preprocess the image then normalization is a must for each image as it improves the intensity of the image and makes more clear. Thus it helps you to manipulate the image in a beautiful way. These are the steps to implement cv2. normalize() method in Python. I hope you have liked this tutorial. If you have any queries then you can contact us for more help.
Source: