-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsalsaNextSemanticSegmentationExample.m
85 lines (71 loc) · 2.4 KB
/
salsaNextSemanticSegmentationExample.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
%% Lidar Point Cloud Semantic Segmentation Using SalsaNext Deep Learning Network
% The following code demonstrates running prediction on a pre-trained
% SalsaNext network, trained on Pandaset Dataset.
%% Prerequisites
% To run this example you need the following prerequisites -
% # MATLAB (R2021a or later) with Lidar and Deep Learning Toolbox.
% # Pretrained SalsaNext network(download instructions below)
%% Download the pre-trained network
model = helper.downloadPretrainedSalsaNext;
net = model.net;
% Define ClassNames
classNames = ["unlabelled"
"Vegetation"
"Ground"
"Road"
"RoadMarkings"
"SideWalk"
"Car"
"Truck"
"OtherVehicle"
"Pedestrian"
"RoadBarriers"
"Signs"
"Buildings"];
%% Perform Semantic Segmentation Using SalsaNext Network
% Read test point cloud.
ptCloud = pcread('pointclouds/Input1.pcd');
% Convert point cloud to 5-channel image.
I = helper.pointCloudToImage(ptCloud);
% Segment objects from the test point cloud.
predictedResult = semanticseg(I, net,"ExecutionEnvironment","auto");
%% Display Output
figure;
helper.displayLidarOverlayImage(I, predictedResult, classNames);
title('Semantic Segmentation Result');
% Display in point cloud format.
cmap = helper.lidarColorMap();
colormap = cmap(single(predictedResult),:);
ptCloudMod = pointCloud(reshape(I(:,:,1:3),[],3),"Color",colormap);
figure
ax = pcshow(ptCloudMod);
zoom(ax,3);
%% Get Bounding Boxes from semgenation output.
% Get the indices of points for the required class.
carIdx = (predictedResult == 'Car');
% Select the points of required class and cluster them based on distance.
ptCldMod = select(ptCloud,carIdx);
[labels,numClusters] = pcsegdist(ptCldMod,0.5);
% Select each cluster and fit a cuboid to each cluster.
bboxes = [];
for num = 1:numClusters
labelIdx = (labels == num);
% Ignore cluster that has points less than 200 points.
if sum(labelIdx,'all') < 200
continue;
end
pcSeg = select(ptCldMod,labelIdx);
try
mdl = pcfitcuboid(pcSeg);
bboxes = [bboxes;mdl.Parameters];
catch
continue;
end
end
% Display the output.
figure;
ax = pcshow(ptCloudMod);
showShape('cuboid',bboxes,'Parent',ax,'Opacity',0.1,...
'Color','green','LineWidth',0.5);
zoom(ax,3);
% Copyright 2021 The MathWorks, Inc