Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

c++ - Can I determine the number of channels in cv::Mat Opencv

This maybe rudimentary, but is it possible to know how many channels a cv::Mat has? For eg, we load an RGB image, I know there are 3 channels. I do the following operations, just to get the laplacian of the image, which is straight from the Opencv Documentation.

int main(int argc, char **argv)
{
     Mat src = imread(argv[1],1),src_gray,dst_gray,abs_dst_gray;

     cvtColor(src,src_gray,COLOR_BGR2GRAY);
     GaussianBlur( src, src, Size(3,3), 0, 0, BORDER_DEFAULT );
     Laplacian(src_gray,dst_gray,ddepth,kernel_size,scale,delta,BORDER_DEFAULT);
     convertScaleAbs(dst_gray,abs_dst_gray);
}

After converting to Grayscale, we should have only one channel. But how can I determine the number of channels of abs_dst_gray in program? Is there any function to do this? Or is it possible through any other method, which should be written by the programmer? Please help me here.

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Call Mat.channels() :

cv::Mat img(1,1,CV_8U,cvScalar(0));
std::cout<<img.channels();

Output:

1

which is the number of channels.

Also, try:

std::cout<<img.type();

Output:

0

which belongs to CV_8U (look here at line 542). Study file types_c.h for each define.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...