1 #include "opencv2/core/utility.hpp"
2 #include "opencv2/imgproc.hpp"
3 #include "opencv2/imgcodecs.hpp"
4 #include "opencv2/highgui.hpp"
11 int _brightness = 100;
16 /* brightness/contrast callback function */
17 static void updateBrightnessContrast( int /*arg*/, void* )
20 int brightness = _brightness - 100;
21 int contrast = _contrast - 100;
24 * The algorithm is by Werner D. Streidt
25 * (http://visca.com/ffactory/archives/5-99/msg00021.html)
30 double delta = 127.*contrast/100;
31 a = 255./(255. - delta*2);
32 b = a*(brightness - delta);
36 double delta = -128.*contrast/100;
37 a = (256.-delta*2)/255.;
38 b = a*brightness + delta;
42 image.convertTo(dst, CV_8U, a, b);
45 calcHist(&dst, 1, 0, Mat(), hist, 1, &histSize, 0);
46 Mat histImage = Mat::ones(200, 320, CV_8U)*255;
48 normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, CV_32F);
50 histImage = Scalar::all(255);
51 int binW = cvRound((double)histImage.cols/histSize);
53 for( int i = 0; i < histSize; i++ )
54 rectangle( histImage, Point(i*binW, histImage.rows),
55 Point((i+1)*binW, histImage.rows - cvRound(hist.at<float>(i))),
56 Scalar::all(0), -1, 8, 0 );
57 imshow("histogram", histImage);
61 std::cout << "\nThis program demonstrates the use of calcHist() -- histogram creation.\n"
62 << "Usage: \n" << "demhist [image_name -- Defaults to baboon.jpg]" << std::endl;
67 "{@image|baboon.jpg|input image file}"
70 int main( int argc, const char** argv )
74 CommandLineParser parser(argc, argv, keys);
75 string inputImage = parser.get<string>(0);
77 // Load the source image. HighGUI use.
78 image = imread( inputImage, 0 );
81 std::cerr << "Cannot read image file: " << inputImage << std::endl;
85 namedWindow("image", 0);
86 namedWindow("histogram", 0);
88 createTrackbar("brightness", "image", &_brightness, 200, updateBrightnessContrast);
89 createTrackbar("contrast", "image", &_contrast, 200, updateBrightnessContrast);
91 updateBrightnessContrast(0, 0);