Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

an endpoint of your line has exactly 1 white neighbour, the others have 2.

so, you'll have to check the 8-neighbourhood of each white pixel, and count the white neighbours, if you end up with 1 -- you found an endpoint !

pts = cv2.findNonZero(im)
print(pts.shape)
## (114, 1, 2)
## note the weird, extra dimension in the middle
ends = []
for pt in pts:
    on = 0
    p=pt[0] ## access the point from a 2d array, see above
    ## 1 2 3
    ## 4 * 5
    ## 6 7 8
    if im[p[1]+1,p[0]-1]>0: on +=1
    if im[p[1]+1,p[0]  ]>0: on +=1
    ## student project, -- your task here !
    ... 
    if on==1: ends.append(p)

print (ends)
[array([17, 12], dtype=int32), array([99, 87], dtype=int32)]