opencv学习日记之单通道分离多通道融合

代码:

#include"stdafx.h"
#include
#include 
#include 
#include 

using namespace cv;
using namespace std;

bool LinearBlending();
void splitImage();
void colorReduce(Mat & inputImage, Mat & outputImage, int value1, int valut2, int value3);

int main(void)
{
	// 调用
	bool linearblending = LinearBlending();

	cout << linearblending << endl;

	splitImage();

	waitKey(0);
	return(0);  
}

bool LinearBlending() {
	// 定义局部变量
	double alphaValue = 0.6;
	double betaValue;
	// 读取图片,两个图片大小尺寸必须一样
	Mat src1, src2, dst;
	src1 = imread("E:\\pictures\\For_Project\\New_opencv\\newstar.jpg");
	src2 = imread("E:\\pictures\\For_Project\\New_opencv\\newgo.jpg");
	// 图像混合加权操作
	betaValue = 1.0 - alphaValue;
	addWeighted(src1, alphaValue, src2, betaValue, 0.6, dst);

	namedWindow("原图1");
	imshow("原图1", src1);

	namedWindow("原图2");
	imshow("原图2", src2);

	namedWindow("效果");
	imshow("效果", dst);

	return true;

}

void splitImage() {
	Mat srcImage = imread("E:\\pictures\\For_Project\\Opencv_Test\\cao.png");

	Mat blueImage, greenImage, redImage;

	vector channels;

	split(srcImage, channels);

	namedWindow("原图");
	imshow("原图", srcImage);

	blueImage = channels.at(0);
	namedWindow("Blue");
	imshow("Blue", blueImage);

	greenImage = channels.at(1);
	namedWindow("Green");
	imshow("Green", greenImage);

	redImage = channels.at(2);
	namedWindow("Red");
	imshow("Red", redImage);

	Mat dst;
	merge(channels, dst);
	imshow("合成", dst);

	Mat imgMix;
	imgMix.create(600, 800, CV_8UC3);
	colorReduce(imgMix, imgMix, 255, 0, 0);
	imshow("Mix", imgMix);
}

void colorReduce(Mat & inputImage, Mat & outputImage, int value1, int value2, int value3) {
	// 复制实参到临时变量
	outputImage = inputImage.clone();
	// 分别获取行数和列数
	int rowNumber = outputImage.rows;
	int colNumber = outputImage.cols;
	// 存取彩色图像像素
	for (int i = 0;i < rowNumber;i++) {
		for (int j = 0;j < colNumber;j++) {
			// 蓝色通道
			outputImage.at(i, j)[0] = value1;
			// 绿色通道
			outputImage.at(i, j)[1] = value2;
			// 红色通道
			outputImage.at(i, j)[2] = value3;
		}
	}
}
效果:

opencv学习日记之单通道分离多通道融合_第1张图片

你可能感兴趣的:(opencv,split,merge)