Popular understood that the image brightness is brightness level of the image, the digital image f (X, Y) = I (X, Y) R & lt (X, Y) , if the gray value between [0,255], the f value, the more the lower the luminance close to 0, F value is close to 255 higher luminance. And we want to distinguish between brightness and contrast, as mentioned above refers to the contrast between the highest and lowest gradation gray level difference.
Saturation refers to the number of kinds of colors an image, the above-mentioned image gray level is [Lmin, Lmax], then in the more intermediate value Lmin, Lmax, the type of image would represent multiple colors, it is even more saturation high, the appearance will appear more vivid image, may adjust the saturation or overexposure is not sufficiently corrected exposure image. Making the image look more natural
Converting the digital image, the original pixel gray level is set F (I, J) , the pixel gray into G (I, J) , the linear transformation is commonly g (i, j) = af (i, j ) + b , where a coefficient of influence on the image contrast, image brightness influence coefficient b, as follows:
(1) is a picture when a = 1;
(2) a> 1 when the contrast enhancement, the image appears more clearly;
( 3) a <1 when the contrast reduction, the image appears to darken;
(. 4) B affect the brightness of the image, with the increase of b (b> a, the entire image gradation values (b> 0) b 0) and reduced shift up or down, that is, the entire image becomes bright or dark, will not change the contrast of the image
Common Code:
In opencv image data is stored in Mat type of data, we know that there is a pixel rgb constitution, it is Mat is a three-dimensional array, it is simple to obtain mat image pixels.
//三个for循环,执行运算 new_image(i,j) =a*image(i,j) + b for(int y = 0; y < image.rows; y++ ) { for(int x = 0; x < image.cols; x++ ) { for(int c = 0; c < 3; c++ ) { new_image.at<Vec3b>(y,x)[c]= saturate_cast<uchar>( (g_nContrastValue*0.01)*(image.at<Vec3b>(y,x)[c] ) + g_nBrightValue ); } } }
The above code image.at <Vec3b> (y, x ) [c] where, y is the row of pixels where, x is the pixel column where, c is an R, G, B (corresponding to 0, 1) wherein the One.
saturate_cast For security conversion operation result may exceed the pixel value range (overflow), it may also be a non-integer (if it is floating), with the results converted saturate_cast, to ensure it is a valid value.
code show as below:
// ----------------------------------- [section] headers contain ------- -------------------------------- // description: contains the program file header depends // ----- -------------------------------------------------- --------------------------------------- #include <opencv2 / Core / core.hpp> #include <opencv2 / HighGUI / highgui.hpp> #include " opencv2 / imgproc / imgproc.hpp " #include <the iostream> // --------------------- -------------- [section] ------------------------------ namespace declaration --------- // description: namespace contains the procedures used // ---------------------------- -------------------------------------------------- ----------------- a using namespace std; a using namespace CV; // ----------------------------------- [global] function declaration part - ------------------------------------- // description: global function declaration // ---- -------------------------------------------------- ----------------------------------------- static void ContrastAndBright ( int , void * ) ; // ----------------------------------- [section] ------ global variable declaration -------------------------------- // description: The global variable declaration // --------- -------------------------------------------------- ------------------------------------ int g_nContrastValue; // contrast value int g_nBrightValue; // luminance value Mat g_srcImage, g_dstImage; // ----------------------------------- [ main () function] -------------------------------------------- / / description: entry function console application, our program starts here // ------------------------------- -------------------------------------------------- -------------- int main () { // change the foreground and background colors console System ( " color. 2F " ); // read image of a user supplied g_srcImage imread = ( " E: \\ \\ vs2015 VS2015Opencv Project \\ \\ \\ cat.jpg Picture " ); IF (g_srcImage.data) {printf (!" Read error image g_srcImage ~ \ n-! " ); Return to false ;} g_dstImage = Mat :: zeros (g_srcImage.size (), g_srcImage.type ()); // set the initial value of the contrast and brightness g_nContrastValue = 80 ; g_nBrightValue = 80 ; // Create window namedWindow ( " [effects] FIG windows " , 1 ); // Create a track bar createTrackbar ( " contrast: " , " [effects] FIG window " , & g_nContrastValue, 300 , ContrastAndBright); createTrackbar ( "Brightness: " , " [Effects] FIG Window " , & g_nBrightValue, 200 is , ContrastAndBright); // call the callback function ContrastAndBright (g_nContrastValue, 0 ); ContrastAndBright (g_nBrightValue, 0 ); // output some help information cout << endl << " \ t runs successfully, the scroll bar to adjust image effects observed \ the n-\ the n- " << " \ t press the" q "key, the program exits \ the n- " ; // when you press the "q" key, the program exits while ( char (waitKey ( . 1 ))! = ' Q ') {} Return 0 0; } // ----------------------------- [ContrastAndBright () function] ----------- ------------------------- // description: callback function changes the image contrast and brightness values // ---------- -------------------------------------------------- ----------------------------------- static void ContrastAndBright ( int , void * ) { // create the window namedWindow ( " [original] FIG window " , 1 ); // three loop for performing arithmetic g_dstImage (I, J) = A * g_srcImage (I, J) + B for ( int Y = ; Y <g_srcImage.rows; ++ Y ) { for ( int X = 0 ; X <g_srcImage.cols; X ++ ) { for ( int C = 0 ; C < . 3 ; C ++ ) { g_dstImage.at <Vec3b> (Y, X) [C] = saturate_cast <UCHAR> ( (g_nContrastValue * 0.01 ) * (g_srcImage.at <Vec3b> (Y, X) [C]) + g_nBrightValue); } } } // display image imshow ( " [original] FIG window " , g_srcImage); imshow ( "[Effect] FIG Window " , g_dstImage); }
Renderings: