opencv 之 mixChannels

Copies specified channels from input arrays to the specified channels of output arrays.

C++: void mixChannels(const Mat* src, int nsrc, Mat* dst, int ndst, const int* fromTo, size_t npairs)

Parameters:


  • src
    – Input array or vector of matrices. All the matrices must have the same size and the same depth.
  • nsrc – Number of matrices in src .
  • dst – Output array or vector of matrices. All the matrices must be allocated . Their size and depth must be the same as in src[0] .
  • ndst – Number of matrices in dst .
  • fromTo – Array of index pairs specifying which channels are copied and where. fromTo[k*2] is a 0-based index of the input channel in src . fromTo[k*2+1] is an index of the output channel in dst . The continuous channel numbering is used: the first input image channels are indexed from 0 tosrc[0].channels()-1 , the second input image channels are indexed from src[0].channels() tosrc[0].channels() + src[1].channels()-1, and so on. The same scheme is used for the output image channels. As a special case, when fromTo[k*2] is negative, the corresponding output channel is filled with zero .
  • npairs – Number of index pairs in fromTo.

 

In the example below, the code splits a 4-channel RGBA image into a 3-channel BGR (with R and B channels swapped) and a separate alpha-channel image:

Mat rgba( 3, 4, CV_8UC4, Scalar(1,2,3,4) );
Mat bgr( rgba.rows, rgba.cols, CV_8UC3 );
Mat alpha( rgba.rows, rgba.cols, CV_8UC1 );

// forming an array of matrices is a quite efficient operation,
// because the matrix data is not copied, only the headers
Mat out[] = { bgr, alpha };
// rgba[0] -> bgr[2], rgba[1] -> bgr[1],
// rgba[2] -> bgr[0], rgba[3] -> alpha[0]
int from_to[] = { 0,2, 1,1, 2,0, 3,3 };
mixChannels( &rgba, 1, out, 2, from_to, 4 );


运行结果:

改编的代码:

					Mat rgba( 3, 4, CV_8UC4, Scalar(1,2,3,4) );
					Mat bgr( rgba.rows, rgba.cols, CV_8UC3 );
					Mat alpha( rgba.rows, rgba.cols, CV_8UC1 );
					Mat a( rgba.rows, rgba.cols, CV_8UC1 );
					Mat b( rgba.rows, rgba.cols, CV_8UC1 );
					Mat c( rgba.rows, rgba.cols, CV_8UC1 );;
					Mat d( rgba.rows, rgba.cols, CV_8UC1 );

					Mat out[] = { a,b,c,d };
					int from_to[]={0,0,1,1,2,2,3,3};
					mixChannels( &rgba, 1,out, 4, from_to, 4 );
					cout<<"rgba: "<<endl<<rgba<<endl;
					cout<<"a: "<<endl<<a<<endl;
					cout<<"b: "<<endl<<b<<endl;
					cout<<"c: "<<endl<<c<<endl;
					cout<<"d: "<<endl<<d<<endl;


运行结果:

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值