OpenCV学习之路(九) 图像对比度、亮度

官方文档

 

通常图像操作有两种:

点操作

邻域操作

对比度、亮度调整就是典型的点操作。

简单操作实例如下:

#include

using namespace cv;

void on_BrightnessAndContrast(int, void*);

int g_nContrast, g_nBrightness;
int g_nMaxContrast = 300;
int g_nMaxBrightness = 200;
Mat srcImage, dstImage;

int main()
{
	srcImage = imread("cat.jpg");

	dstImage = Mat::zeros(srcImage.size(), srcImage.type());

	namedWindow("【效果图窗口】", 1);
	createTrackbar("对比度", "【效果图窗口】", &g_nContrast, g_nMaxContrast, on_BrightnessAndContrast);
	createTrackbar("亮度", "【效果图窗口】", &g_nBrightness, g_nMaxBrightness, on_BrightnessAndContrast);

	on_BrightnessAndContrast(g_nContrast, 0);
	on_BrightnessAndContrast(g_nBrightness, 0);

	waitKey(0);
	return 0;
}

void on_BrightnessAndContrast(int, void*)
{
	for (int i = 0; i < srcImage.rows; i++)
		for (int j = 0; j < srcImage.cols; j++)
			for (int c = 0; c < srcImage.channels(); c++)
			{
				dstImage.at(i, j)[c] = saturate_cast((g_nContrast * 0.01) * (srcImage.at(i, j)[c]) + g_nBrightness);
			}

	
	imshow("原图", srcImage);
	imshow("【效果图窗口】", dstImage);
}

 代码运行结果如下图:

OpenCV学习之路(九) 图像对比度、亮度_第1张图片

 

你可能感兴趣的:(OpenCV,学习)