title: Finding the end points of a hand drawn line with opencv
body: <p>I'm trying to find the two end points of a hand drawn line
  I have written this snippet which finds the contours,
  but the end points is not correct:</p>
  <pre><code>img = cv2.imread(&quot;my_img.jpeg&quot;)
  
  img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  
  # Binary Threshold:
  _, thr_img = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
  
  cv2.imshow(winname=&quot;after threshold&quot;, mat=thr_img)
  cv2.waitKey(0)
  
  contours, _ = cv2.findContours(image=thr_img, mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_SIMPLE)
  
  for idx, cnt in enumerate(contours):
      print(&quot;Contour #&quot;, idx)
      cv2.drawContours(image=img, contours=[cnt], contourIdx=0, color=(255, 0, 0), thickness=3)
      cv2.circle(img, tuple(cnt[0][0]), 5, (255, 255, 0), 5) # Result in wrong result
      cv2.circle(img, tuple(cnt[-1][0]), 5, (0, 0, 255), 5)  # Result in wrong result
      cv2.imshow(winname=&quot;contour&quot; + str(idx), mat=img)
      cv2.waitKey(0)
  </code></pre>
  <p>Original image:</p>
  <p><a href="https://i.stack.imgur.com/hJVBX.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hJVBX.jpg" alt="enter image description here" /></a></p>
  <p>I have also tried <code>cornerHarris</code> but it gave me some extra points,</p>
  <p>Can someone please suggest an accurate and better approach?</p>