Ask Your Question
0

How to detect the end point of the line from this image

asked 2017-12-29 02:40:13 -0600

NirvanaDon gravatar image

Hey, im trying to detect the end points of the line from this image. And marking a cross at it. Can someone help me?

image description

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-12-29 04:52:29 -0600

berak gravatar image

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)]
edit flag offensive delete link more

Comments

I'm sorry , i been trying very hard to understand the code u given me.. but i still dun quite get it

NirvanaDon gravatar imageNirvanaDon ( 2018-01-04 22:00:23 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-12-29 02:40:13 -0600

Seen: 710 times

Last updated: Dec 29 '17