改变图像的对比度和亮度-----学习记录(4)

本文介绍如何使用OpenCV进行图像处理的基本操作,包括读取图像、创建零矩阵、应用线性变换以及显示图像。通过输入alpha和beta值,用户可以调整图像的亮度和对比度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include "stdafx.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include "iostream"
using namespace cv;
using namespace std;


int main(int argc, char* argv[])
{
	int alpha;
	int beta;
	char* name1 = "sourec";
	char* name2 = "result";
	Mat src = imread( argv[1] );
	Mat new_map = Mat::zeros( src.size(), src.type() ); //构建一个新的零矩阵用来存储转换以后的图像  第一个参数是矩阵,第二个是矩阵类型参数3个通道的RGB
	/// 初始化
	cout << " Basic Linear Transforms " << endl;
	cout << "-------------------------" << endl;
	cout << "* Enter the alpha value [1.0-3.0]: ";
	cin >> alpha;
	cout << "* Enter the beta value [0-100]: ";
	cin >> beta;
	/*for (int y = 0; y < src.rows; y++)
	{
		for ( int x = 0; x < src.cols; x++ )
		{
			for ( int c = 0; c < 3; c++ )
			{
				new_map.at<Vec3b>(y,x)[c] = saturate_cast<uchar>( alpha * ( src.at<Vec3b>(y,x)[c] ) + beta );//<span style="font-family: Arial, Helvetica, sans-serif;">saturate_cast防止像素超出</span>
			}
		}
	}*/
	src.convertTo(new_map, -1, alpha, beta);  //等价于前面的循环处理结果      执行运算 new_map(i,j) = alpha*image(i,j) + beta
	
	namedWindow(name1,1);
	namedWindow(name2,1);
	imshow(name1,src);
	imshow(name2,new_map);
	waitKey();


	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值