How to Normalize an Image in OpenCV Python?
Image normalization is a process used for adjusting the contrast and brightness of an image. It enhances the details of the image. OpenCV is a widely used library for image processing. In this article, we will discuss how to normalize an image in OpenCV Python.
Steps to Normalize an Image in OpenCV Python
The following steps are involved in image normalization in OpenCV Python.
Step 1: Importing the libraries
OpenCV and Matplotlib libraries are required to normalize and show an image. Use the following commands to import the libraries.
#Importing the Libraries
import cv2
import matplotlib.pyplot as plt
Step 2: Loading the Image
Use the following commands to load the image.
#Reading the Image
img = cv2.imread('image.jpg')
Step 3: Converting the Image to Grayscale
Before normalizing an image, it is necessary to convert it to grayscale. Use the following command to convert the image to grayscale.
#Converting image to grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
Step 4: Normalizing the Image
Use the following command to normalize the image.
#Normalizing the Image
normalized_image = cv2.normalize(gray, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)
In the above command, the ‘normalize’ function is used for normalizing the image. In this function, ‘gray’ represents the grayscale image that needs to be normalized. ‘None’ is used to set the normalization output image size to the same size as the input image. ‘alpha’ and ‘beta’ values are used to set the minimum and maximum pixel values of the output image. ‘norm_type’ is used to set the normalization type.
Step 5: Displaying the Original and Normalized Images
Use the following commands to display the original and normalized images.
#Displaying the Original Image
plt.subplot(121)
plt.imshow(gray,cmap = 'gray')
plt.title('Original Image')
plt.xticks([])
plt.yticks([])
#Displaying the Normalized Image
plt.subplot(122)
plt.imshow(normalized_image,cmap = 'gray')
plt.title('Normalized Image')
plt.xticks([])
plt.yticks([])
plt.show()
In the above commands, ‘plt.subplot(121)’ is used to set the position of the first image. ‘plt.imshow(gray,cmap = ‘gray’)’ is used to display the original grayscale image with the ‘gray’ colormap. ‘plt.title(‘Original Image’)’ is used to set the title of the first image. ‘plt.xticks([])’ and ‘plt.yticks([])’ are used to hide the x and y-axis ticks.
The same steps are followed to display the normalized image.
Complete Code
#Importing the Libraries
import cv2
import matplotlib.pyplot as plt
#Reading the Image
img = cv2.imread('image.jpg')
#Converting image to grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#Normalizing the Image
normalized_image = cv2.normalize(gray, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)
#Displaying the Original and Normalized Images
plt.subplot(121)
plt.imshow(gray,cmap = 'gray')
plt.title('Original Image')
plt.xticks([])
plt.yticks([])
plt.subplot(122)
plt.imshow(normalized_image,cmap = 'gray')
plt.title('Normalized Image')
plt.xticks([])
plt.yticks([])
plt.show()
Conclusion
Image normalization is a process of enhancing the contrast and brightness of an image. In OpenCV Python, image normalization can be done using the ‘normalize’ function. Before normalizing the image, it is necessary to convert it to grayscale. A simple Python script was provided in this article to normalize an image in OpenCV Python.