我几乎已将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 与此处的特征向量相同例子。
请你告诉我,我将如何完成此列表并建立一个边界框?
一般来说,完全相同的列表适用于3D点集吗? 谢谢!
答案 0 :(得分:3)
他没有完全解释他如何获得中心和最后的边界框,但我认为这应该有效:
%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()