Code records

Python OpenCV

# resize image to model input
# (height, width, 3): shape of matrix read from w*h image
# (x, y): points in OpenCV/PIL/COCO, x is the width-index, y is the height-index to left-up origin
def get_affine_transform(input_size, output_size, rot=0, inv=0):
    src_h = input_size[0]
    src_w = input_size[1]
    dst_h = output_size[0]
    dst_w = output_size[1]

    rot_rad = np.pi * rot / 180

    src = np.zeros((3, 2), dtype=np.float32)
    dst = np.zeros((3, 2), dtype=np.float32)


    src[0, :] = [src_w/2, src_h/2]
    dst[0, :] = [dst_w/2, dst_h/2]
    src[1, :] = [0, 0]
    # dst[1, :] = [0, 0]
    dst[1, :] = get_dir([-dst_w/2, -dst_h/2], rot_rad) + np.array([dst_w/2, dst_h/2])
    src[2, :] = [0, src_h]
    # dst[2, :] = [0, dst_h]
    dst[2, :] = get_dir([-dst_w/2, dst_h/2], rot_rad) + np.array([dst_w/2, dst_h/2])

    if inv:
        trans = cv2.getAffineTransform(np.float32(dst), np.float32(src))
    else:
        trans = cv2.getAffineTransform(np.float32(src), np.float32(dst))

    return trans

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章