What does cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC1);
Asked Answered
G

3

71

What does the cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC1); do in OpenCV?

I went through the documentation and was unable to understand what alpha, beta, NORM_MINMAX and CV_8UC1 actually do. I am aware alpha sets the lower and beta the higher bound. CV_8UC1 stands for an 8-bit unsigned single channel. But what exactly these arguments do to the picture is what I am unable to comprehend.

Gentlefolk answered 19/8, 2012 at 3:19 Comment(1)
Use CV_8U instead for the dtype argument.dtype: when negative, the output array has the same type as src; otherwise, it has the same number of channels as src and the depth = CV_MAT_DEPTH(dtype).” The dst will always have the same number of channels as the src.Potluck
B
78

When the normType is NORM_MINMAX, cv::normalize normalizes _src in such a way that the min value of dst is alpha and max value of dst is beta. cv::normalize does its magic using only scales and shifts (i.e. adding constants and multiplying by constants).

CV_8UC1 says how many channels dst has.

The documentation here is pretty clear: http://docs.opencv.org/modules/core/doc/operations_on_arrays.html#normalize

Bevins answered 19/8, 2012 at 3:40 Comment(11)
What do alpha and beta mean in the image. As far as I know alpha stood for minimum range and beta for max.So if I am converting to grayscale shouldnt alpha be 0 and beta be 1 if I am using a single channel?Gentlefolk
alpha and beta are the highest and lowest value in the dst image, respectively.Bevins
So when I am using a single channel it should ideally be 0 to 1 right? Or am I mistaken here?Gentlefolk
0 to 255 are the physical limits of your array: you can't store anything above 255 or below 0. What values to use it depends on what limits you need: if you use 5 and 20 there won't be anything below 5 or above 20.Bevins
But single channels simply use 0 and 1 right? So the 0 and 255 are kind of redundant?Gentlefolk
No... single channel use one channel but the signal is between 0 and 255.Bevins
OKay. I guess now I need to read up more on channels and what they are. Thanks for the help!Gentlefolk
can you use this function to normalize a set of images best on mean and std of all images not just a single image?Beulabeulah
@Bevins alpha and beta are the highest and lowest value in the dst image, respectively. isn't it the reverse; alpha is lower limit so it is the minimum and beta is upper limit so it is the maximum in the dst?Dilemma
0 to 255 limits are related to CV_8UC1, it is different for other typesMalaspina
dtype: when negative, the output array has the same type as src; otherwise, it has the same number of channels as src and the depth = CV_MAT_DEPTH(dtype).” No, you shouldn’t specify how many channels dst has, because it is always the same with the src. Use CV_8U instead.Potluck
N
0

There are several types of normalization in Opencv.

Try running samples in the Online OpenCV-Flow tool to better understand each type.

Normalize Sample

Nutt answered 22/1, 2023 at 20:34 Comment(0)
S
0
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.

Stalder answered 9/5 at 14:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.