Automate point cloud plane and clustering with Python

Hi
I need to share with you how to automate point cloud segmentation by use python 3 in dynamo

I use the pythone code created by Florent Poux, Ph.D. and used in inside dynamo python3 node

To automate point cloud segmentation and 3D shape detection used multi-order RANSAC and unsupervised clustering (DBSCAN).
“Open 3d” python packedge was used in this example and the sample is loaded from the Florent Poux example
Getting started — Open3D 0.14.1 documentation


image

"""
Ramiz Mohareb
enramiz@yahoo.com
I use the pythone code created by Florent Poux, Ph.D. and used in inside dynamo python3 node
https://medium.com/towards-data-science/how-to-automate-3d-point-cloud-segmentation-and-clustering-with-python-343c9039e4f5
"""

import random
import numpy as np
import clr
import open3d

clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
import matplotlib.pyplot as plt


filepath=IN[0]


import numpy as np
import open3d as o3d
[Point_Cloud.dyn|attachment](upload://zpv8LTaRTHJfMHGL7phUZJS3XZy.dyn) (42.1 KB)



segment_models={}
segments={}
inliers={}
inlier_cloud={}

max_plane_idx=20

Outt=[]
pcd = o3d.io.read_point_cloud(filepath)


rest=pcd
for i in range(max_plane_idx):
    colors = plt.get_cmap("tab20")(i)
    segment_models[i], inliers = rest.segment_plane(
    distance_threshold=0.01,ransac_n=5,num_iterations=10000)
    segments[i]=rest.select_by_index(inliers)
    labels = np.array(segments[i].cluster_dbscan(eps=0.1, min_points=100))
    candidates=[len(np.where(labels==j)[0]) for j in np.unique(labels)]
    best_candidate=int(np.unique(labels)[np.where(candidates== np.max(candidates))[0]])
    rest = rest.select_by_index(inliers, invert=True) + segments[i].select_by_index(list(np.where(labels!=best_candidate)[0]))
    segments[i]=segments[i].select_by_index(list(np.where(labels== best_candidate)[0]))
    
    
for j in range(max_plane_idx):  
    Outt.append(np.asarray(segments[j].points).tolist())
    
o3d.visualization.draw_geometries([segments[i] for i in range(max_plane_idx)]+[rest])
#
OUT = Outt
8 Likes