问题描述
我有一些代码,主要取自这篇文章底部链接的各种来源,用 Python 编写,它获取形状为 [height,width]
的图像和一些 [x_min,y_min,x_max,y_max]
格式的边界框,两者都是numpy 数组,并逆时针旋转图像及其边界框。由于旋转后边界框变得更像“菱形”,即未轴对齐,因此我执行了一些计算以使其轴对齐。此代码的目的是通过使用旋转数据(其中水平或垂直翻转很常见)在训练对象检测神经网络时执行数据增强。看起来其他角度的翻转对于图像分类来说很常见,没有边界框,但是当有框时,如何翻转框以及图像的资源相对稀疏/利基。
似乎当我输入 45 度角时,我得到了一些不太“紧密”的边界框,因为四个角不是一个很好的注释,而原始的接近完美。
下图是 MS COCO 2014 对象检测数据集(训练图像)中的第一张图像,及其第一个边界框注释。我的代码如下:
import math
import cv2
import numpy as np
# angle assumed to be in degrees
# bbs a list of bounding Boxes in x_min,y_max format
def rotateImageAndBoundingBoxes(im,bbs,angle):
h,w = im.shape[0],im.shape[1]
(cX,cY) = (w//2,h//2) # original image center
M = cv2.getRotationMatrix2D((cX,cY),angle,1.0) # 2 by 3 rotation matrix
cos = np.abs(M[0,0])
sin = np.abs(M[0,1])
# compute the dimensions of the rotated image
nW = int((h * sin) + (w * cos))
nH = int((h * cos) + (w * sin))
# adjust the rotation matrix to take into account translation of the new centre
M[0,2] += (nW / 2) - cX
M[1,2] += (nH / 2) - cY
rotated_im = cv2.warpAffine(im,M,(nW,nH))
rotated_bbs = []
for bb in bbs:
# get the four rotated corners of the bounding Box
vec1 = np.matmul(M,np.array([bb[0],bb[1],1],dtype=np.float64)) # top left corner transformed
vec2 = np.matmul(M,np.array([bb[2],dtype=np.float64)) # top right corner transformed
vec3 = np.matmul(M,bb[3],dtype=np.float64)) # bottom left corner transformed
vec4 = np.matmul(M,dtype=np.float64)) # bottom right corner transformed
x_vals = [vec1[0],vec2[0],vec3[0],vec4[0]]
y_vals = [vec1[1],vec2[1],vec3[1],vec4[1]]
x_min = math.ceil(np.min(x_vals))
x_max = math.floor(np.max(x_vals))
y_min = math.ceil(np.min(y_vals))
y_max = math.floor(np.max(y_vals))
bb = [x_min,y_max]
rotated_bbs.append(bb)
// my function to resize image and bbs to the original image size
rotated_im,rotated_bbs = resizeImageAndBoxes(rotated_im,w,h,rotated_bbs)
return rotated_im,rotated_bbs
不太好的边界框看起来像:
我正在尝试确定这是我的代码错误还是预期行为?看起来这个问题在 pi/2 弧度(90 度)的整数倍时不太明显,但我想在任何旋转角度都实现紧密的边界框。任何见解都表示赞赏。
来源: [打开 CV2 文档] https://docs.opencv.org/3.4/da/d54/group__imgproc__transform.html#gafbbc470ce83812914a70abfb604f4326
【数据增强讨论】 https://blog.paperspace.com/data-augmentation-for-object-detection-rotation-and-shearing/
[绕二维任意点旋转的数学] https://math.stackexchange.com/questions/2093314/rotation-matrix-of-rotation-around-a-point-other-than-the-origin
解决方法
在大多数情况下,这似乎是评论中的预期行为。我确实有一个解决这个问题的hacky解决方案,你可以写一个像
这样的函数# assuming box coords = [x_min,y_min,x_max,y_max]
def cropBoxByPercentage(box_coords,image_width,image_height,x_percentage=0.05,y_percentage=0.05):
box_xmin = box_coords[0]
box_ymin = box_coords[1]
box_xmax = box_coords[2]
box_ymax = box_coords[3]
box_width = box_xmax-box_xmin+1
box_height = box_ymax-box_ymin+1
dx = int(x_percentage * box_width)
dy = int(y_percentage * box_height)
box_xmin = max(0,box_xmin-dx)
box_xmax = min(image_width-1,box_xmax+dx)
box_ymin = max(0,box_ymax - dy)
box_ymax = min(image_height - 1,box_ymax + dy)
return np.array([box_xmin,box_xmax,box_ymin,box_ymax])
x_percentage 和 y_percentage 的计算可以使用固定值计算,也可以使用启发式计算。