1 /**
2  * @file BasicLinearTransforms.cpp
3  * @brief Simple program to change contrast and brightness
4  * @author OpenCV team
5  */
6 
7 #include "opencv2/imgcodecs.hpp"
8 #include "opencv2/highgui/highgui.hpp"
9 #include <iostream>
10 
11 using namespace cv;
12 
13 double alpha; /**< Simple contrast control */
14 int beta;  /**< Simple brightness control */
15 
16 /**
17  * @function main
18  * @brief Main function
19  */
main(int,char ** argv)20 int main( int, char** argv )
21 {
22    /// Read image given by user
23    Mat image = imread( argv[1] );
24    Mat new_image = Mat::zeros( image.size(), image.type() );
25 
26    /// Initialize values
27    std::cout<<" Basic Linear Transforms "<<std::endl;
28    std::cout<<"-------------------------"<<std::endl;
29    std::cout<<"* Enter the alpha value [1.0-3.0]: ";std::cin>>alpha;
30    std::cout<<"* Enter the beta value [0-100]: "; std::cin>>beta;
31 
32 
33    /// Do the operation new_image(i,j) = alpha*image(i,j) + beta
34    /// Instead of these 'for' loops we could have used simply:
35    /// image.convertTo(new_image, -1, alpha, beta);
36    /// but we wanted to show you how to access the pixels :)
37    for( int y = 0; y < image.rows; y++ )
38       { for( int x = 0; x < image.cols; x++ )
39            { for( int c = 0; c < 3; c++ )
40                 {
41           new_image.at<Vec3b>(y,x)[c] = saturate_cast<uchar>( alpha*( image.at<Vec3b>(y,x)[c] ) + beta );
42                 }
43        }
44       }
45 
46    /// Create Windows
47    namedWindow("Original Image", 1);
48    namedWindow("New Image", 1);
49 
50    /// Show stuff
51    imshow("Original Image", image);
52    imshow("New Image", new_image);
53 
54 
55    /// Wait until user press some key
56    waitKey();
57    return 0;
58 }
59