1 #include <opencv2/core/core.hpp> 2 #include<opencv2/highgui/highgui.hpp> 3 #include"opencv2/imgproc/imgproc.hpp" 4 #include <iostream> 5 6 using namespace std; 7 using namespace cv; 8 9 static void ContrastAndBright(int, void *); 10 11 int g_nContrastValue; //对比度值 12 int g_nBrightValue; //亮度值 13 Mat g_srcImage, g_dstImage; 14 15 int main() 16 { 17 //读入用户提供的图像 18 g_srcImage = imread("E:/test.jpg"); 19 20 g_dstImage = Mat::zeros(g_srcImage.size(), g_srcImage.type()); 21 22 //设定对比度和亮度的初值 23 g_nContrastValue = 80; 24 g_nBrightValue = 80; 25 26 //创建窗口 27 namedWindow("【效果图窗口】", 1); 28 29 //创建轨迹条 30 createTrackbar("对比度:", "【效果图窗口】", &g_nContrastValue, 300, ContrastAndBright); 31 createTrackbar("亮 度:", "【效果图窗口】", &g_nBrightValue, 200, ContrastAndBright); 32 33 //调用回调函数 34 ContrastAndBright(g_nContrastValue, 0); 35 ContrastAndBright(g_nBrightValue, 0); 36 waitKey(); 37 return 0; 38 } 39 40 41 static void ContrastAndBright(int, void *) 42 { 43 44 //创建窗口 45 namedWindow("【原始图窗口】", 1); 46 47 //三个for循环,执行运算 g_dstImage(i,j) =a*g_srcImage(i,j) + b 48 for (int y = 0; y < g_srcImage.rows; y++) 49 { 50 for (int x = 0; x < g_srcImage.cols; x++) 51 { 52 for (int c = 0; c < 3; c++) 53 { 54 g_dstImage.at<Vec3b>(y, x)[c] = saturate_cast<uchar>((g_nContrastValue*0.01)*(g_srcImage.at<Vec3b>(y, x)[c]) + g_nBrightValue); //改变每个像素每个通道的值 55 } 56 } 57 } 58 59 //显示图像 60 imshow("【原始图窗口】", g_srcImage); 61 imshow("【效果图窗口】", g_dstImage); 62 }
颜色空间缩减
//使用动态地址遍历
for (int i = 0; i < src.rows; i++) { for (int j = 0; j < src.cols; j++) { for (int c = 0; c < 3; c++) { dst.at<Vec3b>(i, j)[c] = (src.at<Vec3b>(i, j)[c] / div) *div; //每10个像素变为一个。 0-9->0 10-19->10 } } }
//使用迭代器遍历
dst = src.clone(); Mat_<Vec3b>::iterator it = dst.begin<Vec3b>(); Mat_<Vec3b>::iterator itend = dst.end<Vec3b>(); for (; it != itend; it++) { for (int i = 0; i < 3; i++) { (*it)[i] = (*it)[i] / 10 * 10; } }
//使用鼠标画图
1 #include<opencv.hpp> 2 #include<iostream> 3 using namespace std; 4 using namespace cv; 5 #define WINDOW_NAME "程序窗口" 6 Rect g_rectangle; 7 bool is_Press = false; 8 RNG g_rng(12345); 9 10 11 void DrawRectangle(cv::Mat& img, cv::Rect box) 12 { 13 rectangle(img, box.tl(), box.br(), Scalar(g_rng.uniform(0, 255), 14 g_rng.uniform(0, 255), g_rng.uniform(0, 255))); 15 } 16 void on_MouseHandle(int event,int x,int y,int flags,void* param) 17 { 18 Mat& image = *(cv::Mat*)param; 19 switch (event) 20 { 21 case EVENT_MOUSEMOVE: 22 { 23 if (is_Press) 24 { 25 g_rectangle.width = x - g_rectangle.x; 26 g_rectangle.height = y - g_rectangle.y; 27 } 28 } 29 break; 30 case EVENT_LBUTTONDOWN: 31 { 32 is_Press = true; 33 g_rectangle.x = x; 34 g_rectangle.y = y; 35 } 36 break; 37 case EVENT_LBUTTONUP: 38 { 39 is_Press = false; 40 if (g_rectangle.width < 0) 41 { 42 g_rectangle.x += g_rectangle.width; 43 g_rectangle.width *= -1; 44 } 45 if (g_rectangle.height < 0) 46 { 47 g_rectangle.y += g_rectangle.height; 48 g_rectangle.height *= -1; 49 } 50 DrawRectangle(image, g_rectangle); 51 } 52 break; 53 } 54 } 55 56 int main() 57 { 58 g_rectangle = Rect(-1, -1, 0, 0); 59 Mat srcImage(600, 800, CV_8UC3), tmpImage; 60 srcImage.copyTo(tmpImage); 61 srcImage = Scalar::all(0); 62 63 namedWindow(WINDOW_NAME); 64 setMouseCallback(WINDOW_NAME, on_MouseHandle, (void*)& srcImage); 65 while (1) 66 { 67 srcImage.copyTo(tmpImage); 68 if (is_Press) 69 DrawRectangle(tmpImage, g_rectangle); 70 imshow(WINDOW_NAME, tmpImage); 71 if (waitKey(10) == 27) 72 break; 73 } 74 return 0; 75 }