理论公式:
g(x)=a*f(x)+b; 其中a>0为对比度,b为亮度;
消化完大神的源代码:
//滑动块全局变量
Mat g_src,g_dst;
int g_ContrastValue=100,g_BrightValue=0,g_MaxContrastValue=300,g_MaxBraghtValue=100;
void on_ContrastAndBright(int,void*)
{
g_dst=g_ContrastValue*0.01*g_src+g_BrightValue;
imshow("原始图",g_src);
imshow("对比度和亮度测试",g_dst);
//cout<<"yuantu\n"<<g_src<<endl;
//cout<<g_dst<<endl;
}
//对比度和亮度调节
bool ContrastAndBright()
{
g_src=imread("lena.jpg");
if (!g_src.data)
{
printf("reading g_src error!!!");return 0;
}
//g_src=g_src(Rect(0,0,10,10));
namedWindow("对比度和亮度测试");
char ContrastName[50],BrightNamme[50];
sprintf_s(ContrastName,"对比度 %d",g_MaxContrastValue);
sprintf_s(BrightNamme,"亮度值 %d",g_MaxBraghtValue);
createTrackbar(ContrastName,"对比度和亮度测试",&g_ContrastValue,g_MaxContrastValue,on_ContrastAndBright);
createTrackbar(BrightNamme,"对比度和亮度测试",&g_BrightValue,g_MaxBraghtValue,on_ContrastAndBright);
on_ContrastAndBright(g_ContrastValue,0);
on_ContrastAndBright(g_BrightValue,0);
return 1;
}
int main()
{
if(ContrastAndBright())
cout<<"success"<<endl;
waitKey(0);
return 0;
}
此程序的核心代码充分体现了opencv库函数的变态(可直接加减),亦可以用vect写3个循环来作理论公式的计算。