Open CV 学习笔记:多通道图像混合

一、通道分离函数:split函数


Divides a multi-channel array into several single-channel arrays.

C++: void split(const Mat& mtx, Mat* mv)

C++: void split(const Mat& mtx, vector<Mat>& mv)

Parameters:                    
  • mtx – Source multi-channel array.
  • 分离的多通道数组
  • mv – Destination array or vector of arrays. In the first variant of the function the number of arrays must match mtx.channels() . The arrays themselves are reallocated, if needed.
  • 输出的数组或者是vector容器

The functions split split a multi-channel array into separate single-channel arrays:

分割多通道转换为独立的单通道

\texttt{mv} [c](I) =  \texttt{mtx} (I)_c

If you need to extract a single channel or do some other sophisticated channel permutation, use mixChannels() .


二、通道合并函数:merge函数


Composes a multi-channel array from several single-channel arrays.


C++: void merge(const Mat* mv, size_t count, OutputArray dst)

C++: void merge(const vector<Mat>& mv, OutputArray dst)

与split函数相反,将孤立的单通道合并成一个多通道
Parameters:          
  • mv – Source array or vector of matrices to be merged. All the matrices in mv must have the same size and the same depth.
  • 输入矩阵或vector容器
  • count – Number of source matrices when  mv is a plain C array. It must be greater than zero.
  • 输入矩阵的个数
  • dst – Destination array of the same size and the same depth as  mv[0] . The number of channels will be the total number of channels in the matrix array.
  • 输出矩阵

The functions merge merge several arrays to make a single multi-channel array. That is, each element of the output array will be a concatenation of the elements of the input arrays, where elements of i-th input array are treated as mv[i].channels()-element vectors.


示例:

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

using namespace cv;
using namespace std;



int main(int argc,char **argv){
	Mat srcImage,logoImage;
	vector<Mat>channels;

	Mat blueChannel;

	//blue
	srcImage = imread("img.jpg");
	logoImage = imread("logo.jpg",0);//注意载入的是灰度图像,因为下面融合的是单通道,原始图像为彩色图像
	imshow("原图",srcImage);
	imshow("logo",logoImage);
	if(!srcImage.data){
		cout<<"input error!"<<endl;
		return 0;
	}
	if(!logoImage.data){
		cout<<"input error!"<<endl;
		return 0;
	}

	split(srcImage,channels);
	blueChannel = channels.at(0);

	addWeighted(blueChannel(Rect(30,20,logoImage.cols,logoImage.rows)),1.0,logoImage,0.5,0.0,blueChannel(Rect(30,20,logoImage.cols,logoImage.rows)));

	merge(channels,srcImage);
	
	imshow("蓝色通道",srcImage);


	Mat greenChannel;

	//green
	srcImage = imread("img.jpg");
	logoImage = imread("logo.jpg",0);
	
	if(!srcImage.data){
		cout<<"input error!"<<endl;
		return 0;
	}
	if(!logoImage.data){
		cout<<"input error!"<<endl;
		return 0;
	}

	split(srcImage,channels);
	greenChannel = channels.at(1);


	addWeighted(greenChannel(Rect(30,20,logoImage.cols,logoImage.rows)),1.0,logoImage,0.5,0.0,greenChannel(Rect(30,20,logoImage.cols,logoImage.rows)));

	merge(channels,srcImage);

	imshow("绿色通道",srcImage);


	Mat redChannel;

	//red
	srcImage = imread("img.jpg");
	logoImage = imread("logo.jpg",0);

	if(!srcImage.data){
		cout<<"input error!"<<endl;
		return 0;
	}
	if(!logoImage.data){
		cout<<"input error!"<<endl;
		return 0;
	}

	split(srcImage,channels);
	redChannel = channels.at(2);


	addWeighted(redChannel(Rect(30,20,logoImage.cols,logoImage.rows)),1.0,logoImage,0.5,0.0,redChannel(Rect(30,20,logoImage.cols,logoImage.rows)));

	merge(channels,srcImage);

	imshow("红色通道",srcImage);

	waitKey(0);
	return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值