title: How to mask a video frame using contours with opencv in python
body: <p>I am using opencv (cv2 module) in python to recognize objects in a video. In each frame, I want to extract a particular region aka, the contour. After learning from <a href="https://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_imgproc/py_histograms/py_histogram_begins/py_histogram_begins.html?highlight=calchist" rel="nofollow">opencv docs</a>, I have the following code snippet:</p>
  
  <pre><code>        # np is numpy module, contours are expected results,
          # frame is each frame of the video
  
          # Iterate through the contours.
          for contour in contours:
              # Compute the bounding box for the contour, draw
              # it on the frame, and update the text.
              x, y, w, h = cv2.boundingRect(contour)
  
              # Find the mask and build a histogram for the object.
              mask = np.zeros(frame.shape[:2], np.uint8)
              mask[y:h, x:w] = 255
              masked_img = cv2.bitwise_and(frame, frame, mask = mask)
              obj_hist = cv2.calcHist([masked_img], [0], None, [256], [0, 256])
  </code></pre>
  
  <p>However, when I use <code>matplotlib</code> to show the <code>masked_img</code>, it returns a dark image. The <code>obj_hist</code> has only one element with number greater than <code>0</code>, which is the first one. What is wrong?</p>