?
Formula:
Two parameters \alpha > 0 and \beta are generally referred to as gain and offset parameters. We often use these two parameters to control the contrast and brightness separately.
?
- #include "stdafx.h"
- #include <iostream>
- #include <thread>
- #include <vector>
- #include <opencv2/core/core.hpp>
- #include <opencv2/contrib/contrib.hpp>
- #include <opencv2/highgui/highgui.hpp>
- #include <opencv2/imgproc/imgproc.hpp>
- #include <opencv2/objdetect/objdetect.hpp>
- ?
- using namespace CV;
- using namespace std;
- ?
- int g_slider_position = 0, g_slider_position2 = 0;
- Mat image;
- Mat New_image;
- double alpha, beta;
- ?
- void ontrackingbarslide (int POS)
- {
- ??? New_image = Mat::zeros (Image.size (), Image.type ());
- ??? beta = pos;
- ??? for (int y = 0; y < image.rows; y++)
- ??? {
- ?????? for (int x = 0; x < image.cols; × x + +)
- ?????? {
- ????????? for (int c = 0; c < 3; C + +)
- ????????? {
- ???????????? //saturate_cast prevent data overflow
- ???????????? New_image.at<vec3b> (y, x) [c] = saturate_cast<uchar> (alpha* (image.at<vec3b> (y, x) [C]) + beta);
- ?????????}
- ??????}
- ???}
- ?
- ??? Imshow ("New Image", new_image);
- }
- ?
- void onTrackingbarSlide2 (int POS)
- {
- ??? New_image = Mat::zeros (Image.size (), Image.type ());
- ?
- ??? Alpha = (double) pos/10.0;
- ?
- ??? for (int y = 0; y < image.rows; y++)
- ??? {
- ?????? for (int x = 0; x < image.cols; × x + +)
- ?????? {
- ????????? for (int c = 0; c < 3; C + +)
- ????????? {
- ???????????? //saturate_cast prevent data overflow
- ???????????? New_image.at<vec3b> (y, x) [c] = saturate_cast<uchar> (alpha* (image.at<vec3b> (y, x) [C]) + beta);
- ?????????}
- ??????}
- ???}
- ?
- ??? Imshow ("New Image", new_image);
- }
- ?
- int _tmain (int argc, _tchar* argv[])
- {
- ??? /// read into user-supplied images
- ??? Image = Imread ("e:\\myimage\\sql.png");
- ?
- ??? //initialized to 0 array
- ??? Mat new_image = Mat::zeros (Image.size (), Image.type ());
- ?
- ??? /// Initialize
- ??? cout << "* Enter the alpha value [1.0-3.0]: ";
- ??? CIN >> Alpha;
- ??? cout << "* Enter the beta value [0-100]: ";
- ??? CIN >> Beta;
- ?
- ??? // Create window
- ??? Namedwindow ("Original Image", 1); //1:window_autosize
- ??? Namedwindow ("New Image", 1);
- ?
- ??? Cvcreatetrackbar (" brightness (Gain)", "New Image", &g_slider_position, Ontrackingbarslide);
- ??? Cvcreatetrackbar (" contrast (offset)", "New Image", &g_slider_position2, OnTrackingbarSlide2);
- ?
- ??? ///Perform operation new_image (i,j) = Alpha*image (i,j) + Beta
- ??? for (int y = 0; y < image.rows; y++)
- ??? {
- ?????? for (int x = 0; x < image.cols; × x + +)
- ?????? {
- ????????? for (int c = 0; c < 3; C + +)
- ????????? {
- ???????????? //saturate_cast prevent data overflow
- ???????????? New_image.at<vec3b> (y, x) [c] = saturate_cast<uchar> (alpha* (image.at<vec3b> (y, x) [C]) + beta);
- ?????????}
- ??????}
- ???}
- ?
- ??? /// display image
- ??? Imshow ("Original image", image);
- ??? Imshow ("New Image", new_image);
- ?
- ??? /// wait for user keys
- ??? Waitkey ();
- ??? return 0;
- }
?
?
Reference:
Http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/core/basic_linear_transform/basic_linear_ Transform.html#basic-linear-transform
http://blog.csdn.net/mjlsuccess/article/details/12401839
OpenCV (5) Contrast and brightness