我的应用程序:我正在尝试旋转图像(使用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) 我需要进行数据扩充,但不需要任何填充模式,constant, reflect, nearest, wrap。代替每次图像旋转或平移,我想有它中心裁剪(如下所示),从而不具有任何黑,白,反射,或恒定的边缘/边界所解释这里。
如何在ImageDataGenerator考虑这些点的情况下扩展类(如果这是唯一的方法并且没有开箱即用的中心裁剪)?
保留ImageDataGenerator 的现有部分而不是增强部分,并编写自定义增强功能
在增强发生之前保留原始尺寸的图像而不调整大小将是有效的,因为中心裁剪会导致调整大小后大量数据丢失。Translate/Rotate -> Center crop -> Resize应该比Resize -> Translate/Rotate -> Center crop