opencv學習筆記之調整圖像的亮度與對比度

圖像的亮度與對比度調整

使用下式


f(x)爲輸入圖像,g(x)爲輸出圖像,alpha爲大於零的參數,beta爲偏置參數。

// Change_the_brghtied_img.cpp : 定義控制檯應用程序的入口點。
//

#include "stdafx.h"
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main()
{
	Mat img=imread("1.jpg",1);
	Mat out=Mat::zeros(img.size(),img.type());

	double alpha;
	int beta;

	cin>>alpha;
	cin>>beta;

	for(int y=0;y<img.rows;y++)
	{
		for(int x=0;x<img.cols;x++)
		{
			for(int c=0;c<3;c++)
			{
				//saturate_cast防止溢出
				out.at<Vec3b>(y,x)[c]=saturate_cast<uchar>(alpha*(img.at<Vec3b>(y,x)[c])+beta);
		
			}
		}
	}
	namedWindow("src",1);
	namedWindow("dst",1);

	imshow("src",img);
	imshow("dst",out);

	waitKey(0);
	return 0;
}
效果:

設置變量alpha=2.0;beta=20;

     

            原圖                                變換後的圖像


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章