Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to normalize an image in OpenCV Python?
Image normalization in OpenCV rescales pixel values to a specific range, improving image processing and machine learning model performance. The cv2.normalize() function provides various normalization techniques to transform pixel intensities.
Syntax
The cv2.normalize() function accepts the following parameters ?
cv2.normalize(src, dst, alpha, beta, norm_type, dtype, mask)
Parameters
src Input image array
dst Output array of the same size as src
alpha Lower norm value for range normalization
beta Upper norm value for range normalization
norm_type Normalization type (NORM_MINMAX, NORM_L2, etc.)
dtype Data type of output array
mask Optional operation mask
Example 1: Grayscale Image Normalization
This example normalizes a grayscale image to the range [0,1] using min-max normalization ?
import cv2
import numpy as np
# Create a sample grayscale image with different intensity values
img = np.array([[37, 45, 55, 120],
[80, 90, 100, 150],
[200, 220, 240, 255]], dtype=np.uint8)
print("Image data before Normalize:")
print(img)
print("Min value:", img.min(), "Max value:", img.max())
# Normalize the image to range [0, 1]
img_normalized = cv2.normalize(img, None, 0, 1.0, cv2.NORM_MINMAX, dtype=cv2.CV_32F)
print("\nImage data after Normalize:")
print(img_normalized)
print("Min value:", img_normalized.min(), "Max value:", img_normalized.max())
Image data before Normalize: [[ 37 45 55 120] [ 80 90 100 150] [200 220 240 255]] Min value: 37 Max value: 255 Image data after Normalize: [[0. 0.03669725 0.08256881 0.38073394] [0.19724771 0.24311927 0.28899083 0.51834863] [0.74770642 0.8440367 0.93119266 1. ]] Min value: 0.0 Max value: 1.0
Example 2: Binary Image Normalization
This example creates a binary image and normalizes it to [0,1] range ?
import cv2
import numpy as np
# Create a sample grayscale image
img = np.array([[37, 45, 155, 120],
[80, 190, 100, 150],
[200, 220, 40, 255]], dtype=np.uint8)
print("Original image data:")
print(img)
# Apply threshold to create a binary image
ret, thresh = cv2.threshold(img, 140, 255, cv2.THRESH_BINARY)
print("\nImage data after Thresholding:")
print(thresh)
# Normalize the binary image
img_normalized = cv2.normalize(thresh, None, 0, 1.0, cv2.NORM_MINMAX, dtype=cv2.CV_32F)
print("\nImage data after Normalize:")
print(img_normalized)
Original image data: [[ 37 45 155 120] [ 80 190 100 150] [200 220 40 255]] Image data after Thresholding: [[ 0 0 255 0] [ 0 255 0 255] [255 255 0 255]] Image data after Normalize: [[0. 0. 1. 0.] [0. 1. 0. 1.] [1. 1. 0. 1.]]
Normalization Types
OpenCV provides different normalization types ?
| Type | Description | Use Case |
|---|---|---|
| NORM_MINMAX | Scales to [alpha, beta] range | General normalization |
| NORM_L1 | L1 norm (sum of absolute values) | Feature normalization |
| NORM_L2 | L2 norm (Euclidean length) | Vector normalization |
| NORM_INF | Infinity norm (maximum absolute value) | Maximum scaling |
Example 3: Different Normalization Types
import cv2
import numpy as np
# Create sample data
data = np.array([3, 4, 5], dtype=np.float32)
print("Original data:", data)
# L2 normalization
l2_norm = cv2.normalize(data, None, 1.0, 0, cv2.NORM_L2)
print("L2 normalized:", l2_norm)
# L1 normalization
l1_norm = cv2.normalize(data, None, 1.0, 0, cv2.NORM_L1)
print("L1 normalized:", l1_norm)
# Min-Max normalization to [0, 255]
minmax_norm = cv2.normalize(data, None, 0, 255, cv2.NORM_MINMAX)
print("Min-Max normalized:", minmax_norm)
Original data: [3. 4. 5.] L2 normalized: [0.42426407 0.5656854 0.7071068 ] L1 normalized: [0.25 0.33333334 0.41666667] Min-Max normalized: [ 0. 127.5 255. ]
Conclusion
Use cv2.normalize() with NORM_MINMAX for scaling images to specific ranges. Choose L1/L2 normalization for feature vectors in machine learning applications.
