# Automate point cloud plane and clustering with Python

**URL:** <https://forum.dynamobim.com/t/automate-point-cloud-plane-and-clustering-with-python/71054>\
**Category:** Uncategorized\
**Created:** [December 3, 2021, 2:47pm UTC](https://forum.dynamobim.com/t/automate-point-cloud-plane-and-clustering-with-python/71054 "2021-12-03T14:47:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![RMohareb](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/rmohareb/32/120534_2.png) [@RMohareb](https://forum.dynamobim.com/u/RMohareb)\
**Post date:** [December 3, 2021, 2:47pm UTC](https://forum.dynamobim.com/t/automate-point-cloud-plane-and-clustering-with-python/71054/1 "2021-12-03T14:47:28Z")

</div>

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

> **[How to automate 3D point cloud segmentation and clustering with Python](https://towardsdatascience.com/how-to-automate-3d-point-cloud-segmentation-and-clustering-with-python-343c9039e4f5)**
>
> A complete python tutorial to learn how to automate point cloud segmentation and 3D shape detection using RANSAC and unsupervised clustering with DBSCAN

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](http://www.open3d.org/docs/release/getting_started.html)

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/6/5/65f269ebdccfd12514616cd015699d52cfc6cd87.jpeg)  
 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/b/6/b6fc3432e773b0d06e86180ef8358efada398e94.jpeg)  
 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/b/c/bc5f8c9ea3cd83f094de8f12fb52ad3ce199dd2e.png)  
 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/2/b/2b3ac87c62782f1aa38b9aded1e1f36d424604bc.png)

```auto
"""
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

```

---

<div class="post-metadata">

**Author:** ![rafaelramonvigo](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@rafaelramonvigo](https://forum.dynamobim.com/u/rafaelramonvigo)\
**Post date:** [June 16, 2023, 3:57pm UTC](https://forum.dynamobim.com/t/automate-point-cloud-plane-and-clustering-with-python/71054/2 "2023-06-16T15:57:15Z")

</div>

Hi RMohareb, would you so kind to share the dynamo code used for that ilustrations? Thanks in advance. Best regards

---

<div class="post-metadata">

**Author:** ![bayowindapo](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/bayowindapo/32/184532_2.png) [@bayowindapo](https://forum.dynamobim.com/u/bayowindapo)\
**Post date:** [June 24, 2023, 9:23pm UTC](https://forum.dynamobim.com/t/automate-point-cloud-plane-and-clustering-with-python/71054/3 "2023-06-24T21:23:30Z")

</div>

Hello. Thanks for this. What format was your point cloud file? PTS?

---

<div class="post-metadata">

**Author:** ![RMohareb](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/rmohareb/32/120534_2.png) [@RMohareb](https://forum.dynamobim.com/u/RMohareb)\
**Post date:** [June 24, 2023, 9:33pm UTC](https://forum.dynamobim.com/t/automate-point-cloud-plane-and-clustering-with-python/71054/4 "2023-06-24T21:33:28Z")

</div>

Hi @bayowindapo yeit is pts file you can export it from Recap

---

<div class="post-metadata">

**Author:** ![bayowindapo](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/bayowindapo/32/184532_2.png) [@bayowindapo](https://forum.dynamobim.com/u/bayowindapo)\
**Post date:** [June 25, 2023, 9:37am UTC](https://forum.dynamobim.com/t/automate-point-cloud-plane-and-clustering-with-python/71054/5 "2023-06-25T09:37:50Z")

</div>

I thought so. Thank you!
