我的应用程序:我正在尝试旋转图像(使用OpenCV和Python)

目前我开发了以下代码,用于旋转输入图像,用黑色边框填充它,给我A.我想要的是B - 旋转图像中最大可能区域裁剪窗口.我称之为轴对齐的边界框.
这与旋转和裁剪基本相同,但是我无法得到关于该问题的答案.此外,该答案显然仅对方形图像有效.我的图像是矩形的.
代码给A:
import cv2
import numpy as np
def getTranslationMatrix2d(dx, dy):
"""
Returns a numpy affine transformation matrix for a 2D translation of
(dx, dy)
"""
return np.matrix([[1, 0, dx], [0, 1, dy], [0, 0, 1]])
def rotateImage(image, angle):
"""
Rotates the given image about it's centre
"""
image_size = (image.shape[1], image.shape[0])
image_center = tuple(np.array(image_size) / 2)
rot_mat = np.vstack([cv2.getRotationMatrix2D(image_center, angle, 1.0), [0, 0, 1]])
trans_mat = np.identity(3)
w2 = image_size[0] * 0.5
h2 …Run Code Online (Sandbox Code Playgroud)