首页 > 代码库 > 9.调整图像的对比度和亮度

9.调整图像的对比度和亮度

//调整对比度和亮度
int g_nContrastValue;
int g_nBrightValue;
Mat g_srcImage_Contrast;
Mat g_dstImage_Contrast;
//滑动条事件处理函数
void on_ContrastAndBright(int,void*)
{
	for(int x=0;x<g_srcImage_Contrast.rows;x++)
	{
		for(int y=0;y<g_srcImage_Contrast.cols;y++)
		{
			for(int z=0;z<3;z++)
			{
				g_dstImage_Contrast.at<Vec3b>(x,y)[z]=saturate_cast<uchar>((g_nContrastValue*0.01)*(g_srcImage_Contrast.at<Vec3b>(x,y)[z])+g_nBrightValue);
			}
		}
	}
	imshow("原图",g_srcImage_Contrast);
	imshow("调整对比度和亮度",g_dstImage_Contrast);
}
void Test_InitContrastAndBright()
{
	namedWindow("原图",1);
	namedWindow("调整对比度和亮度",1);
	g_srcImage_Contrast=imread("D:\\OpenCV Projects\\OpenCV_Test_Image\\27.jpg");
	g_dstImage_Contrast=Mat::zeros(g_srcImage_Contrast.size(),g_srcImage_Contrast.type());
	g_nContrastValue=http://www.mamicode.com/75;"调整对比度","调整对比度和亮度",&g_nContrastValue,300,on_ContrastAndBright);
	createTrackbar("亮度","调整对比度和亮度",&g_nBrightValue,200,on_ContrastAndBright);
	on_ContrastAndBright(g_nContrastValue, 0);
	on_ContrastAndBright(g_nBrightValue, 0);
}

  技术分享

 

9.调整图像的对比度和亮度