Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

normalizing the distance is a bad idea already, since the max value will always be 1.

then, also remember, that the distanceTransform gives you the shortest distance to any zero pixel in the neighbourhood, so it's value is only half of the actual line's width.

to find the point with the max.distance, use minMaxLoc():

dist = cv2.distanceTransform(img, cv2.DIST_L2, 3)
_,mv,_,mp = cv2.minMaxLoc(dist)
print(mv*2, mp) # (half)width*2, pos
draw = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
cv2.line(draw, (0,mp[1]), (img.shape[1],mp[1]), (0,0,200), 3, -1)
cv2.imshow("dist", draw)

16.108600616455078 (96, 57)

image description