【发布时间】:2023-01-21 21:10:07
【问题描述】:
我正在研究数据扩充,我正在尝试生成数据集中每个图像的合成版本。所以我需要旋转图像以及图像中的边界框。
我只会将图像旋转 90、180、270 度。
我正在使用 here 所示的 pascal-voc 注释格式。结果我有以下信息。
x_min, y_min, x_max, y_max。图像的来源(我可以从图像大小中得到它)
我已经搜索了很多。但我找不到任何旋转边界框(或矩形)的解决方案
我试过这样的事情; 我从here 那里得到了这个解决方案,并尝试对其进行调整但没有奏效。
def rotateRect(bndbox, img_size, angle):
angle = angle * math.pi/180 # conversion from degree to radian
y_min, y_max, x_min, x_max = bndbox
ox, oy = img_size[0]/2, img_size[1]/2 # coordinate of origin of image
rect = [[x_min, y_min], [x_min, y_max],[x_max, y_min],[x_max, y_max]] # coordinates of points of corners of bounding box rectangle.
nrp = [[0, 0], [0,0 ],[0,0],[0, 0]] #new rectangle position
for i, pt in enumerate(rect):
newPx = int(ox + math.cos(angle) * (pt[0] - ox) - math.sin(angle) * (pt[1] - oy)) # new coordinate of point x
newPy = int(oy + math.sin(angle) * (pt[0] - ox) + math.cos(angle) * (pt[1] - oy)) # new coordinate of point y
nrp[i] = newPx,newPy
nx_min, ny_min, nx_max, ny_max = nrp[0][0], nrp[0][1], nrp[2][0], nrp[2][1] # new bounding boxes values.
return [ny_min, ny_max, nx_min, nx_max]
谢谢。
编辑:
我需要将此旋转与图像和边界框一起使用。 第一张图片是原始图片,第二张图片旋转了 90 度(逆时针),第三张图片旋转了 -90 度(逆时针)。 为了准确起见,我试着在油漆上手动旋转。所以我得到了这些结果。
original of img size:(640x480)
rotation orj, 90, -90
--------------
x_min = 98, 345, 17
y_min = 345, 218, 98
x_max = 420, 462, 420
y_max = 462, 540, 134
【问题讨论】:
-
绕什么点旋转?这是关键问题。矩形的中心?
-
不,因为它没有意义。新边界框不适合旋转版本中的新对象位置。因此,我需要围绕图像中心旋转它。
标签: python computer-vision object-detection yolo data-augmentation