Ask Your Question

Using openCV distance transform to find width of a curve

asked 2018-11-30 01:14:01 -0500

Esh gravatar image

updated 2018-11-30 01:29:04 -0500

berak gravatar image

I am trying to find width of a curve/line segment using openCV in python. The image on the left is the input image. I just need the maximum width. I read that Distance transform can help give the width. I used distance transform and was able to get the image on the right as a result. Now, I am stuck on how do i get the width of the curve from this result image of distance transform.

My code for distance transform:



A solution in python would be highly appreciated. Thanks in advance.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-11-30 02:23:33 -0500

berak gravatar image

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

edit flag offensive delete link more



@berak Thank You. Exactly what i was looking for :)

Esh gravatar imageEsh ( 2018-11-30 21:48:15 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-11-30 01:14:01 -0500

Seen: 33 times

Last updated: Nov 30