如果您将框放在Nx8
张量/数组中,则可以(cx, cy, w, h, a)
通过以下方式将它们转换为(假设第一个点是左上角,第二个点是左下角,然后是右下角,然后是右上角......):
def DOTA_2_OBB(boxes):
#calculate the angle of the box using arctan (degrees here)
angle = (torch.atan((boxes[:,7] - boxes[:,5])/(boxes[:,6] - boxes[:,4]))*180/np.pi).float()
#centrepoint is the mean of adjacent points
cx = boxes[:,[4,0]].mean(1)
cy = boxes[:,[7,3]].mean(1)
#calculate w and h based on the distance between adjacent points
w = ((boxes[:,7] - boxes[:,1])**2+(boxes[:,6] - boxes[:,0])**2)**0.5
h = ((boxes[:,1] - boxes[:,3])**2+(boxes[:,0] - boxes[:,2])**2)**0.5
return torch.stack([cx,cy,w,h,angle]).T
然后给这个测试...
In [40]: boxes = torch.tensor([[0,2,1,0,2,2,1,3],[4,12,8,2,12,12,8,22]]).float()
In [43]: DOTA_2_OBB(boxes)
Out[43]:
tensor([[ 1.0000, 1.5000, 1.4142, 2.2361, -45.0000],
[ 8.0000, 12.0000, 10.7703, 10.7703, -68.1986]])