# Using openCV distance transform to find width of a curve

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:

dist=cv2.distanceTransform(image,cv2.DIST_L2,3)
cv2.normalize(dist,dist,0,1.0,cv2.NORM_MINMAX)


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

edit retag close merge delete

Sort by ยป oldest newest most voted

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)
cv2.waitKey()

16.108600616455078 (96, 57)


more

1

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

( 2018-11-30 21:48:15 -0600 )edit

Official site

GitHub

Wiki

Documentation