Normalize an Image in OpenCV Python
Last Updated :
07 May, 2024
Normalization involves adjusting the range of pixel intensity values in an image. Normalization can be beneficial for various purposes, such as improving the contrast or making the image more suitable for processing by other algorithms. In this article, we will explore how to normalize images using OpenCV in Python.
What is Image Normalization?
Image normalization is the process of adjusting the pixel intensity values of an image to a predefined range. This range is typically between 0 and 255 for images with 8-bit depth, where 0 represents black and 255 represents white. Normalization can be performed to improve the contrast of an image or to standardize the pixel values for further processing.
In OpenCV Python, the normalize() function from the cv2 module is used to normalize images. This function allows us to specify the desired range for the pixel intensity values.
Normalize an Image in OpenCV Python
Below are some of the examples by which we can understand about normalizing images in OpenCV Python:
Example 1: Normalizing Grayscale Image
In this example, a grayscale image is read and normalized to enhance contrast using the NORM_MINMAX normalization method. Both the original and normalized images are displayed using OpenCV imshow() function.
Python
import cv2
# Read grayscale image
image = cv2.imread('kl.png', cv2.IMREAD_GRAYSCALE)
# Normalize image
normalized_image = cv2.normalize(
image, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)
# Display original and normalized images
cv2.imshow('Original Image', image)
cv2.imshow('Normalized Image', normalized_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:

Example 2: Normalizing Color Image
In this example, a color image is converted to grayscale, then normalized to enhance contrast. The normalized grayscale image is converted back to color and displayed alongside the original image using OpenCV.
Python
import cv2
# Read color image
image = cv2.imread('kl.png')
# Convert image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Normalize grayscale image
normalized_gray_image = cv2.normalize(
gray_image, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)
# Convert normalized grayscale image back to color
normalized_color_image = cv2.cvtColor(
normalized_gray_image, cv2.COLOR_GRAY2BGR)
# Display original and normalized images
cv2.imshow('Original Image', image)
cv2.imshow('Normalized Image', normalized_color_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:

Similar Reads
Reading an image in OpenCV using Python Prerequisite: Basics of OpenCVIn this article, we'll try to open an image by using OpenCV (Open Source Computer Vision) library.  Following types of files are supported in OpenCV library:Windows bitmaps - *.bmp, *.dibJPEG files - *.jpeg, *.jpgPortable Network Graphics - *.png WebP - *.webp Sun raste
6 min read
PyQtGraph â Normalize Image in Image View In this article we will see how we can normalize image in the image view object in PyQTGraph. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics
4 min read
Convert OpenCV image to PIL image in Python OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human. When it is integrate
3 min read
How to Display an OpenCV image in Python with Matplotlib? The OpenCV module is an open-source computer vision and machine learning software library. It is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and vide
2 min read
Image Processing in Python Image processing involves analyzing and modifying digital images using computer algorithms. It is widely used in fields like computer vision, medical imaging, security and artificial intelligence. Python with its vast libraries simplifies image processing, making it a valuable tool for researchers a
7 min read