1 #include <iostream>
2 #include <opencv2/opencv.hpp>
3 using namespace std;
4 using namespace cv;
5 //--------------------声明全局函数---------------------------------
6 static void on_ContrastAndBright(int, void*);
7 //static void helphelp();
8 //--------------------声明全局变量---------------------------------
9 Mat srcImg;
10 Mat dstImg;
11 int g_Bright;
12 int g_Contrast;
13 //--------------------主函数入口-----------------------------------
14 int main()
15 {
16 srcImg = imread("1.jpg");
17 if (!srcImg.data)
18 {
19 cout<<"0000"<<endl;
20 }
21 dstImg = Mat::zeros(srcImg.size(),srcImg.type());
22 namedWindow("stage2",1);
23 int g_Bright = 80;
24 int g_Contrst = 80;
25 namedWindow("stage",1);
26 //creat the Trackbar
27 createTrackbar("Contrast", "stage2", &g_Contrast, 300, on_ContrastAndBright);
28 createTrackbar("Bright", "stage2", &g_Bright, 250, on_ContrastAndBright);
29 on_ContrastAndBright(g_Contrst,0);
30 on_ContrastAndBright(g_Bright,0);
31 while (char(waitKey(1)) != ‘s‘)
32 {}
33 return 0;
34 }
35 static void on_ContrastAndBright(int,void*)
36 {
37 for(int i=0; i<srcImg.rows; i++)
38 {
39 for(int j=0; j<srcImg.cols; j++)
40 {
41 for(int c=0; c<3; c++)
42 {
43 dstImg.at<Vec3b>(i,j)[c] = saturate_cast<uchar>( (g_Contrast * 0.01) * ( srcImg.at<Vec3b>(i,j)[c]) + g_Bright );
44 }
45 }
46 }
47 imshow("stage",srcImg);
48 imshow("stage2",dstImg);
49 }