1 所用到的主要OpenCv API
/** @overload
@param m input multi-channel array.输入一副多通道图片
@param mv output vector of arrays; the arrays themselves are reallocated, if needed.
结果输出至一个多数组容器,数组在容器中可以进行再次分配,即自动erase?
*/
CV_EXPORTS_W void split(InputArray m, OutputArrayOfArrays mv);
/** @overload
@param mv input vector of matrices to be merged; all the matrices in mv must have the same
size and the same depth.
@param dst output 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.
*/
CV_EXPORTS_W void merge(InputArrayOfArrays mv, OutputArray dst);
2 程序代码
#include "opencv2opencv.hpp" #include <iostream> using namespace std; using namespace cv; int main(int argc, char** argv) { Mat src = imread("G:\CVworkstudy\program_wwx\研习社140课时\ZhaiZhigang140\lena.jpg"); if (src.empty()) { printf("Could not load image... "); return -1; } namedWindow("SrcImg", WINDOW_AUTOSIZE); imshow("SrcImg", src); vector<Mat> mv; Mat dstBlue, dstGreen, dstRed; //分离图片的BGR通道图片至Vector //以下分别对B、G、R进行赋值操作(将对应通道的颜色赋值为0,即去除该颜色),再进行通道合并 //如果分别对B、G、R进行赋值操作(将对应通道的颜色赋值为255,则整个图片将显示偏"BGR"色) split(src, mv); mv[0] = Scalar(0); //mv[1] = Scalar(0);//此时整个图片的BG通道均为0,图片整体颜色偏红R merge(mv, dstBlue); split(src, mv); mv[1] = Scalar(0); merge(mv, dstGreen); split(src, mv); mv[2] = Scalar(0); merge(mv, dstRed); //显示操作结果 imshow("DstB", dstBlue); imshow("DstG", dstGreen); imshow("DstR", dstRed); waitKey(0); return 0; } //inrange可以提取特定颜色
3 运行结果