0
点赞
收藏
分享

微信扫一扫

OpenCV.修改图像 亮度和对比度

丹柯yx 2022-02-02 阅读 75

代码可用,修改变量alpha和beta即可修改亮度和对比度。

#include <opencv2/opencv.hpp>
#include <iostream>
#include <opencv2/core/utils/logger.hpp>

using namespace std;
using namespace cv;

int main(int argc, const char* argv[])
{
	utils::logging::setLogLevel(utils::logging::LOG_LEVEL_SILENT);	//设置日志输出

	Mat src = imread("E:/picture/9.jpg");
	Mat dst;
	dst = Mat::zeros(src.size(), src.type());
	int height = src.rows;
	int width = src.cols;
	float alpha=1.2;
	float beta=30.0;
	for (int row = 0; row < height; row++)
	{
		for (int col = 0; col < width; col++)
		{
			//read the pixs;
			float b = src.at<Vec3b>(row, col)[0];
			float g = src.at<Vec3b>(row, col)[1];
			float r = src.at<Vec3b>(row, col)[2];
			//write the pixs;
			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);
		}
	}
	imshow("input", src);
	imshow("output", dst);
	waitKey(0);
	destroyAllWindows();
	return 0;
}

输出结果: 

 

举报

相关推荐

0 条评论