当涉及到测试计算机视觉算法对象检测时,通过旋转测试图像可以检测到一些遗漏的对象。通过这样做,由矩形中的每个点的(x,y)坐标表示的那些检测到的对象位置应该旋转回来。对象检测器的输出是Numpy阵列,其包含例如100个元素,每个元素具有4对点,表示被检测对象周围的矩形的(x,y)坐标,即具有(100,8)形状的Numpy阵列。这些对象是在原始图像的旋转版本中检测到的。因此,为了在原始图像上可视化,必须将它们旋转回来。原始图像的分辨率为5616x3744px,因此90度旋转版本的分辨率为3744x5616px。
问题是我们如何在一条直线上围绕图像中心旋转所有这些点,让我们考虑一下(2808,1872)。当我运行下面的代码时,Python抛出了一个ValueError: operands could not be broadcast together with shapes (1000,8) (2,)
错误,这是有意义的。在这种情况下,速度很重要。所以我尽量避免使用for
循环。
def Rotate2D(pts,cnt,degree):
ang = math.radians(degree)
'''pts = {} Rotates points(nx2) about center cnt(2) by angle ang(1) in radian'''
return scipy.dot(pts-cnt,scipy.array([[scipy.cos(ang),scipy.sin(ang)],[-scipy.sin(ang),scipy.cos(ang)]]))+cnt
发布于 2018-05-28 09:16:28
问题是,您正在尝试从(100, 8)
“坐标数组”中减去“中心”-一个2元素向量。在什么空间? 8D?如果是这样的话,中心也应该是8个坐标的列表,因为8D空间中的一个点是通过提供其沿8个轴中的每个轴的坐标来定义的。
如果坐标数组的形状为(100, 2)
,则代码可以正常工作。
如果您说"100个元素,每个元素有8个点代表(x,y)
__“,您的意思是数组中的每一行包含4个(而不是8个)点(即x
和y
对),如x1
、y1
、x2
、y2
、x3
、y3
、x4
、y4
,那么处理这种情况的最好方法是重塑pts
数组:
import numpy as np
def Rotate2D(pts, cnt, degree):
ang = math.radians(degree)
m = scipy.array([[scipy.cos(ang), scipy.sin(ang)],
[-scipy.sin(ang), scipy.cos(ang)]])
rpts = scipy.dot(np.reshape(pts, (pts.size // 2, 2)) - cnt, m) + cnt
rpts = np.reshape(rpts, pts.shape)
发布于 2018-05-29 06:58:00
在@AGNGazer的帮助下,这个问题得到了回答。首先需要将所有的点移动到中心: 0,0,为了简单和90度旋转,让我们考虑2个对象而不是100个对象。
old_coord = np.zeros((2,8))
old_coord [0] = [500, 500, 510, 500, 510, 510, 500, 510] # only filling one is enough to show the functionality.
使用旋转的图像中心移动到中心1872,2808。
old_coord [:, list(range(0, 8, 2))] -= 1872
old_coord [:, list(range(1, 8, 2))] -= 2808
应用@AGNGazer的函数。
new_coord = Rotate2D(old_coord, [0, 0], 90)
移至原始图像中心1872,2808
new_coord [:, list(range(1, 8, 2))] += 1872
new_coord [:, list(range(0, 8, 2))] += 2808
>>> new_coord
array([[5116., 500., 5116., 510., 5106., 510., 5106., 500.],
[0., 0., 0., 0., 0., 0., 0., 0.]])
https://stackoverflow.com/questions/50557669
复制相似问题