split函数用于将一个多通道数组分离成几个单通道数组。
split函数的C++版本有两个原型,分别是:
void split(const Mat& src, Mat*mvbegin);
void split(InputArray m, OutputArrayOfArrays mv);
参数含义如下:
第一个参数:InputArray m(const Mat& src),填我们需要进行分离的多通道数组。
第二个参数:OutputArrayOfArrays mv,填函数的输出数组或者输出的vector容器。
split函数分割多通道数组转换成独立的单通道数组,公式如下:
下边是一个示例:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
void main(){
vector<Mat> channels;
Mat imageBlueChannel;
Mat imageGreenChannel;
Mat imageRedChannel;
Mat srcImage4 = imread("mogu.jpg");
imshow("【原始图】", srcImage4);
//把一个3通道图像转换成3个单通道图像
split(srcImage4, channels);
imageBlueChannel = channels.at(0);
imageGreenChannel = channels.at(1);
imageRedChannel = channels.at(2);
//显示单通道图像
imshow("【BlueChannel】", imageBlueChannel);
imshow("【GreenChannel】", imageGreenChannel);
imshow("【RedChannel】", imageRedChannel);
waitKey();
}
代码解析:
上述代码先做了相关的类型声明,然后把载入的3通道图像转换成3个单通道图像,放到vector<Mat>类型的channels中,接着进行引用赋值。
OpenCV的BGR色彩空间(Blue、Green、Red,蓝、绿、红),其中channels.at(0)表示引用取出的是蓝色分量,channels.at(1)表示引用取出的是绿色分量,channels.at(2)表示引用取出的是红色分量。
运行结果:
原始图 | BlueChannel |
GreenChannel | RedChannel |