Main Content

pcsegdist

Segment point cloud into clusters based on Euclidean distance

Description

labels = pcsegdist(ptCloud,minDistance) segments a point cloud into clusters, with a minimum Euclidean distance of minDistance between points from different clusters. pcsegdist assigns an integer cluster label to each point in the point cloud, and returns the labels of all points.

example

[labels,numClusters] = pcsegdist(ptCloud,minDistance) also returns the number of clusters.

[labels,numClusters] = segmentLidarData(ptCloud,'NumClusterPoints',minmaxNCP) specifies the minimum and maximum number of points in each cluster.

Examples

collapse all

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')

Figure contains an axes. The axes with title Point Cloud contains an object of type scatter.

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')

Figure contains an axes. The axes with title Point Cloud Clusters contains an object of type scatter.

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 0.

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')

Figure contains an axes. The axes with title Point Cloud Clusters contains an object of type scatter.

Input Arguments

collapse all

Point cloud, specified as a pointCloud object.

Minimum Euclidean distance between points from two different clusters, specified as a positive scalar.

Data Types: single | double

Minimum and maximum number of points in each cluster, specified as a scalar or a 2-element vector of the form [minPoints maxPoints]. When you specify minmaxNCP as a scalar value, the function does not restrict the maximum number of points in each cluster. The function assigns a label value of 0 for clusters found to be outside the specified range.

Output Arguments

collapse all

Cluster labels, returned as one of the following.

  • If the point cloud, ptCloud, stores point locations as an unorganized M-by-3 matrix, then labels is an M-by-1 vector.

  • If the point cloud, ptCloud, stores point locations as an organized M-by-N-by-3 array, then labels is an M-by-N matrix.

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 Inf or NaN coordinates.

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 points.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Introduced in R2018a