1 /**
2  * @file LinearTransforms.cpp
3  * @brief Simple program to change contrast and brightness
4  * @date Mon, June 6, 2011
5  * @author OpenCV team
6  */
7 
8 #include "opencv2/imgcodecs.hpp"
9 #include "opencv2/highgui/highgui.hpp"
10 
11 using namespace cv;
12 
13 /** Global Variables */
14 const int alpha_max = 5;
15 const int beta_max = 125;
16 int alpha; /**< Simple contrast control */
17 int beta;  /**< Simple brightness control*/
18 
19 /** Matrices to store images */
20 Mat image;
21 
22 /**
23  * @function on_trackbar
24  * @brief Called whenever any of alpha or beta changes
25  */
on_trackbar(int,void *)26 static void on_trackbar( int, void* )
27 {
28     Mat new_image = Mat::zeros( image.size(), image.type() );
29 
30     for( int y = 0; y < image.rows; y++ )
31         for( int x = 0; x < image.cols; x++ )
32             for( int c = 0; c < 3; c++ )
33                 new_image.at<Vec3b>(y,x)[c] = saturate_cast<uchar>( alpha*( image.at<Vec3b>(y,x)[c] ) + beta );
34 
35     imshow("New Image", new_image);
36 }
37 
38 
39 /**
40  * @function main
41  * @brief Main function
42  */
main(int,char ** argv)43 int main( int, char** argv )
44 {
45    /// Read image given by user
46    image = imread( argv[1] );
47 
48    /// Initialize values
49    alpha = 1;
50    beta = 0;
51 
52    /// Create Windows
53    namedWindow("Original Image", 1);
54    namedWindow("New Image", 1);
55 
56    /// Create Trackbars
57    createTrackbar( "Contrast Trackbar", "New Image", &alpha, alpha_max, on_trackbar );
58    createTrackbar( "Brightness Trackbar", "New Image", &beta, beta_max, on_trackbar );
59 
60    /// Show some stuff
61    imshow("Original Image", image);
62    imshow("New Image", image);
63 
64    /// Wait until user press some key
65    waitKey();
66    return 0;
67 }
68