OpenCV自带例子(四)改变一张图片的对比度与亮度

对于第i行第j个像素

g(i,j) = alpha * f(i,j) + beta


alpha 对应着对比度

beta 对应着亮度

#include <cv.h>
#include <highgui.h>
#include <iostream>
using namespace cv;

double alpha; /**< Simple contrast control */
int beta; /**< Simple brightness control */

int main( int argc, char** argv )
{
	/// Read image given by user
	Mat image = imread( argv[1] );
	Mat new_image = Mat::zeros( image.size(), image.type() );
	/// Initialize values
	std::cout<<" Basic Linear Transforms "<<std::endl;
	std::cout<<"-------------------------"<<std::endl;
	std::cout<<"* Enter the alpha value [1.0-3.0]: ";std::cin>>alpha;
	std::cout<<"* Enter the beta value [0-100]: "; std::cin>>beta;
	/// Do the operation new_image(i,j) = alpha*image(i,j) + beta


	//image.convertTo(new_image, -1, alpha, beta);也可以使用这个函数代替for循环,同样的效果
	for( int y = 0; y < image.rows; y++ )
	{ 
		for( int x = 0; x < image.cols; x++ )
		{ 
			for( int c = 0; c < 3; c++ )
			{
				new_image.at<Vec3b>(y,x)[c] =
				saturate_cast<uchar>( alpha*( image.at<Vec3b>(y,x)[c] ) + beta );
				//saturate_cast 保证数据是0到255之间
			}			
		}
	}
	/// Create Windows
	namedWindow("Original Image", 1);
	namedWindow("New Image", 1);
	/// Show stuff
	imshow("Original Image", image);
	imshow("New Image", new_image);
	/// Wait until user press some key
	waitKey();
	return 0;
}



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值