一、对比度亮度调整

 #include<opencv2/opencv.hpp>
using namespace cv; #define WIN_NAME "输出图像"
Mat src,dst;
int contrast=,bright=; void onChange(int,void*){
for (int i = ; i < src.rows; i++)
{
for (int j = ; j < src.cols; j++)
{
//saturate_cast<uchar> 溢出保护:if(data<0) data=0; if(data>255) data=255;
dst.at<Vec3b>(i,j)[]=saturate_cast<uchar>(src.at<Vec3b>(i,j)[]*contrast*0.01+bright);
dst.at<Vec3b>(i,j)[]=saturate_cast<uchar>(src.at<Vec3b>(i,j)[]*contrast*0.01+bright);
dst.at<Vec3b>(i,j)[]=saturate_cast<uchar>(src.at<Vec3b>(i,j)[]*contrast*0.01+bright);
}
}
imshow("原图",src);
imshow(WIN_NAME,dst);
} void main(){
src=imread("E://1.jpg");
dst=Mat::zeros(src.size(),src.type());
//Mat::zeros();//将矩阵元素置为0
//Mat::ones();//置1
namedWindow(WIN_NAME,CV_WINDOW_AUTOSIZE);
createTrackbar("对比度",WIN_NAME,&contrast,,onChange,);
createTrackbar("亮 度",WIN_NAME,&bright,,onChange,); onChange(contrast,);//回调函数初始化
onChange(bright,); waitKey(); }

opencv学习之路(9)、对比度亮度调整与通道分离-LMLPHP

二、通道分离与合并

 #include<opencv2/opencv.hpp>
using namespace cv; void main(){
Mat img=imread("E://2.jpg");
Mat dst;
vector<Mat>channels;//定义Mat类型的向量 split(img,channels);//通道分离
Mat blue=channels.at();
Mat green=channels.at();
Mat red=channels.at(); threshold(blue,blue,,,THRESH_BINARY);//二值化:大于200的赋值255,小于200的赋值0
threshold(green,green,,,THRESH_BINARY);
threshold(red,red,,,THRESH_BINARY); merge(channels,dst);//通道合并 imshow("原图",img);
imshow("blue",blue);
imshow("green",green);
imshow("red",red);
imshow("dst",dst);
waitKey();
}

opencv学习之路(9)、对比度亮度调整与通道分离-LMLPHP

05-06 06:46