opencv image pixel type conversion and normalization

opencv image pixel type conversion and normalization

1. Why convert and normalize image pixel types?
  • Data consistency: Different images may use different pixel types to represent image data, such as 8-bit unsigned integers, 16-bit unsigned integers, 32-bit floating point numbers, etc. In some cases, for subsequent processing or comparison with other images, it is necessary to ensure that all images use the same data type, so pixel type conversion is required to make their data types consistent.
  • Algorithm requirements: Some image processing algorithms have specific requirements for the data type of the input image. For example, some filters or transformations may require the input to be a floating point image, and therefore require converting the image pixel type from integer to floating point.
  • Enhance contrast: Normalization is mapping pixel values ​​to a specific range, usually [0, 1] or [0, 255]. This can be used to enhance the contrast of an image, making the brightness range easier to visualize or for subsequent processing.
  • Avoid overflow and truncation: When doing some image operations, pixel values ​​may fall outside a certain range (like 255 for an 8-bit unsigned integer image). Normalization ensures that pixel values ​​are within the valid range and avoid overflow or truncation.
  • Data processing and machine learning: In machine learning and deep learning, it is often necessary to standardize or normalize the input data so that the model can train and perform better. Normalization of image pixel values ​​can be part of this process.
  • Display and Visualization: Sometimes, normalizing image pixel values ​​to a suitable range can better display or visualize the image, making it easier to understand and analyze.
2. In OpenCV, convertTo() and normalize() are two commonly used image processing functions, used for image pixel type conversion and normalization;
  • The pixel type of the image is the data type of the cv::Mat element
  • The element type of cv::Mat determines the type of data it can store, such as 8-bit unsigned integer (CV_8U), 16-bit unsigned integer (CV_16U), 32-bit floating point number (CV_32F), etc. Different element types determine the color depth, accuracy, and range of expression of an image.
The (1)convertTo() function is used to convert the pixel type of an cv::Mat object to another type. Its basic usage is as follows:
void cv::Mat::convertTo(
    OutputArray m,
    int rtype,
    double alpha = 1,
    double beta = 0
) const;

参数解释:
m:输出的目标图像;
rtype:目标图像的数据类型(可以使用 OpenCV 提供的常量如CV_8U、CV_16U、CV_32F等);
alpha:缩放因子,用于线性变换。默认为1
beta:偏移量,用于线性变换。默认为0

Example:

cv::Mat img = cv::imread("input.jpg");
cv::Mat new_img;
img.convertTo(new_img, CV_32F); // 将图像转换为32位浮点数类型
The (2)normalize() function is used to normalize the pixel values ​​of the image to a specified range, usually [0, 1] or [0, 255].
void cv::normalize(
    InputArray src,  
    OutputArray dst,   
    double alpha = 0,  
    double beta = 255,    
    int norm_type = NORM_L2,   
    int dtype = -1,           
    InputArray mask = noArray()   
);

参数解释:
src:输入图像;
dst:输出归一化后的图像;
alpha:归一化的下界; 
beta:归一化的上界; 
norm_type:归一化类型,默认为NORM_L2(
	NORM_MINMAX
	NORM_INF
	NORM_L1
	NORM_L2
)
dtype:输出图像的数据类型(默认值-1,如果为负数,将使用输入图像的数据类型) ;
mask:掩码,可选参数; 

Example:

cv::Mat img = cv::imread("input.jpg", cv::IMREAD_GRAYSCALE);
cv::Mat normalized_img;
cv::normalize(img, normalized_img, 0, 255, cv::NORM_MINMAX, CV_8U);

在这个示例中,normalize() 函数将图像的像素值归一化到 [0, 255] 的范围内,并将结果保存在 normalized_img 中。

In summary, convertTo() is used to change the data type of the image, while normalize() is used to normalize the pixel values ​​to a specified range. These two functions are often used in image processing and can be selected according to specific needs.

3. Code example description:
(1) Image pixel type conversion:
#include <opencv2/opencv.hpp>
#include <iostream>

int main() {
    
    
    // 读取图像
    cv::Mat img = cv::imread("input.jpg");

    if (img.empty()) {
    
    
        std::cerr << "Could not read the image." << std::endl;
        return -1;
    }
    // 转换前的像素数据类型CV_8UC3
    std::cout << img.type() << std::endl;
    // 将图像从当前类型转换为新的类型CV_32FC3
    cv::Mat new_img;
    img.convertTo(new_img, CV_32F); // 这里将图像转换为32位浮点数类型,可以使用 CV_8U、CV_16U、CV_32F 等来选择不同的目标类型
    // 转换后的像素数据类型
    std::cout << new_img.type() << std::endl;

    // 进一步处理 new_img ...

    return 0;
}

(2) Image normalization: Map the value of image pixels to a specific range, usually [0, 1] or [0, 255].
convertTo() function

Normalize pixel values ​​to the [0, 1] range:

#include <opencv2/opencv.hpp>
#include <iostream>

int main() {
    
    
    // 读取灰度图像
    cv::Mat img = cv::imread("input.jpg", cv::IMREAD_GRAYSCALE);

    if (img.empty()) {
    
    
        std::cerr << "Could not read the image." << std::endl;
        return -1;
    }

    // 将像素值转换为浮点数类型并归一化
    cv::Mat normalized_img;
    img.convertTo(normalized_img, CV_32F);
    normalized_img /= 255.0;

    // 进一步处理 normalized_img ...

    return 0;
}

Normalize pixel values ​​to the [0, 255] range:

#include <opencv2/opencv.hpp>
#include <iostream>

int main() {
    
    
    // 读取灰度图像
    cv::Mat img = cv::imread("input.jpg", cv::IMREAD_GRAYSCALE);

    if (img.empty()) {
    
    
        std::cerr << "Could not read the image." << std::endl;
        return -1;
    }

    // 将像素值转换为整数类型并归一化
    cv::Mat normalized_img;
    img.convertTo(normalized_img, CV_8U);
    normalized_img *= 255;

    // 进一步处理 normalized_img ...

    return 0;
}

normalize() function

Normalize pixel values ​​to the [0, 1] range:

#include <opencv2/opencv.hpp>
#include <iostream>

int main() {
    
    
    // 读取灰度图像
    cv::Mat img = cv::imread("input.jpg", cv::IMREAD_GRAYSCALE);

    if (img.empty()) {
    
    
        std::cerr << "Could not read the image." << std::endl;
        return -1;
    }

    // 将像素值转换为浮点数类型并归一化
    cv::Mat normalized_img;
    cv::normalize(img, normalized_img, 0.0, 1.0, cv::NORM_MINMAX, CV_32F);

    // 进一步处理 normalized_img ...

    return 0;
}

Normalize pixel values ​​to the [0, 255] range:

#include <opencv2/opencv.hpp>
#include <iostream>

int main() {
    
    
    // 读取灰度图像
    cv::Mat img = cv::imread("input.jpg", cv::IMREAD_GRAYSCALE);

    if (img.empty()) {
    
    
        std::cerr << "Could not read the image." << std::endl;
        return -1;
    }

    // 将像素值归一化到[0, 255]范围
    cv::Mat normalized_img;
    cv::normalize(img, normalized_img, 0, 255, cv::NORM_MINMAX, CV_8U);

    // 进一步处理 normalized_img ...

    return 0;
}

convertTo()Both functions and normalize() can be used to normalize images, but their specific uses are slightly different:

  • convertTo()The function is used to convert the pixel type of an cv::Mat object to another type. It can be used to perform conversions between different types, including normalization. For example, you can convert an image from an 8-bit unsigned integer to a 32-bit floating point type, or you can map pixel values ​​into a specific range.
  • normalize()The function is designed to normalize the pixel values ​​of an image to a specified range, usually [0, 1] or [0, 255]. This is a normalization-specific function that is typically used in situations such as data preprocessing.

You can choose to use the two according to actual needs, but please pay attention to their different uses and parameters. If you only need to perform a simple normalization operation, then normalize() may be more intuitive and convenient. If more complex type conversions are required, including converting images from one type to another, then convertTo() may be more appropriate.

Guess you like

Origin blog.csdn.net/qq_33867131/article/details/132991294