【python】【code】矩形坐标转化,两个角点与中心点坐标互转。

【python】【code】矩形坐标转化,两个角点与中心点坐标互转。_第1张图片

源码如下

import numpy as np
#  将[x1,y1,x2,y2]转化成[x,y,s,r]
def convert_bbox_to_z(bbox):
    w = bbox[2] - bbox[0]
    h = bbox[3] - bbox[1]
    x = bbox[0] + w / 2.
    y = bbox[1] + w / 2.
    s = w * h
    r = w / float(h)
    return np.array([x,y,s,r]).reshape((4,1))  # 返回四行一列,列向量


# 将[x,y,s,r]转化成[x1,y1,x2,y2]
def convert_x_to_bbox(x,score = None):
    '''
    :param x: [x,y,s,r]
    :param score:  当前候选框的置信度
    :return:
    '''
    w = np.sqrt(x[2]* x[3])
    h = x[2] / w
    x1 = x[0] - w /2.
    y1 = x[1] - h / 2.
    x2 = x[0] + w / 2.
    y2 = x[1] + h /2.
    if score is None:
        return np.array([x1,y1,x2,y2]).reshape((1,4))  # 返回行向量
    else:
        return np.array([x1,y1,x2,y2,score]).reshape((1,5)) # 返回行向量,带置信度

你可能感兴趣的:(python,常用代码模块)