OpenCV4入门系列教程73:图像直方图动态调整参数均衡化

入门系列索引:OpenCV入门系列教程索引目录

上一篇:OpenCV4入门系列教程72:图像直方图均衡化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//--------------------------------------【程序说明】-------------------------------------------
// 程序说明:《OpenCV3编程入门》OpenCV2版书本附赠示例程序10
// 程序描述:用滑动条控制直方图
// 2014年11月 Revised by @浅墨_毛星云
//------------------------------------------------------------------------------------------------

//---------------------------------【头文件、命名空间包含部分】----------------------------
// 描述:包含程序所使用的头文件和命名空间
//------------------------------------------------------------------------------------------------
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;

//-----------------------------------【全局变量声明部分】---------------------------------------
// 描述:声明全局变量
//---------------------------------------------------------------------------------------------------
int _brightness = 100;
int _contrast = 100;
Mat image;

/* brightness/contrast callback function */
static void updateBrightnessContrast(int /*arg*/, void *) {
int histSize = 64;
int brightness = _brightness - 100;
int contrast = _contrast - 100;

/*
* The algorithm is by Werner D. Streidt
* (http://visca.com/ffactory/archives/5-99/msg00021.html)
*/
double a, b;
if (contrast > 0) {
double delta = 127. * contrast / 100;
a = 255. / (255. - delta * 2);
b = a * (brightness - delta);
} else {
double delta = -128. * contrast / 100;
a = (256. - delta * 2) / 255.;
b = a * brightness + delta;
}

Mat dst, hist;
image.convertTo(dst, CV_8U, a, b);
imshow("image", dst);

calcHist(&dst, 1, 0, Mat(), hist, 1, &histSize, 0);
Mat histImage = Mat::ones(200, 320, CV_8U) * 255;

normalize(hist, hist, 0, histImage.rows, MINMAX, CV_32F);

histImage = Scalar::all(255);
int binW = cvRound((double)histImage.cols / histSize);

for (int i = 0; i < histSize; i++)
rectangle(histImage, Point(i * binW, histImage.rows),
Point((i + 1) * binW, histImage.rows - cvRound(hist.at<float>(i))),
Scalar::all(0), -1, 8, 0);
imshow("histogram", histImage);
}

//--------------------------------------【help( )函数】--------------------------------------
// 描述:输出一些帮助信息
//----------------------------------------------------------------------------------------------
static void help() {
cout << "\n此程序结合滚动条的创建,演示了如何用calcHist来创建直方图。可以条件滚动条,看到不同形"
"态的图像直方图。\n"
<< std::endl;
}

//-----------------------------------【main( )函数】--------------------------------------------
// 描述:控制台应用程序的入口函数,我们的程序从这里开始
//-------------------------------------------------------------------------------------------------
int main(int argc, const char **argv) {
help();

image = imread("1.jpg", 1);
imshow("原始图", image);

// 载入原始图
image = imread("1.jpg", 0);
imshow("灰度图", image);

namedWindow("image", 0);
namedWindow("histogram", 0);

createTrackbar("brightness", "image", &_brightness, 200, updateBrightnessContrast);
createTrackbar("contrast", "image", &_contrast, 200, updateBrightnessContrast);

updateBrightnessContrast(0, 0);
waitKey();

return 0;
}

下一篇:OpenCV4入门系列教程74:图像直方图比较