点云学习01
1. PCA and normal vector
def PCA(data, correlation = False, sort = True):
data_normal = data - np.mean(data, axis = 0)
eig, eig_vec = np.linalg.eig(np.dot(data_normal.transpose(), data_normal))
if sort and data_normal.shape[0] > 0:
sort_index = eig.argsort()[::-1]
eig = np.sqrt(np.abs(eig[sort_index]) / data_normal.shape[0])
eig_vec = eig_vec[:, sort_index]
return eig, eig_vec
cat_index = 'airplane'
root_dir = '/home/lss/Code/point_cloud/modelnet40_normal_resampled'
file_name = os.path.join(root_dir, cat_index, cat_index + "_0134.txt")
data = np.loadtxt(file_name, dtype = float, delimiter = ',')
point_cloud = data[:, 0:3]
point_cloud_o3d = o3d.geometry.PointCloud()
point_cloud_o3d.points = o3d.utility.Vector3dVector(point_cloud)
o3d.visualization.draw_geometries([point_cloud_o3d])
w, v = PCA(point_cloud)
point_mean = np.mean(point_cloud, axis=0)
pca_vec = w * v
pca_points = np.vstack((np.array([0, 0, 0]), pca_vec.transpose())) + point_mean
line_index = [
[0, 1],
[0, 2],
[0, 3]
]
line_colors = [
[1.0, 0, 0],
[1.0, 0, 0],
[1.0, 0, 0]
]
pca_line_set = o3d.geometry.LineSet(
points = o3d.ut