首页 > 解决方案 > 如何让轮廓第一个坐标始终位于左上角?

问题描述

好的,所以我的轮廓总是矩形或正方形,所以我每个只有 4 个坐标([top_left,bottom_left,bottom_right,top_right])。但是对于某些轮廓,第一个坐标不是 top_left 上的坐标,它可以从 4 个角中的任何一个开始。我怎样才能让他们每次都从一个角落开始?

以下是数字代表坐标顺序的图片: https ://i.imgur.com/bPt7Wah.png

我使用这些坐标来裁剪图像,但如果坐标的顺序不正确,它就不起作用。这是裁剪的代码:

def get_colors(img, squares):
    for square in squares:
        x0, y0 = square[0]
        x1, y1 = square[1]
        x2, y2 = square[2]
        x3, y3 = square[3]

        top_left_x = max([x0, x1])
        top_left_y = max([y0, y3])
        bot_right_x = min([x2, x3])
        bot_right_y = min([y1, y2])

        croped = img[top_left_y:bot_right_y, top_left_x:bot_right_x]

标签: pythonnumpyopencv

解决方案


推荐阅读