Detect holes, ends and beginnings of a line using openCV?

纵饮孤独 提交于 2019-12-01 18:41:45

I added a func getLandmarks() it returns all the wholes. So here I assume that it will be counted as a hole if there are 2 corners in a radius of 30 pix

if abs(x1-x2)<=30 and abs(y1-y2)<=30:

This line defines the range.

import cv2
import numpy as np

def getLandmarks(corners):
    holes=[]
    for i in range(0,len(corners)):
        for j in range(i+1,len(corners)):
            x1,y1=corners[i].ravel()
            x2,y2=corners[j].ravel()
            if abs(x1-x2)<=30 and abs(y1-y2)<=30:
                holes.append((int((x1+x2)/2),int((y1+y2)/2)))
    return holes

# lodes in img

img = cv2.imread('img.png', cv2.IMREAD_COLOR)
img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

corners = cv2.goodFeaturesToTrack(img_gray, 200, 0.05, 10)

holes=getLandmarks(corners)
print len(holes)
for corner in holes:
    cv2.circle(img, (corner), 7, (255,255,0), -1)

cv2.imshow('img',img)
cv2.waitKey(0)

output

Now for the Start and end You can easily sort the corners in either X(if the Path is along left to right) or Y(If the path is along top to down) and the min and max will be your start and end!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!