title: OpenCV calculate the area
body: <p>I'm trying to calculate the area of what 'see' my camera. I need it to decide if the object is close or not (to control my robot). In the old version I've got this code:</p>
  
  <pre><code>moments = cv.Moments(mat, 0)
  area = cv.GetCentralMoment(moments, 0, 0)
  moment10 = cv.GetSpatialMoment(moments, 1, 0)
  moment01 = cv.GetSpatialMoment(moments, 0,1)
  </code></pre>
  
  <p>and everything was working, but now i want to do this with the nev api (cv2). This is my code:</p>
  
  <pre><code>mask = cv2.inRange(hsv, lower_red, upper_red)
  
  ret,thresh = cv2.threshold(mask,127,255,0)
  
  image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
  cnt = contours[0]
  M = cv2.moments(cnt)
  
  area = cv2.contourArea(cnt)
  </code></pre>
  
  <p>and I'm gettin the area = 0. </p>
  
  <p>Or maybe instead of calculating the area the better solution is to draw a rectangle around the white area and then calculate its dimensions?</p>
  
  <p><img src="https://i.stack.imgur.com/gR7uI.png" alt=""></p>