相关疑难解决方法(0)

用opencv找到手绘线的端点

我试图找到手绘线的两个端点,我已经编写了这个找到轮廓的片段,但端点不正确:

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)
Run Code Online (Sandbox Code Playgroud)

原图:

在此输入图像描述

我也尝试过cornerHarris,但它给了我一些加分,

有人可以建议一个准确且更好的方法吗?

python opencv image-processing

6
推荐指数
2
解决办法
2366
查看次数

标签 统计

image-processing ×1

opencv ×1

python ×1