Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

opencv

mat

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.

like image 393
Lakshmi Narayanan Avatar asked Jun 28 '13 11:06

Lakshmi Narayanan


People also ask

How can I find out the number of channels in an image?

If len(img. shape) gives you three, third element gives you number of channels.

What is number of channels in OpenCV?

There are three channels in an RGB image- red, green and blue. The color space where red, green and blue channels represent images is called RGB color space. In OpenCV, BGR sequence is used instead of RGB. This means the first channel is blue, the second channel is green, and the third channel is red.

What is use of mat class in OpenCV?

The Mat class of OpenCV library is used to store the values of an image. It represents an n-dimensional array and is used to store image data of grayscale or color images, voxel volumes, vector fields, point clouds, tensors, histograms, etc.

What is Vec3b OpenCV?

Vec3b is the abbreviation for "vector with 3 byte entries" Here those byte entries are unsigned char values to represent values between 0 .. 255.


2 Answers

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.

like image 174
LovaBill Avatar answered Sep 17 '22 15:09

LovaBill


you might use:

Mat::channels() 

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

like image 36
hetepeperfan Avatar answered Sep 20 '22 15:09

hetepeperfan