1 #include<opencv2/opencv.hpp> 2 #include<iostream> 3 4 using namespace std; 5 using namespace cv; 6 7 static void on_ContrastAndBright(int, void *); 8 static void ShowHelpText(); 9 10 int g_nContrastValue; //对比度值 11 int g_nBrightValue; //亮度值 12 Mat g_srcImage, g_dstImage; 13 14 int main() { 15 //读取输入图像 16 g_srcImage = imread("C:\\Users\\Nelsoner\\Desktop\\Camera Roll\\05.jpg"); 17 g_dstImage = Mat::zeros(g_srcImage.size(), g_srcImage.type()); 18 19 //设定对比度和亮的初值 20 g_nContrastValue = 80; 21 g_nBrightValue = 80; 22 23 24 25 //窗口 26 namedWindow("【效果图】", 1); 27 28 //创建滑块 29 createTrackbar("对比度:", "【效果图】", &g_nContrastValue, 300, on_ContrastAndBright); 30 createTrackbar("亮 度:", "【效果图】", &g_nBrightValue, 200, on_ContrastAndBright); 31 32 //进行回调函数的初始化 33 on_ContrastAndBright(g_nContrastValue, 0); 34 on_ContrastAndBright(g_nBrightValue, 0); 35 36 waitKey(); 37 return 0; 38 } 39 40 static void on_ContrastAndBright(int, void *) { 41 //创建窗口 42 namedWindow("【原始图窗口】", 1); 43 //三个for循环,执行运算g_dstImage(i,j) = a*g_srcImage(i,j) +b 44 for (int y = 0; y < g_srcImage.rows; y++) { 45 for (int x = 0; x < g_srcImage.cols; x++) { 46 for (int c = 0; c < 3; c++) { 47 g_dstImage.at<Vec3b>(y, x)[c] = saturate_cast<uchar>((g_nContrastValue*0.01)*(g_srcImage.at<Vec3b>(y, x)[c]) + g_nBrightValue); 48 } 49 } 50 } 51 52 //显示图像 53 imshow("【原始图窗口】", g_srcImage); 54 imshow("【效果图】", g_dstImage); 55 }
时间: 2024-11-04 20:39:13