Ask Your Question
0

Using openCV distance transform to find width of a curve

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

Esh gravatar image

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

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:

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

Image

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
2

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

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

16.108600616455078 (96, 57)

image description

edit flag offensive delete link more

Comments

1

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

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

Question Tools

1 follower

Stats

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

Seen: 2,956 times

Last updated: Nov 30 '18