C++ 我可以确定 cv::Mat Opencv 中的通道数吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17363886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I determine the number of channels in cv::Mat Opencv
提问by Lakshmi Narayanan
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.
这可能是基本的,但是否有可能知道 acv::Mat
有多少个频道?例如,我们加载一个 RGB 图像,我知道有 3 个通道。我做了以下操作,只是为了得到图像的拉普拉斯算子,直接来自 Opencv 文档。
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.
转换为灰度后,我们应该只有一个通道。但是如何确定abs_dst_gray
节目中的频道数?有什么功能可以做到这一点吗?或者是否可以通过任何其他方法,应该由程序员编写?请在这里帮助我。
Thanks in advance.
提前致谢。
回答by LovaBill
Call Mat.channels()
:
电话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 hereat line 542). Study file types_c.h
for each define
.
属于 CV_8U(请看这里的第 542 行)。研究文件types_c.h
每个define
。
回答by hetepeperfan
you might use:
你可能会使用:
Mat::channels()
http://docs.opencv.org/modules/core/doc/basic_structures.html#mat-channels
http://docs.opencv.org/modules/core/doc/basic_structures.html#mat-channels