K-means 方法实现3D点云的聚类算法

import numpy as np
import open3d as o3d
import copy
import random
from matplotlib import pyplot as plt
from sklearn import datasets
from mpl_toolkits.mplot3d import Axes3D
#matplotlib notebook##draw labels on the point cloud
def draw_labels_on_model(pcl,labels):cmap = plt.get_cmap("tab20")pcl_temp = copy.deepcopy(pcl)max_label = labels.max()colors = cmap(labels / (max_label if max_label > 0 else 1))pcl_temp.colors = o3d.utility.Vector3dVector(colors[:,:3])o3d.visualization.draw_geometries([pcl_temp])d = 4
mesh = o3d.geometry.TriangleMesh.create_tetrahedron().translate((-d, 0, 0))
mesh += o3d.geometry.TriangleMesh.create_octahedron().translate((0, 0, 0))
mesh += o3d.geometry.TriangleMesh.create_icosahedron().translate((d, 0, 0))
mesh += o3d.geometry.TriangleMesh.create_torus().translate((-d, -d, 0))
mesh += o3d.geometry.TriangleMesh.create_moebius(twists=1).translate((0, -d, 0))
mesh += o3d.geometry.TriangleMesh.create_moebius(twists=2).translate((d, -d, 0))##apply k means on this point cloud
point_cloud = mesh.sample_points_uniformly(int(1e3))
##transfer point cloud into array
xyz = np.asarray(point_cloud.points)##define several necessary methods here
#normalize the dataset
def normalize(X,axis=-1,p=2):#normalize the array and then transfer into a vectorlp_norm = np.atleast_1d(np.linalg.norm(X,p,axis))lp_norm[lp_norm == 0] = 1#expand a dimension along axis for lp_norm#this is to make sure X and lp_norm have the same dimensionsreturn X / np.expand_dims(lp_norm,axis)def euclidean_distance(one_sample,X):#transfer one_sample into 1D vectorone_sample = one_sample.reshape(1,-1)#transfer X into 1D vectorX = X.reshape(X.shape[0],-1)#this is used to make sure one_sample's dimension is same as Xdistances = np.power(np.tile(one_sample,(X.shape[0],1))-X,2).sum(axis=1)return distancesclass Kmeans():#constructordef __init__(self,k=2,max_iterations=1500,tolerance=0.00001):self.k = kself.max_iterations = max_iterationsself.tolerance = tolerance#randomly select k centroidsdef init_random_centroids(self,X):#save the shape of Xn_samples, n_features = np.shape(X)#make a zero matrix to store valuescentroids = np.zeros((self.k,n_features))#bcs there is k centroids, so we loop k tiemsfor i in range(self.k):#selecting values under the range radomlycentroid = X[np.random.choice(range(n_samples))]centroids[i] = centroidreturn centroids#find the closest centroid of a sampledef closest_centroid(self,sample,centroids):distances = euclidean_distance(sample,centroids)#np.argmin return the indices of the minimum of distancesclosest_i = np.argmin(distances)return closest_i#determine the clusersdef create_clusters(self,centroids,X):n_samples = np.shape(X)[0]#This is to construct the nested list for storing clustersclusters = [[] for _ in range(self.k)]for sample_i, sample in enumerate(X):centroid_i = self.closest_centroid(sample,centroids)clusters[centroid_i].append(sample_i)return clusters#update the centroids based on mean algorithmdef update_centroids(self,clusters,X):n_features = np.shape(X)[1]centroids = np.zeros((self.k,n_features))for i, cluster in enumerate(clusters):centroid = np.mean(X[cluster],axis=0)centroids[i] = centroidreturn centroids#obtain the labels#same cluster, same y_pred valuedef get_cluster_labels(self,clusters,X):y_pred = np.zeros(np.shape(X)[0])for cluster_i, cluster in enumerate(clusters):for sample_i in cluster:y_pred[sample_i] = cluster_ireturn y_pred#predict the labelsdef predict(self,X):#selecting the centroids randomlycentroids = self.init_random_centroids(X)for _ in range(self.max_iterations):#clustering all the data pointclusters = self.create_clusters(centroids,X)former_centroids = centroids#calculate new cluster centercentroids = self.update_centroids(clusters,X)#judge the current difference if it meets convergence  diff = centroids - former_centroidsif diff.any() < self.tolerance:breakreturn self.get_cluster_labels(clusters,X) if __name__ == "__main__":clf = Kmeans(k=6)labels = clf.predict(xyz)draw_labels_on_model(point_cloud,labels)

K-means 方法实现3D点云的聚类算法相关推荐

  1. 毫米波雷达点云 DBSCAN聚类算法

    毫米雷达点云 DBSCAN聚类算法 聚类的目的 聚类算法分类 原型聚类 层次聚类 密度聚类 DBSCAN聚类算法原理 相关定义 算法流程以及伪代码 DBSCAN算法优缺点 DBSCAN参数选择 聚类衡 ...

  2. PCL 点云DBSCAN聚类算法

    文章目录 一.简介 二.实现代码 三.实现效果 参考资料 一.简介 关于DBSCAN聚类算法的原理可以看之前的文章:点云DBSCAN聚类算法(C++),这里仅仅是对该算法进行了整理,让它可以支持PCL ...

  3. 3D点云补全算法汇总及最新进展

    点击上方"3D视觉工坊",选择"星标" 干货第一时间送达 Part 1  前言 在探讨3D 点云补全专题前,先介绍三个概念: 概念一:partial obser ...

  4. 3D-BoNet:比3D点云实例分割算法快10倍!代码已开源

    点击我爱计算机视觉标星,更快获取CVML新技术 本文转载自新智元(AI_era)   新智元报道   来源:投稿 编辑:元子 [新智元导读]本文提出了一种基于边界框回归的高效点云实例分割算法,通过最小 ...

  5. 【点云论文速读】点云分层聚类算法

    点云PCL免费知识星球,点云论文速读. 标题:PAIRWISE LINKAGE FOR POINT CLOUD SEGMENTATION 作者:Lu, Xiaohu and Yao, Jian and ...

  6. 点云DBSCAN聚类算法(C++)

    文章目录 一.DBSCAN算法 二.算法步骤 三.代码实现效果 四.小结 一.DBSCAN算法 DBSCAN算法,全称为"Density-Based Spatial Clustering o ...

  7. 多任务上实现SOTA,UBC、谷歌联合Hinton等提出3D点云的无监督胶囊网络

    点击上方"3D视觉工坊",选择"星标" 干货第一时间送达 作者丨杜伟.小舟 来源丨机器之心 这是一种为 3D 点云提出的无监督胶囊架构,并且在 3D 点云重构. ...

  8. UBC、谷歌联合Hinton等提出3D点云的无监督胶囊网络,多任务上实现SOTA

    本文转载自机器之心. 机器之心报道 作者:杜伟.小舟 这是一种为 3D 点云提出的无监督胶囊架构,并且在 3D 点云重构.配准和无监督分类方面优于 SOTA 方法. 理解对象是计算机视觉的核心问题之一 ...

  9. UBC、谷歌联合Hinton等提出3D点云的无监督胶囊网络,多任务上实现SOTA | AI日报...

    多任务上实现SOTA,UBC.谷歌联合Hinton等提出3D点云的无监督胶囊网络 理解对象是计算机视觉的核心问题之一.传统方法而言,理解对象任务可以依赖于大型带注释的数据集,而无监督方法已经消除了对标 ...

最新文章

  1. 从ViewDragLayout中学到的一些事实
  2. 【移动开发】Android中强大的适配功能----Fragment(碎片)总结
  3. Struts2利用iText导出word文档(包含表格)
  4. JTable动态显示隐藏列
  5. VS 工具-选项对话框 调试选项相关学习总结
  6. c++17(17)-异常try catch,operator[],vector at
  7. LeetCode 863. 二叉树中所有距离为 K 的结点(公共祖先/ DFS+BFS)
  8. 基于matlab的pcb焊盘,EDA复习题
  9. java 数组构造_java – 从数组构造(非二进制)树
  10. Android RecyclerView 滑动到指定位置 RecyclerView 滑动到顶部
  11. 2 image pil 转_「博文连载」RGB888转YCbCr的HDL实现
  12. PowerShell Format-Table的细节(AutoSize和Wrap参数)
  13. 腾讯产品笔试策划+经验​
  14. 冰河又一MySQL力作出版(文末送书)!!
  15. 存储服务器 自建,搭建及了解存储服务器
  16. 研究区分onbeforeunload事件是刷新还是关闭
  17. 压缩工具Bandizip
  18. 必须掌握的hashcode()方法
  19. 制作github.io学术版个人主页
  20. Win2008服务器操作系统-DNS

热门文章

  1. Nginx静态资源部署
  2. Nginx静态资源配置
  3. mybatisplus配置详解
  4. oracle 授权用户查看指定视图
  5. 设计模式--工厂模式(六)
  6. Linux c 线程分离(两种方法实现线程分离)
  7. linux运维工程师最近两个月遇到的面试题汇总
  8. 使用html制作酒店主页,主页.html
  9. python读取excel中文_python如何读取excel
  10. CC2530驱动_DHT11