split函数和merge函数
split函数的主要功能是把一个彩色图像分割成3个通道,方便进一步的图像处理,具体说明如下:
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& mv)
其实还有一个函数merge可以实现相反的操作,简单说明如下:
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& mv, OutputArray dst)
其示例代码如下:
#include "stdafx.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
int main( int argc, char** argv )
{
Mat src, srcOfMerge;
vector mv;
/// Read image ( same size, same type )
src = imread("d:\\images\\WindowsLogo.jpg");
if( !src.data ) { printf("Error loading src \n"); return -1; }
split(src,mv);
/// Create Windows
namedWindow("RGB", 1);
namedWindow("R", 1);
namedWindow("R", 1);
namedWindow("R", 1);
namedWindow("srcOfMerge", 1);
imshow( "RGB", src);
imshow("R",mv[0]);
imshow("G",mv[1]);
imshow("B",mv[2]);
merge(mv,srcOfMerge);
imshow("srcOfMerge",srcOfMerge);
waitKey(0);
return 0;
}