##名称:图像对象度与对比度调整(由轨迹条分别控制对比度和亮度值) ##平台:QT5.7.1+opencv3.2.0 ##时间:2017年12月13日 /***********建立QT控制台程序************/ #include <QCoreApplication> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <iostream>
using namespace std; using namespace cv; //全局变量声明
Mat srcImage; Mat dstImage; int contrastValue; int brightValue; //const string WINDOW_NAME = "contrast&brightValue"; /*******改变图像对比度和亮度值的回调函数*******/
void on_change(int,void*) { namedWindow("contrast&brightValue",1); for(int i = 0; i < srcImage.rows; i++) { for(int j = 0; j < srcImage.cols; j++) { for(int c = 0; c < 3; c++) { dstImage.at<Vec3b>(i,j)[c] = saturate_cast<uchar>((contrastValue * 0.01) * ( srcImage.at<Vec3b>(i,j)[c] ) + brightValue); } } } imshow("contrast&brightValue",dstImage); } int main() { srcImage = imread("/home/ttwang/11.jpg"); dstImage = Mat::zeros(srcImage.size(),srcImage.type()); contrastValue = 80;//设定对比度初值
brightValue = 80;//设定对亮度的初值
namedWindow("contrast&brightValue",1); //创建轨迹条
createTrackbar("对比度: ", "contrast&brightValue", &contrastValue, 300, on_change);//第二个参数不能为中文,该参数为中文时,我的运行不成功
createTrackbar("亮度: ","contrast&brightValue", &brightValue, 200, on_change); //进行回调函数初始化
on_change(contrastValue,0); on_change(brightValue,0); waitKey(0); return 0; }