Can I determine the number of channels in cv::Mat Opencv
Asked Answered
I

2

33

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.

Inhospitality answered 28/6, 2013 at 11:39 Comment(1)
Have a look at the anser i gave in another thread. I explained how to get the cannels and type of a matrix and how you can access single elements afterwards: #7399844Martainn
C
65

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.

Cartierbresson answered 28/6, 2013 at 11:55 Comment(5)
This gives the data type @William. How do I get the no.of channels?Inhospitality
Of course. Everybody here are willing to help, @LakshmiNarayanan. Just post, and if I have some free time, I'll give it a try.Cartierbresson
@LakshmiNarayanan Post the other doubt in a new question please.Kenleigh
@Kenleigh I have asked it here #17365487 Please bare with me if its pretty rudimentary.Inhospitality
William's link seems dead. Here's a substitute: ninghang.blogspot.co.uk/2012/11/list-of-mat-type-in-opencv.htmlHurwitz
K
14

you might use:

Mat::channels()

http://docs.opencv.org/modules/core/doc/basic_structures.html#mat-channels

Kenleigh answered 28/6, 2013 at 11:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.