【code】四坐标点位排序

sortCoordinate

排序坐标,使得坐标符合(x_min,y_min),(x_max,y_min),(x_max,y_max),(x_min,y_max)

def sortCoordinate(box):

    '''
    :param box: x,y坐标共4点8值;[x1, y1, x2, y2, x3, y3, x4, y4]
    x1y1->x2y2->x3y3-x4y4
    top_left->top_right->bottom_right->bottom_letft
    (x_min,y_min),(x_max,y_min),(x_max,y_max),(x_min,y_max)
    ------------------
    | *********      |
    |  *********     |
    |   *********    |
    |    *********   |
    |     *********  |
    ------------------
    :return: [[x1, y1], [x2, y2], [x3, y3], [x4, y4]]
    '''

    x1, y1, x2, y2, x3, y3, x4, y4 = box[:8]
    newBox = [[x1, y1], [x2, y2], [x3, y3], [x4, y4]]
    
    # 按第一维度正序排序[x_min,x_min,x_max,x_max]
    newBox = sorted(newBox, key=lambda x: x[0],reverse=False) 
    # 排序后,前两个是左边的上下两个坐标点x_min,按第二维排序,使得y_min最小的在前面
    x1, y1 = sorted(newBox[:2], key=lambda x: x[1])[0] 
    index = newBox.index([x1, y1]) # 取出(x_min,y_min)
    newBox.pop(index)
    
    newBox = sorted(newBox, key=lambda x: x[1],reverse=True)
    x4, y4 = sorted(newBox[:2], key=lambda x: x[0])[0]
    index = newBox.index([x4, y4])
    newBox.pop(index)
    
    newBox = sorted(newBox, key=lambda x: x[0],reverse=True)
    x2, y2 = sorted(newBox[:2], key=lambda x: x[1])[0]
    index = newBox.index([x2, y2])
    newBox.pop(index)

    newBox = sorted(newBox, key=lambda x: x[1],reverse=True)
    x3, y3 = sorted(newBox[:2], key=lambda x: x[0])[0]

    res = [[x1, y1], [x2, y2], [x3, y3], [x4, y4]]
    
    return res
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值