Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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 ?

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

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 ?

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

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 ?