title: how to get the number of channels from an image, in OpenCV 2?
body: <p>The answers at <a href="https://stackoverflow.com/questions/17363886/can-i-determin-the-number-of-channels-in-cvmat-opencv">Can I determine the number of channels in cv::Mat Opencv</a> answer this question for OpenCV 1: you use the <code>Mat.channels()</code> method of the image.</p>
  
  <p>But in cv2 (I'm using 2.4.6), the image data structure I have doesn't have a <code>channels()</code> method. I'm using Python 2.7.</p>
  
  <p>Code snippet:</p>
  
  <pre><code>cam = cv2.VideoCapture(source)
  ret, img = cam.read()
  # Here's where I would like to find the number of channels in img.
  </code></pre>
  
  <p>Interactive attempt:</p>
  
  <pre><code>&gt;&gt;&gt; img.channels()
  Traceback (most recent call last):
    File "&lt;interactive input&gt;", line 1, in &lt;module&gt;
  AttributeError: 'numpy.ndarray' object has no attribute 'channels'
  &gt;&gt;&gt; type(img)
  &lt;type 'numpy.ndarray'&gt;
  &gt;&gt;&gt; img.dtype
  dtype('uint8')
  &gt;&gt;&gt; dir(img)
  ['T',
   '__abs__',
   '__add__',
  ...
   'transpose',
   'var',
   'view']
  # Nothing obvious that would expose the number of channels.
  </code></pre>
  
  <p>Thanks for any help.</p>