// 利用轨迹条调整图像对比度和亮度.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include<opencv2/opencv.hpp>
#include<opencv2/highgui/highgui.hpp>
using namespace cv;
void onconst(int, void*);
int g_nl;//对比度
int g_cl;//亮度值
Mat src, dst;
int main()
{
src = imread("E:\\Pictures\\1.jpg");
dst = Mat::zeros(src.size(),src.type());
//设置对比度
g_nl = 80;
g_cl = 80;
namedWindow("效果窗口",1);
//创建轨迹条
createTrackbar("对比度","效果窗口",&g_nl,300,onconst);
createTrackbar("亮度","效果窗口", &g_cl, 200, onconst);
//回调函数初始化
onconst(g_nl,0);
onconst(g_cl,0);
while(char(waitKey(1) )!= 'q'){}
return 0;
}
void onconst(int,void*) {
namedWindow("原始窗口", 1);
// 三个for循环,执行运算 g_dstImage(i,j) = a*g_srcImage(i,j) + b 调整对比度和亮度的公式
for (int y = 0; y < src.rows; y++)
{
for (int x = 0; x < src.cols; x++)
{
for (int c = 0; c < 3; c++)
{
dst.at<Vec3b>(y, x)[c] = saturate_cast<uchar>((g_nl*0.01)*(src.at<Vec3b>(y, x)[c]) + g_cl);
}
}
}
// 显示图像
imshow("【原始图窗口】", src);
imshow("【效果图窗口】", dst);
}
#include "stdafx.h"
#include<opencv2/opencv.hpp>
#include<opencv2/highgui/highgui.hpp>
using namespace cv;
void onconst(int, void*);
int g_nl;//对比度
int g_cl;//亮度值
Mat src, dst;
int main()
{
src = imread("E:\\Pictures\\1.jpg");
dst = Mat::zeros(src.size(),src.type());
//设置对比度
g_nl = 80;
g_cl = 80;
namedWindow("效果窗口",1);
//创建轨迹条
createTrackbar("对比度","效果窗口",&g_nl,300,onconst);
createTrackbar("亮度","效果窗口", &g_cl, 200, onconst);
//回调函数初始化
onconst(g_nl,0);
onconst(g_cl,0);
while(char(waitKey(1) )!= 'q'){}
return 0;
}
void onconst(int,void*) {
namedWindow("原始窗口", 1);
// 三个for循环,执行运算 g_dstImage(i,j) = a*g_srcImage(i,j) + b 调整对比度和亮度的公式
for (int y = 0; y < src.rows; y++)
{
for (int x = 0; x < src.cols; x++)
{
for (int c = 0; c < 3; c++)
{
dst.at<Vec3b>(y, x)[c] = saturate_cast<uchar>((g_nl*0.01)*(src.at<Vec3b>(y, x)[c]) + g_cl);
}
}
}
// 显示图像
imshow("【原始图窗口】", src);
imshow("【效果图窗口】", dst);
}