访问OpenCV中的每个单独通道

时间:2022-07-23 19:41:38

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 img to ch1?

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

4 个解决方案

#1


11  

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

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

#2


45  

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];

#3


9  

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,channel);

//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();

#4


2  

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()的文档

#1


11  

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

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

#2


45  

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];

#3


9  

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,channel);

//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();

#4


2  

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()的文档