一、对比度亮度调整
1 #include<opencv2/opencv.hpp>
2 using namespace cv;
3
4 #define WIN_NAME "输出图像"
5 Mat src,dst;
6 int contrast=20,bright=20;
7
8 void onChange(int,void*){
9 for (int i = 0; i < src.rows; i++)
10 {
11 for (int j = 0; j < src.cols; j++)
12 {
13 //saturate_cast<uchar> 溢出保护:if(data<0) data=0; if(data>255) data=255;
14 dst.at<Vec3b>(i,j)[0]=saturate_cast<uchar>(src.at<Vec3b>(i,j)[0]*contrast*0.01+bright);
15 dst.at<Vec3b>(i,j)[1]=saturate_cast<uchar>(src.at<Vec3b>(i,j)[1]*contrast*0.01+bright);
16 dst.at<Vec3b>(i,j)[2]=saturate_cast<uchar>(src.at<Vec3b>(i,j)[2]*contrast*0.01+bright);
17 }
18 }
19 imshow("原图",src);
20 imshow(WIN_NAME,dst);
21 }
22
23 void main(){
24 src=imread("E://1.jpg");
25 dst=Mat::zeros(src.size(),src.type());
26 //Mat::zeros();//将矩阵元素置为0
27 //Mat::ones();//置1
28 namedWindow(WIN_NAME,CV_WINDOW_AUTOSIZE);
29 createTrackbar("对比度",WIN_NAME,&contrast,300,onChange,0);
30 createTrackbar("亮 度",WIN_NAME,&bright,200,onChange,0);
31
32 onChange(contrast,0);//回调函数初始化
33 onChange(bright,0);
34
35 waitKey(0);
36
37 }
二、通道分离与合并
1 #include<opencv2/opencv.hpp>
2 using namespace cv;
3
4 void main(){
5 Mat img=imread("E://2.jpg");
6 Mat dst;
7 vector<Mat>channels;//定义Mat类型的向量
8
9 split(img,channels);//通道分离
10 Mat blue=channels.at(0);
11 Mat green=channels.at(1);
12 Mat red=channels.at(2);
13
14 threshold(blue,blue,200,255,THRESH_BINARY);//二值化:大于200的赋值255,小于200的赋值0
15 threshold(green,green,200,255,THRESH_BINARY);
16 threshold(red,red,200,255,THRESH_BINARY);
17
18 merge(channels,dst);//通道合并
19
20 imshow("原图",img);
21 imshow("blue",blue);
22 imshow("green",green);
23 imshow("red",red);
24 imshow("dst",dst);
25 waitKey(0);
26 }