Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/cpp/demhist.cpp
16337 views
1
#include "opencv2/core/utility.hpp"
2
#include "opencv2/imgproc.hpp"
3
#include "opencv2/imgcodecs.hpp"
4
#include "opencv2/highgui.hpp"
5
6
#include <iostream>
7
8
using namespace cv;
9
using namespace std;
10
11
int _brightness = 100;
12
int _contrast = 100;
13
14
Mat image;
15
16
/* brightness/contrast callback function */
17
static void updateBrightnessContrast( int /*arg*/, void* )
18
{
19
int histSize = 64;
20
int brightness = _brightness - 100;
21
int contrast = _contrast - 100;
22
23
/*
24
* The algorithm is by Werner D. Streidt
25
* (http://visca.com/ffactory/archives/5-99/msg00021.html)
26
*/
27
double a, b;
28
if( contrast > 0 )
29
{
30
double delta = 127.*contrast/100;
31
a = 255./(255. - delta*2);
32
b = a*(brightness - delta);
33
}
34
else
35
{
36
double delta = -128.*contrast/100;
37
a = (256.-delta*2)/255.;
38
b = a*brightness + delta;
39
}
40
41
Mat dst, hist;
42
image.convertTo(dst, CV_8U, a, b);
43
imshow("image", dst);
44
45
calcHist(&dst, 1, 0, Mat(), hist, 1, &histSize, 0);
46
Mat histImage = Mat::ones(200, 320, CV_8U)*255;
47
48
normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, CV_32F);
49
50
histImage = Scalar::all(255);
51
int binW = cvRound((double)histImage.cols/histSize);
52
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);
58
}
59
static void help()
60
{
61
std::cout << "\nThis program demonstrates the use of calcHist() -- histogram creation.\n"
62
<< "Usage: \n" << "demhist [image_name -- Defaults to ../data/baboon.jpg]" << std::endl;
63
}
64
65
const char* keys =
66
{
67
"{help h||}{@image|../data/baboon.jpg|input image file}"
68
};
69
70
int main( int argc, const char** argv )
71
{
72
CommandLineParser parser(argc, argv, keys);
73
if (parser.has("help"))
74
{
75
help();
76
return 0;
77
}
78
string inputImage = parser.get<string>(0);
79
80
// Load the source image. HighGUI use.
81
image = imread( inputImage, 0 );
82
if(image.empty())
83
{
84
std::cerr << "Cannot read image file: " << inputImage << std::endl;
85
return -1;
86
}
87
88
namedWindow("image", 0);
89
namedWindow("histogram", 0);
90
91
createTrackbar("brightness", "image", &_brightness, 200, updateBrightnessContrast);
92
createTrackbar("contrast", "image", &_contrast, 200, updateBrightnessContrast);
93
94
updateBrightnessContrast(0, 0);
95
waitKey();
96
97
return 0;
98
}
99
100