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;
}



發佈了229 篇原創文章 · 獲贊 12 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章