Finding the end points of a hand drawn line with opencv
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:
img = cv2.imread("my_img.jpeg")
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="after threshold", 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("Contour #", 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="contour" + str(idx), mat=img)
cv2.waitKey(0)
Original image:
I have also tried cornerHarris but it gave me some extra points,
Can someone please suggest an accurate and better approach?
