欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

opencv 通道分离(split)与合并(merge)

程序员文章站 2022-05-16 15:15:43
...

通道分离

void cv::split(
	const cv::Mat& mtx, //输入图像
	vector<Mat>& mv // 输出的多通道序列(n个单通道序列)
);

通道合并

void cv::split(
	const cv::Mat& mtx, //输入图像
	vector<Mat>& mv // 输出的多通道序列(n个单通道序列)
);

demo

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


using namespace cv;
int main() {

	cv::Mat blank_ch,fin_img;
	cv::Mat src = cv::imread("E:\\Windowstest\\ConsoleApplication4\\ycy.jpg", 1);

	blank_ch = cv::Mat::zeros(cv::Size(src.cols, src.rows), CV_8UC1);
	std::vector<cv::Mat> rgbChannels(3);
	split(src, rgbChannels);//分为三个通道

    //R通道
	std::vector<cv::Mat> channels_r;
	channels_r.push_back(blank_ch);
	channels_r.push_back(blank_ch);
	channels_r.push_back(rgbChannels[2]);
	merge(channels_r,fin_img);
	cv::namedWindow("t1", 0);
	cv::imshow("t1", fin_img);

    //G通道
	std::vector<cv::Mat> channels_g;
	channels_g.push_back(blank_ch);
	channels_g.push_back(rgbChannels[1]);
	channels_g.push_back(blank_ch);
	merge(channels_g, fin_img);
	namedWindow("t2",0);
	imshow("t2",fin_img);

    //B通道
	std::vector<cv::Mat> channels_b;
	channels_b.push_back(rgbChannels[0]);
	channels_b.push_back(blank_ch);
	channels_b.push_back(blank_ch);
	merge(channels_b,fin_img);
	namedWindow("t3", 0);
	imshow("t3", fin_img);

	cv:namedWindow("result", 0);
	cv::imshow("result", src);
	cv::waitKey(0);

	return 0;
}