阅读背景:

07- 提高图像对比度和亮度_lyjaoruiqiyuan的博客

来源:互联网 

07- 提高图像对比度和亮度 : 代码部分
 


#include<opencv2/opencv.hpp>;
#include<iostream>;
using namespace std;
using namespace cv;
int main(int argc, char* srgv) { //07- 调整图像对比度 和 亮度
	Mat source1, change1;
	source1 = imread("F:\OpenCV-Test\TestPicture\SourcePicture1\1.PNG");

	if (source1.empty()) {
		printf("can not load image ... \n");
		return -1;
	}
	char source1Title[] = "source1title ";
	imshow(source1Title, source1);
	//cvtColor(source1,change1,6);  //灰度图像 nchannels==1;单通道

	//contrast and bright changes
	change1 = Mat::zeros(source1.size(), source1.type());
	//Mat source2;
	//source1.convertTo(source2,CV_32F);  
	//将source1的图像深度转为32位即3b转为3f数据,提取像素值时可用source1.at<Vec3f>(row, col)[0]
	int rows = source1.rows;
	int cols = source1.cols;
	int nchannels = source1.channels();
	float alpha = 1.5;
	float beta = 30;
	for (int row = 0; row < rows; row++) {
		for (int col = 0; col < cols; col++) {
			if (nchannels == 1) {  //若单通道
				float piex = source1.at<uchar>(row, col); //像素值
				change1.at<uchar>(row, col) = saturate_cast<uchar>(piex * alpha + beta); //记忆
						                      //像素值增加:像素值*对比度 + 亮度值
			}
			else if (nchannels == 3) { //若3通道
				float B = source1.at<Vec3b>(row, col)[0];  //float B = source1.at<Vec3f>(row, col)[0];
				float G = source1.at<Vec3b>(row, col)[1];  //float G = source1.at<Vec3f>(row, col)[1];
				float R = source1.at<Vec3b>(row, col)[2];  //float R = source1.at<Vec3f>(row, col)[2];

				change1.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(B * alpha + beta); //记忆
				change1.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(G * alpha + beta);
				change1.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(R * alpha + beta);
				                                     // 像素值增加:像素值*对比度 + 亮度值
//change1.at<Vec3b>(row, col)[0] = saturate_cast<uchar>((source1.at<Vec3b>(row, col)[0]) * alpha + beta);//简化
			}
			else {
				printf("image channels is not 1 or 3 ");
				return -1;
			}
		}
	}
	char change1Title[] = "change1Title";
	imshow(change1Title, change1);

	waitKey(0);
	return 0;
}
#include<opencv2/ope



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: