OpenCV(C++)---调整图像亮度和对比度

1、理论

  • 图像变换可以看作如下:
    • 像素变换 – 点操作
    • 邻域操作 – 区域
  • 调整图像亮度和对比度属于像素变换-点操作:
    在这里插入图片描述

2、重要的API

  • Mat new_image = Mat::zeros( image.size(), image.type() );
    创建一张跟原图像大小和类型一致的空白图像、像素值初始化为0

  • saturate_cast(value)
    确保值大小范围为0~255之间

  • Mat.at(y,x)[index]=value
    给每个像素点每个通道赋值

3、代码实现

#include 
#include 

using namespace std;
using namespace cv;

int main(int argc, char** argv) {
	Mat src, dst;
	src = imread("C:/Users/86180/Desktop/文档/学习/opencv/图片处理/zqy4_1.jpg");
	if (!src.data) {
		cout << "could not load image..4." << endl;
		return -1;
	}
	char input_win[] = "input image";
	namedWindow(input_win, WINDOW_AUTOSIZE);
    imshow(input_win, src);
	
	//对比度跟亮度的调整
	int height = src.rows;
	int width = src.cols;
	dst = Mat::zeros(src.size(), src.type());
	float alpha = 1.5;
	float beta = 10;
	for (int row = 0; row < height; row++)
	{
		for (int col = 0; col < width; col++)
		{
			if (src.channels() == 3)
			{
				int b = src.at<Vec3b>(row, col)[0];
				int g = src.at<Vec3b>(row, col)[1];
				int r = src.at<Vec3b>(row, col)[2];

				dst.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(b * alpha + beta);
				dst.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(g * alpha + beta);
				dst.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(r * alpha + beta);

			}
			else if (src.channels() == 1)
			{
				float v = src.at<uchar>(row, col);
				dst.at<uchar>(row, col) = saturate_cast<uchar>(v * alpha + beta);
			}
		}
	}
	char output_title[] = "contrast and brightness change demo";
	namedWindow(output_title, WINDOW_AUTOSIZE);
	imshow(output_title, dst);

	waitKey(0);
	return 0;
}

其运行结果为:

如果需要用Vec3f,则需要用:

    Mat m1;
	src.convertTo(m1, CV_32F);

其完整代码实现为:

#include 
#include 

using namespace std;
using namespace cv;

int main(int argc, char** argv) {
	Mat src, dst;
	src = imread("C:/Users/86180/Desktop/文档/学习/opencv/图片处理/zqy4_1.jpg");
	if (!src.data) {
		cout << "could not load image..4." << endl;
		return -1;
	}
	char input_win[] = "input image";
	namedWindow(input_win, WINDOW_AUTOSIZE);
    imshow(input_win, src);
	
	//对比度跟亮度的调整
	int height = src.rows;
	int width = src.cols;
	dst = Mat::zeros(src.size(), src.type());
	float alpha = 1.5;
	float beta = 10;

	Mat m1;//注意
	src.convertTo(m1, CV_32F);//注意
	for (int row = 0; row < height; row++)
	{
		for (int col = 0; col < width; col++)
		{
			if (src.channels() == 3)
			{
				int b = m1.at<Vec3f>(row, col)[0];//注意
				int g = m1.at<Vec3f>(row, col)[1];//注意
				int r = m1.at<Vec3f>(row, col)[2];//注意

				dst.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(b * alpha + beta);
				dst.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(g * alpha + beta);
				dst.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(r * alpha + beta);

			}
			else if (src.channels() == 1)
			{
				float v = src.at<uchar>(row, col);
				dst.at<uchar>(row, col) = saturate_cast<uchar>(v * alpha + beta);
			}
		}
	}
	char output_title[] = "contrast and brightness change demo";
	namedWindow(output_title, WINDOW_AUTOSIZE);
	imshow(output_title, dst);

	waitKey(0);
	return 0;
}

其运行结果为:

修改 alpha 和 beta 的值可以进行对比度和亮度的调整。

4、单通道和多通道的区别

  • 单通道: 此通道上值为0-255。 (255为白色,0是黑色) 只能表示灰度,不能表示彩色。
  • 三通道:在BGR (255,255,255为白色, 0,0,0是黑色 ) 可以表示彩色, 灰度也是彩色的一种。

cvtColor这一块经常出错,经过仔细的研究这一函数,发现原来RGB转换成灰度图像时,输出的图像要定义为单通道的图像,学习的时候应该要谨慎,认真。

cvtColor(src, src, COLOR_BGR2GRAY);

灰度图像的操作,完整代码如下:

using namespace std;
using namespace cv;

int main(int argc, char** argv) {
	Mat src, dst;
	src = imread("C:/Users/86180/Desktop/文档/学习/opencv/图片处理/zqy4_1.jpg");
	if (!src.data) {
		cout << "could not load image..4." << endl;
		return -1;
	}
	char input_win[] = "input image";
	cvtColor(src, src, COLOR_BGR2GRAY);//注意了
	namedWindow(input_win, WINDOW_AUTOSIZE);
    imshow(input_win, src);
	
	//对比度跟亮度的调整
	int height = src.rows;
	int width = src.cols;
	dst = Mat::zeros(src.size(), src.type());
	float alpha = 1.5;
	float beta = 10;

	Mat m1;
	src.convertTo(m1, CV_32F);
	for (int row = 0; row < height; row++)
	{
		for (int col = 0; col < width; col++)
		{
			if (src.channels() == 3)
			{
				int b = m1.at<Vec3f>(row, col)[0];
				int g = m1.at<Vec3f>(row, col)[1];
				int r = m1.at<Vec3f>(row, col)[2];

				dst.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(b * alpha + beta);
				dst.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(g * alpha + beta);
				dst.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(r * alpha + beta);

			}
			else if (src.channels() == 1)
			{
				float v = src.at<uchar>(row, col);
				dst.at<uchar>(row, col) = saturate_cast<uchar>(v * alpha + beta);
			}
		}
	}
	char output_title[] = "contrast and brightness change demo";
	namedWindow(output_title, WINDOW_AUTOSIZE);
	imshow(output_title, dst);

	waitKey(0);
	return 0;
}

其运行结果为;

你可能感兴趣的:(opencv,opencv,c++,计算机视觉)