OpenCv--调整图像亮度和对比度

图像变换

  •   像素变换-点操作
  •   邻域操作-区域

调整图像亮度和对比度属于像素变换-点操作

原理:g(x,y)=f(x,y)+    

示例代码:

#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main()
{
	//原理: a*f(x)+b   a对应对比度,b对应亮度
	Mat src1, src2;
	src1 = imread("C:\\Users\\马迎伟\\Desktop\\heibao.jpg");
	//src2 = imread("C:\\Users\\马迎伟\\Desktop\\a1a.jpg");
	if (src1.empty())
	{
		cout << "could not find src1" << endl;
		return -1;
	}
	//将 8U3转换成float类型
	src1.convertTo(src2, CV_32F);
	imshow("input", src1);
	int height = src1.rows;
	int width = src1.cols;
	int nc = src1.channels();
	float alpha = 1.2;
	float beta = 10;
	for (int row = 0; row < height; row++)
	{
		for (int col = 0; col < width; col++)
		{
			if (nc==1)
			{
				float v = src1.at<uchar>(row, col);
				src1.at<uchar>(row, col) = saturate_cast<uchar>(alpha * v + beta);
			}
			else if (nc==3)
			{
				float b = src1.at<Vec3b>(row, col)[0];
				float g = src1.at<Vec3b>(row, col)[1];
				float r = src1.at<Vec3b>(row, col)[2];

				src1.at<Vec3b>(row, col)[0] = alpha * b + beta;
				src1.at<Vec3b>(row, col)[1] = alpha * g + beta;
				src1.at<Vec3b>(row, col)[2] = alpha * r + beta;
			}
		}
	}
	char name[] = "daigula k";
	namedWindow(name, CV_WINDOW_AUTOSIZE);
	imshow(name, src1);
	waitKey(0);
}

 

全部评论

相关推荐

09-27 00:29
东北大学 Java
伟大的麻辣烫:查看图片
阿里巴巴稳定性 75人发布 投递阿里巴巴等公司10个岗位
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务