import matplotlib.pyplot as plt
import numpy as np
import cv2
# Generate some sample images
image = np.random.randint(0, 256, size=(7, 7), dtype=np.uint8)
print("Original Image:")[enter image description here][1]
print(image)
# Dictionary mapping normalization type constants to their names
norm_type_names = {
cv2.NORM_MINMAX: "NORM_MINMAX",
cv2.NORM_L1: "NORM_L1",
cv2.NORM_L2: "NORM_L2"
}
# Normalize the image using different normalization methods
norm_types = [cv2.NORM_MINMAX, cv2.NORM_L1, cv2.NORM_L2]
norm_images = []
for norm_type in norm_types:
norm_image = cv2.normalize(image, None, alpha=20, beta=50, norm_type=norm_type)
print(f'{norm_type_names[norm_type]}')
print(norm_image)
norm_images.append(norm_image)
print()
# Display the images with pixel values using Matplotlib
fig, axes = plt.subplots(1, len(norm_types), figsize=(10, 10))
for i, norm_image in enumerate(norm_images):
axes[i].imshow(norm_image, cmap='gray')
axes[i].set_title('{}'.format(norm_type_names[norm_types[i]]))
axes[i].axis('off')
# Display pixel values on the normalized images
for y in range(norm_image.shape[0]):
for x in range(norm_image.shape[1]):
axes[i].text(x, y, f'{norm_image[y, x]}', color='red', ha='center', va='center', fontsize=8)
plt.tight_layout()
plt.show()
Link to see the result
The code demonstrates the normalization of an image using different methods and visualizes the results alongside their pixel values.
This allows for a comparison of the effects of different normalization methods on the image data.
The visualization helps in understanding how the pixel values are transformed after normalization and provides insights into the distribution of pixel values in the image.
CV_8U
instead for thedtype
argument. “dtype
: when negative, the output array has the same type assrc
; otherwise, it has the same number of channels assrc
and the depth =CV_MAT_DEPTH(dtype)
.” Thedst
will always have the same number of channels as thesrc
. – Potluck