Can I determine the number of channels in cv::Mat Opencv 的答案为 OpenCV 1 回答了这个问题:你使用Mat.channels()
图像的方法。
但是在cv2(我使用的是2.4.6)中,我拥有的图像数据结构没有channels()
方法。我正在使用 Python 2.7。
代码片段:
cam = cv2.VideoCapture(source)
ret, img = cam.read()
# Here's where I would like to find the number of channels in img.
互动尝试:
>>> img.channels()
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
AttributeError: 'numpy.ndarray' object has no attribute 'channels'
>>> type(img)
<type 'numpy.ndarray'>
>>> img.dtype
dtype('uint8')
>>> dir(img)
['T',
'__abs__',
'__add__',
...
'transpose',
'var',
'view']
# Nothing obvious that would expose the number of channels.
谢谢你的帮助。