OpenCV图像处理---调整图像亮度与对比度

理论:

  • 图像变换可以看作如下:
    • 像素变换 – 点操作
    • 邻域操作 – 区域

调整图像亮度和对比度属于像素变换-点操作
g(i,j) = αf(i,j) +β (其中 α>0 ,α 增益(放大倍数),用来控制图像的对比度,β (偏置),用控制图像的亮度

参考代码:

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main (int argc,char **argv)
{
	Mat src = imread("/home/shining/工作/Opencv-test/picture/01.jpg");
	if(src.empty())
	{
		cout << "Could not load image"<<endl;

		return -1;
	}
	
	cvtColor(src,src,COLOR_BGR2GRAY);

	imshow("input",src);

	int height = src.rows;
	int width = src.cols;
	int channel = src.channels();

	float alpha = 1.2f;
	float beta = 10.0f;

	Mat dst ,convert,dst_convert;

	dst = Mat::zeros(src.size(),src.type());
	src.convertTo(convert,CV_32F);
	dst_convert = Mat :: zeros(src.size(),src.type());

	for(int row = 0;row<height;row++)
	{
		for(int col = 0;col<width;col++)
		{
			if(channel == 1)
			{
				int v = src.at<uchar>(row,col);
				dst.at<uchar>(row,col) = saturate_cast<uchar>(alpha*v + beta);
			}
			else if(channel == 3)
			{
				int b = src.at<Vec3b>(row,col)[0];
				int g = src.at<Vec3b>(row,col)[1];
				int r = src.at<Vec3b>(row,col)[2];
				dst.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(alpha*b + beta);//调整对比度与亮度,公式: g(i,j) = α*f(i,j)+β  其中 α>0, β是增益变量
                dst.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(alpha*g + beta);//图像越亮,颜色值越往255靠近
                dst.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(alpha*r + beta);//对比度: 就是两个像素点之间的差值,差值越大对比度越高,反之越低

				float f_b = convert.at<Vec3f>(row, col)[0];
                float f_g = convert.at<Vec3f>(row, col)[1];
                float f_r = convert.at<Vec3f>(row, col)[2];

				dst_convert.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(alpha*f_b + beta);//用float计算会比uchar精度高一些,值会大一点点
                dst_convert.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(alpha*f_g + beta);
                dst_convert.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(alpha*f_r + beta);

			}
		}
	}

	imshow("dst_Image",dst);
	imshow("dst_convert Image",dst_convert);
	waitKey(0);
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值