Home > OS >  convertTo() function in Python OpenCV
convertTo() function in Python OpenCV

Time:03-02

I want to decrease the contrast of image. I've found code from this website https://www.opencv-srf.com/2018/02/change-contrast-of-images-and-videos.html and using C . The code I want to ask is

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

or

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

What is convertTo() function in OpenCV python? I'm sorry, I really new in computer vision and OpenCV library

CodePudding user response:

since -1 would mean: keep the type 'as-is', it boils down to simple multiplication / subtraction:

img_lower_contrast = img * 0.5
img_lower_brightness = img - 20

CodePudding user response:

void cv::Mat::convertTo     (   OutputArray     m,
        int     rtype,
        double      alpha = 1,
        double      beta = 0 
    )       const

The method converts source pixel values to the target data type. saturate_cast<> is applied at the end to avoid possible overflows:

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

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

here -1 means the output array has same bit depth as the source. i.e., if the source is UINT8 then destination will also be UINT8. These operations convert the image data type to floats and then gives the output bitdepth same as src.

each pixel is multiplied by 0.5 which is effectively lowering the contrast.

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

In this case each pixel is multiplied by 1 and -20 is added. which is effectively decreasing the brightness.

  • Related