使用Python和NumPy创建定向边界框(OBB)

2024-09-07 02:34:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我几乎把这个example翻译成Python了

我的清单是

import numpy
a  = numpy.array([(3.7, 1.7), (4.1, 3.8), (4.7, 2.9), (5.2, 2.8), (6.0,4.0), (6.3, 3.6), (9.7, 6.3), (10.0, 4.9), (11.0, 3.6), (12.5, 6.4)])
ca = numpy.cov(a,y = None,rowvar = 0,bias = 1)
print ca
v, vect = numpy.linalg.eig(ca)
tvect = numpy.transpose(vect)
print tvect

变量ca与示例中的协方差矩阵相同,tvect与示例中的特征向量相同。在

你能告诉我我要做些什么来完成这个列表并建立一个边界框吗?在

一般来说,对于三维点集,相同的列表是否适用? 谢谢!在


Tags: importnumpynone示例列表examplecovarray
1条回答
网友
1楼 · 发布于 2024-09-07 02:34:12

他没有完全解释如何获得中心和最终的边界框,但我认为这应该是可行的:

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

a  = np.array([(3.7, 1.7), (4.1, 3.8), (4.7, 2.9), (5.2, 2.8), (6.0,4.0), (6.3, 3.6), (9.7, 6.3), (10.0, 4.9), (11.0, 3.6), (12.5, 6.4)])
ca = np.cov(a,y = None,rowvar = 0,bias = 1)

v, vect = np.linalg.eig(ca)
tvect = np.transpose(vect)



fig = plt.figure(figsize=(12,12))
ax = fig.add_subplot(111)
ax.scatter(a[:,0],a[:,1])

#use the inverse of the eigenvectors as a rotation matrix and
#rotate the points so they align with the x and y axes
ar = np.dot(a,np.linalg.inv(tvect))

# get the minimum and maximum x and y 
mina = np.min(ar,axis=0)
maxa = np.max(ar,axis=0)
diff = (maxa - mina)*0.5

# the center is just half way between the min and max xy
center = mina + diff

#get the 4 corners by subtracting and adding half the bounding boxes height and width to the center
corners = np.array([center+[-diff[0],-diff[1]],center+[diff[0],-diff[1]],center+[diff[0],diff[1]],center+[-diff[0],diff[1]],center+[-diff[0],-diff[1]]])

#use the the eigenvectors as a rotation matrix and
#rotate the corners and the centerback
corners = np.dot(corners,tvect)
center = np.dot(center,tvect)

ax.scatter([center[0]],[center[1]])    
ax.plot(corners[:,0],corners[:,1],'-')

plt.axis('equal')
plt.show()

enter image description here

相关问题 更多 >