title: openCV: detecting a circle using findContours
body: <p>I'm working on a program where I should detect shapes of the same type, and color each type with different color.</p>
  
  <p>I'm using <code>cv2.findCountours</code> and then <code>cv2.approxPolyDP</code> to detect each shape.</p>
  
  <p>The program detects any shape with 8 edges as a circle, therfore I decided to add some examination - I'm checking the area of the current contour using <code>cv2.contourArea</code>, and I'm also checking the area of <code>cv2.minEnclosingCircle(cnt)</code> of the current contour.</p>
  
  <p>If they are equal, we have a circle.</p>
  
  <pre><code>import numpy as np
  import cv2
  
  img = cv2.imread('1.jpg')
  gray = cv2.imread('1.jpg',0)
  
  ret,thresh = cv2.threshold(gray,127,255,1)
  
  contours,h = cv2.findContours(thresh,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_NONE)
  
  for cnt in contours:
      approx = cv2.approxPolyDP(cnt, .03 * cv2.arcLength(cnt, True), True)
      print len(approx)
      if len(approx)==3:
          print "triangle"
          cv2.drawContours(img,[cnt],0,(122,212,78),-1)
      elif len(approx)==4:
          print "square"
          cv2.drawContours(img,[cnt],0,(94,234,255),-1)
      elif len(approx)==8:
          area = cv2.contourArea(cnt)
          (cx, cy), radius = cv2.minEnclosingCircle(cnt)
          circleArea = radius * radius * np.pi
          print circleArea
          print area
          if circleArea == area:
              cv2.drawContours(img, [cnt], 0, (220, 152, 91), -1)
  
  
  cv2.imshow('img',img)
  cv2.waitKey(0)
  cv2.destroyAllWindows()
  </code></pre>
  
  <p>I printed each of the areas and saw that the results are different - 
  even when the shape is clearly a circle.</p>
  
  <p>For example, for the same shape I got 628.254637106 for the minEnclosingCircle area, and 569 for the contourArea.
  Another example: 2220.55512328 for the minEnclosingCircle area, and 2032.0 for the contourArea.</p>
  
  <p>How can I calculate this area properly?</p>
  
  <p>I will appreciate any help!</p>
  
  <p>The image I used: 
  <img src="https://i.stack.imgur.com/s0a17.jpg" alt=""></p>
  
  <p>and the shapes detected:
  <img src="https://i.stack.imgur.com/V91Wp.jpg" alt=""></p>