Python OpenCV 中的 convertTo() 函数

convertTo() function in Python OpenCV

我想降低图像的对比度。我从这个网站 https://www.opencv-srf.com/2018/02/change-contrast-of-images-and-videos.html 找到了代码并使用了 C++。我要问的代码是

img.convertTo(img_lower_contrast, -1, 0.5, 0); //decrease the contrast (halve)

img.convertTo(img_lower_brightness, -1, 1, -20); //decrease the brightness by 20 for each pixel

什么是 OpenCV 中的 convertTo() 函数 python? 对不起,我对计算机视觉和 OpenCV 库很陌生

因为 -1 意味着:保持类型 'as-is',它归结为简单的乘法/减法:

img_lower_contrast = img * 0.5
img_lower_brightness = img - 20
void cv::Mat::convertTo     (   OutputArray     m,
        int     rtype,
        double      alpha = 1,
        double      beta = 0 
    )       const

该方法将源像素值转换为目标数据类型。 saturate_cast<> 在最后应用以避免可能的溢出:

m(x,y)=saturate_cast<rType>(α(∗this)(x,y)+β)

img.convertTo(img_lower_contrast, -1, 0.5, 0); //decrease the contrast (halve)

此处 -1 表示输出数组与源数组具有相同的位深度。即,如果源是 UINT8,那么目标也将是 UINT8。这些操作将图像数据类型转换为浮点数,然后给出与 src 相同的输出位深度。

每个像素乘以 0.5,这有效地降低了对比度。

img.convertTo(img_lower_brightness, -1, 1, -20); //decrease the brightness by 20 for each pixel

在这种情况下,每个像素都乘以 1,然后加上 -20。这有效地降低了亮度。