opencv Contrast and Brightness in C++


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


moutput matrix; if it does not have a proper size or type before the operation, it is reallocated
rtypedesired output matrix type or, rather, the depth since the number of channels are the same as the input has; if rtype is negative, the output matrix will have the same type as the input
alphaoptional scale factor. This changes the contrast of an image. Values below 1 decrease the contrast and above one increases the contrast
betaoptional delta added to the scaled values. Positive values increases the brightness and negative values decreases the brightnes



Contrast is the difference in luminance or colour that makes an object (or its representation in an image or display) distinguishable. The higher the difference between a pixel and its neighbors the higher the contrast is in that area.


In other words, brightness is the perception elicited by the luminance of a visual target. In terms of pixels, the higher the value of a pixel is the brighter that pixel is.

Contrast and Brightness adjustments:

g(i,j)= α.f(i,j)+β

f(x) as the source image pixels and g(x) as the output image pixels.

i and j indicates that the pixel is located in the i-th row and j-th column.

The parameters α > 0 and β are often called the gain and bias parameters; sometimes these parameters are said to control contrast and brightness respectively.

Opencv has a function called convertTo() which can apply these two operations.