2011-07-14 25 views
19

我有一个带有3个通道(img)的图像,另一个带有一个通道(ch1)。访问OpenCV中的每个独立通道

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

有没有什么有效的方法(不使用for循环)来复制IMG的CH1第一通道?

回答

9

有一个叫做cvMixChannels的功能。你需要在源代码中看到实现,但我敢打赌它已经很好的优化了。

+0

谢谢亚切克。看来,我可以使用mixChannels完成我的任务 – iampat

+0

谢谢你的回答。我尝试了split和mixChannels。而且,他们会抛出错误!你有什么想法,可能是什么原因? – iampat

+0

告诉我什么是错误。 – Jacek

2

一个简单的,如果你有一个RGB 3通道是cvSplit()如果我没有错,你有更少的配置...(我认为它也是很好的优化)。

我会使用cvMixChannel()进行“更难”的任务...:p(我知道我很懒惰)。

here is the documentation for cvSplit()

42

事实上,如果你只是想复制一个信道或在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]; 
7

你可以使用分割功能,然后把零置入你想忽略的通道。这将导致显示三个频道中的一个频道。见下文..

例如:

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(); 
+1

...和回答这个已经有一个被接受的答案的这个3岁的问题的目的是...? – rayryeng

+4

这是一个更好的答案,并通过在其中添加补充函数cv :: merge()来提高最多投票的答案(这不是公认的答案)(尽管也许只是编辑其他答案会更好,但用户缺乏重要点?) –