To change the brightness and contrast of an image:
Formula:
Code:
1#include <opencv2\opencv.hpp>2#include <iostream>3#include <string>4 5 #pragmaComment (linker, "/subsystem:\" windows\ "/entry:\" Maincrtstartup\ "")6 7 using namespacestd;8 using namespaceCV;9 Ten voidShow (std::stringname,mat img) One { A Namedwindow (name, cv_window_autosize); - imshow (Name, IMG); - } the - intMain () - { -Mat img = Imread ("Lena.png"); +Mat new_img =Mat::zeros (Img.size (), Img.type ()); - + DoubleAlpha =2.2; A intBeta = -; at for(inty =0; Y < img.rows; ++y) - { - for(intx =0; x < Img.cols; ++x) - { - for(intc =0; C <3; ++c) - { inNew_img.at<vec3b> (y, x) [c] = static_cast<uchar> (alpha* (img.at<vec3b> (y, x) [C]) +beta); - } to } + } -Show ("Re", IMG); theShow ("Bright", new_img); *Imwrite ("./after/bright.png", new_img); $ Waitkey ();Panax Notoginseng return 0; -}
Results:
(I don't know why my results are not the same as the examples, and I might have calculated errors)
Where we are using
New_img.at<vec3b> (y, x) [C]
Get the value of the channel, use Saturate_cast to do data overflow protection;
Alternatively, you can convert directly using a simpler function:
1#include <opencv2\opencv.hpp>2#include <iostream>3#include <string>4 5 #pragmaComment (linker, "/subsystem:\" windows\ "/entry:\" Maincrtstartup\ "")6 7 using namespacestd;8 using namespaceCV;9 Ten voidShow (std::stringname,mat img) One { A Namedwindow (name, cv_window_autosize); - imshow (Name, IMG); - } the - intMain () - { -Mat img = Imread ("Lena.png"); +Mat new_img =Mat::zeros (Img.size (), Img.type ()); - DoubleAlpha =2.2; + intBeta = -; AImg.convertto (New_img,-1, alpha, beta); atShow (" After", new_img); - Waitkey (); - return 0; -}
At this point the result is similar to the example:
OPENCV Official Document Learning record (3)