Segment point cloud into clusters based on Euclidean distance
Create two concentric spheres and combine them.
[X,Y,Z] = sphere(100); loc1 = [X(:),Y(:),Z(:)]; loc2 = 2*loc1; ptCloud = pointCloud([loc1;loc2]); pcshow(ptCloud) title('Point Cloud')
Set the minimum Euclidean distance between clusters.
minDistance = 0.5;
Segment the point cloud.
[labels,numClusters] = pcsegdist(ptCloud,minDistance);
Plot the labeled results. The points are grouped into two clusters.
pcshow(ptCloud.Location,labels) colormap(hsv(numClusters)) title('Point Cloud Clusters')
Load an organized lidar point cloud in the workspace.
ld = load('drivingLidarPoints.mat');
Detect the ground plane. Distance is measured in meters.
maxDistance = 0.9; referenceVector = [0 0 1]; [~,inliers,outliers] = pcfitplane(ld.ptCloud,maxDistance,referenceVector);
Remove the ground plane points.
ptCloudWithoutGround = select(ld.ptCloud,outliers);
Cluster the point cloud with a minimum of 10 points per cluster.
minDistance = 2; minPoints = 10; [labels,numClusters] = pcsegdist(ptCloudWithoutGround,minDistance,'NumClusterPoints',minPoints);
Remove the points with a label value of
idxValidPoints = find(labels); labelColorIndex = labels(idxValidPoints); segmentedPtCloud = select(ptCloudWithoutGround,idxValidPoints);
Plot the labeled results.
figure colormap(hsv(numClusters)) pcshow(segmentedPtCloud.Location,labelColorIndex) title('Point Cloud Clusters')
minDistance— Minimum Euclidean distance
Minimum Euclidean distance between points from two different clusters, specified as a positive scalar.
Specify optional pairs
of arguments as
Name is the argument name and
Value is the
corresponding value. Name-value arguments must appear after other arguments, but the
order of the pairs does not matter.
NumClusterPoints— Minimum and maximum number of points in each cluster
[1,Inf](default) | 2-element vector | scalar
Minimum and maximum number of points in each cluster, specified as a
scalar or a 2-element vector of the form
NumClusterPoints as a scalar, the
maximum number of points in the cluster is unrestricted. The function
0 when clusters
are outside of the specified range.
ParallelNeighborSearch— Parallel neighbor search to segment point cloud data
Parallel neighbor search to segment point cloud data, specified as
false. Set this
true when you expect there to be
approximately 50 clusters or more with fewer than 100 points per
A parallel neighbor search can improve segmentation speed for some
datasets. Improved speed depends on the dataset and the value of the
labels— Cluster labels
Cluster labels, returned as one of the following.
If the point cloud,
ptCloud, stores point
locations as an unorganized M-by-3 matrix,
labels is an M-by-1
If the point cloud,
ptCloud, stores point
locations as an organized
labels is an
Each point in the point cloud has a cluster label, specified
by the corresponding element in
labels. The value of
each label is an integer from
0 to the number of clusters
of valid points,
numClusters. The value
0 is reserved for invalid points, such as points with
numClusters— Number of clusters
Number of clusters, returned as a positive integer. The number of clusters
does not include the cluster corresponding to invalid points and excludes
the label value,
0, which is reserved for invalid
Usage notes and limitations:
The generated CUDA® code segments the point cloud into clusters by using a combination of algorithms described in  and . The output from the generated code can differ slightly with results from MATLAB® simulation.
NumClusterPoints name-value argument is not
supported for GPU code generation.
 Andrade, Guilherme, Gabriel Ramos, Daniel Madeira, Rafael Sachetto, Renato Ferreira, and Leonardo Rocha. “G-DBSCAN: A GPU Accelerated Algorithm for Density-Based Clustering.” Procedia Computer Science 18 (2013): 369–78. https://doi.org/10.1016/j.procs.2013.05.200.
 Kalentev, Oleksandr, Abha Rai, Stefan Kemnitz, and Ralf Schneider. “Connected Component Labeling on a 2D Grid Using CUDA.” Journal of Parallel and Distributed Computing 71, no. 4 (April 2011): 615–20. https://doi.org/10.1016/j.jpdc.2010.10.012.