C++ 访问 OpenCV 中的每个单独通道

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6699374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:33:47  来源:igfitidea点击:

Access to each separate channel in OpenCV

c++opencv

提问by iampat

I have an image with 3 channels (img) and another one with a single channel (ch1).

我有一个带有 3 个通道 (img) 的图像和另一个带有单个通道 (ch1) 的图像。

    Mat img(5,5,CV_64FC3);
    Mat ch1 (5,5,CV_64FC1);

Is there any efficient way (not using for loop) to copy the first channel of imgto ch1?

是否有任何有效的方法(不使用 for 循环)将img的第一个通道复制到ch1

采纳答案by Jacek

There is a function called cvMixChannels. You'll need to see implementation in the source code, but I bet it is well optimized.

有一个名为cvMixChannels的函数。您需要在源代码中查看实现,但我敢打赌它已得到很好的优化。

回答by orielfrigo

In fact, if you just want to copy one of the channels or split the color image in 3 different channels, CvSplit()is more appropriate (I mean simple to use).

事实上,如果你只是想复制其中一个通道或将彩色图像拆分为 3 个不同的通道,CvSplit()则更合适(我的意思是简单易用)。

Mat img(5,5,CV_64FC3);
Mat ch1, ch2, ch3;
// "channels" is a vector of 3 Mat arrays:
vector<Mat> channels(3);
// split img:
split(img, channels);
// get the channels (dont forget they follow BGR order in OpenCV)
ch1 = channels[0];
ch2 = channels[1];
ch3 = channels[2];

回答by techguy18985

You can use split function and then put zeros to the channels u want to ignore. This will result dispalying one channels out of three. See below..

您可以使用拆分功能,然后将零放在您想要忽略的通道上。这将导致显示三个频道中的一个。见下文..

For example:

例如:

Mat img, chans[3]; 
img = imread(.....);  //make sure its loaded with an image

//split the channels in order to manipulate them
split(img, chans);

//by default opencv put channels in BGR order , so in your situation you want to copy the first channel which is blue. Set green and red channels elements to zero.
chans[1]=Mat::zeros(img.rows, img.cols, CV_8UC1); // green channel is set to 0
chans[2]=Mat::zeros(img.rows, img.cols, CV_8UC1);// red channel is set to 0

//then merge them back
merge(chans, 3, img);

//display 
imshow("BLUE CHAN", img);
cvWaitKey();

回答by Gralex

You can access to specific channel, it works faster that splitoperation

您可以访问特定频道,split操作 速度更快

Mat img(5,5,CV_64FC3);
Mat ch1;
int channelIdx = 0;
extractChannel(img, ch1, channelIdx); // extract specific channel

// or extract them all
vector<Mat> channels(3);
split(img, channels);
cout << channels[0].size() << endl;

回答by jmartel

A simpler one if you have a RGB with 3 channels is cvSplit() if i'm not wrong, you have less to configure... (and i think it is also well optimized).

如果你有一个带有 3 个通道的 RGB,一个更简单的方法是 cvSplit() 如果我没有错的话,你需要配置的更少......(我认为它也得到了很好的优化)。

I would use cvMixChannel() for "harder" tasks... :p (i know i am lazy).

我会使用 cvMixChannel() 来完成“更难”的任务...... :p(我知道我很懒)。

here is the documentation for cvSplit()

这是 cvSplit() 的文档