如何在Python中使用cv :: RotatedRect

时间:2019-04-24 08:00:15

标签: python opencv

我想在Python中使用cv::RotatedRect。但是我找不到它的名称空间。帮助将不胜感激!

编辑:

我需要使用它来实现this

1 个答案:

答案 0 :(得分:0)

自己计算旋转的img的大小

def rotate(img, degree):
    h, w = img.shape[:2]
    center = (w // 2, h // 2)

    dst_h = int(w * math.fabs(math.sin(math.radians(degree))) + h * math.fabs(math.cos(math.radians(degree))))
    dst_w = int(h * math.fabs(math.sin(math.radians(degree))) + w * math.fabs(math.cos(math.radians(degree))))

    matrix = cv2.getRotationMatrix2D(center, degree, 1)
    matrix[0, 2] += dst_w // 2 - center[0]
    matrix[1, 2] += dst_h // 2 - center[1]
    dst_img = cv2.warpAffine(img, matrix, (dst_w, dst_h), borderValue=(255, 255, 255))
    return dst_img