title: How to accurately get the contour of a shape in an image?
body: <p>I'm having black and white 93x94 pixel images with geometric shapes in them. I'm trying to find the shapes contours as well as their center</p>
  
  <p>This is what I tried so far</p>
  
  <pre class="lang-py prettyprint-override"><code>import cv2
  import imutils
  
  img = cv2.imread("input.jpg", cv2.IMREAD_GRAYSCALE)
  blurred = cv2.GaussianBlur(img, (5, 5), 0)
  thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]
  cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
      cv2.CHAIN_APPROX_SIMPLE)
  cnts = imutils.grab_contours(cnts)
  for c in cnts:
      # compute the center of the contour
      M = cv2.moments(c)
      cX = int(M["m10"] / M["m00"])
      cY = int(M["m01"] / M["m00"])
  
      # draw the contour and center of the shape on the image
      cv2.drawContours(img, [c], -1, (0, 255, 0), 2)
      cv2.circle(img, (cX, cY), 7, (0, 0, 0), -1)
      #cv2.putText(img, "center", (cX - 20, cY - 20),
          #cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2)
  
      # show the image
      cv2.imshow('output image',img)
      cv2.waitKey(0)
  
  </code></pre>
  
  <p>Here is a sample input image and output image with given contour and sample</p>
  
  <p>Original image:</p>
  
  <p><img src="https://i.stack.imgur.com/pPWWY.jpg" alt="original image"></p>
  
  <p>Output image:</p>
  
  <p><img src="https://i.stack.imgur.com/brYMI.png" alt="Output image"></p>
  
  <p>As you can see, instead of getting the contour and center of the shape, I'm getting those of the bounding box.</p>
  
  <p>Any idea on how to fix this?</p>