Does openCV distance transform work for horizontal distance but not for vertical distance ?

asked 2018-12-15 23:44:51 -0600

Esh gravatar image

updated 2018-12-15 23:46:25 -0600

I am using openCV distance transform in python to find maximum width of line segments. When i use it for vertical lines i.e. when the width is horizontal, it works perfectly like this:image description

However, for nearly horizontal lines i.e. when the width is supposed to be vertical, it gives almost 10 times bigger value for an image like this :

image description The code i used :

    output_canvas = np.zeros(image.shape, dtype=np.uint8)
    cv2.drawContours(output_canvas, cnt, i, (255, 255, 255), -1)
    dist = cv2.distanceTransform(output_canvas, cv2.DIST_L2, 3)
    _, mv, _, mp = cv2.minMaxLoc(dist)
    width=mv*2  #(half)width*2
    print(width, mp)  # width, pos
    draw = cv2.cvtColor(output_canvas, cv2.COLOR_GRAY2BGR)
    cv2.line(draw, (0, mp[1]), (output_canvas.shape[1], mp[1]), (0, 0, 200), 3, -1)

How can i make my program calculate width regardless of contour orientation ?

edit retag flag offensive close merge delete

Comments

sorry, but i cannot reproduce your problem.

(i rotated the original image by 90° and get exactly the same values)

berak gravatar imageberak ( 2018-12-16 08:52:15 -0600 )edit