opencv图像处理04-图像线性混合与亮度对比度调整

1. 图像的线性混合:

f0x和f1x分别表示2张待混合的图像,2张图像的大小必须相同,gx为混合后的合成图

 

2.图像混合代码演示:

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

using namespace std;
using namespace cv;

int main()
{
	Mat src1 = imread("E:/2.png");
	Mat src2 = imread("E:/3.png");

	if (!src1.data) {
		cout << "could not load image Linux Logo..." << endl;
		return -1;
	}
	if (!src2.data) {
		cout << "could not load image WIN7 Logo..." << endl;
		return -1;
	}

	double alpha = 0.5;
	Mat dst;

	if ((src1.rows == src2.rows)&&(src1.cols == src2.cols)&&(src1.type()==src2.type())) {
		//图像权重混合API
		addWeighted(src1, alpha, src2, (1.0 - alpha), 0.0, dst);
		//multiply(src1, src2, dst, 1.0);
		//add(src1, src2, dst, Mat());
		imshow("test1", src1);
		imshow("test2", src2);
		imshow("test3", dst);
	}
	else {
		cout << "failed" << endl;
		return -1;
	}

	waitKey(0);

	return 0;
}

3. 图像亮度对比度调整:

调整图像lian亮度和对比度属于像素变换操作

alpha和beta分别为增益lian量,当alpha提高的时候,由于是ch乘法,图片里的不同像素的差距会越来越大,对比度得到提高

当beta提高时,图像每个像素点的值都增加,但是差距不变,导致亮度提高

4. 亮度对比度调整代码实现:

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

using namespace std;
using namespace cv;

int main()
{
	Mat src = imread("E:/2.png");
	if (src.empty()) {
		cout << "load failed" << endl;
	}

	imshow("test1", src);

	Mat dst;
	dst = Mat::zeros(src.size(), src.type());
	int height = src.rows;
	int weight = src.cols;
	int nn = src.channels();
	float alpha = 1.2;
	float beta = 20;


	for (int row = 0; row < height; row++) {
		for (int col = 0; col < weight; col++) {
			dst.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(alpha*src.at<Vec3b>(row, col)[0] + beta);
			dst.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(alpha*src.at<Vec3b>(row, col)[1] + beta);
			dst.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(alpha*src.at<Vec3b>(row, col)[2] + beta);
		}
	}

	imshow("test2", dst);

	waitKey(0);

	return 0;
}

猜你喜欢

转载自blog.csdn.net/qinchao315/article/details/81265466