首先,创建一个语义分割算法,分割图像中的道路和天空像素。使用深度学习的语义分割(Computer Vision Toolbox)示例描述了如何为语义分割训练一个深度学习网络。该网络已被训练成可以预测11类语义标签,包括 "道路 "和 "天空"。这些网络的性能取决于它们的可泛化程度。将网络应用于它们在训练过程中没有遇到的情况,可能会导致不合格的结果。在学习过程中反复引入自定义的训练数据,可以使网络在类似的数据集上表现更好。
pretrainedURL= 'https://www.mathworks.com/supportfiles/vision/data/segnetVGG16CamVid.mat';
pretrainedFolder= fullfile(tempdir,'pretrainedSegNet');
pretrainedSegNet= fullfile(pretrainedFolder,'segnetVGG16CamVid.mat');
if~exist(pretrainedSegNet,'file')
if ~exist(pretrainedFolder,'dir')
mkdir(pretrainedFolder);
end
disp('Downloading pretrainedSegNet (107 MB)...');
websave(pretrainedSegNet,pretrainedURL);
end
对图像进行分割并显示:
% Loadthe semantic segmentation network
data =load(pretrainedSegNet);
% Loada test image from drivingdata
roadSequenceData= fullfile(toolboxdir('driving'), 'drivingdata', 'roadSequence');
I =imread(fullfile(roadSequenceData, 'f00000.png'));
% Runthe network on the image
automatedLabels = semanticseg(I, data.net);
%Display the labels overlaid on theimage, choosing relevant categories
figure,imshow(labeloverlay(I, automatedLabels, 'IncludedLabels', ["Sky", "Road"]));
% Listcategories of pixels labeled
categories(automatedLabels)
ans = 11×1cell
{'Sky' }
{'Building' }
{'Pole' }
{'Road' }
{'Pavement' }
{'Tree' }
{'SignSymbol'}
{'Fence' }
{'Car' }
{'Pedestrian'}
{'Bicyclist' }
% Theblue overlay indicates the 'Sky' category, and the green overlay
%indicates 'Road'.
properties(Constant)
%Name
% Character vector specifying name of algorithm.
Name = 'RoadAndSkySegmentation'
%Description
% Character vector specifying short description of algorithm.
Description = 'This algorithm usessemanticseg with a pretrained network to annotate roads and sky'
%UserDirections
% Cell array of character vectors specifying directions for
% algorithm users to follow in order to use algorithm.
UserDirections = {...
['Automation algorithms are a way toautomate manual labeling ' ...
'tasks. This AutomationAlgorithmautomatically creates pixel ', ...
'labelsfor road and sky.'], ...
['Review and Modify: Review automated labelsover the interval ', ...
'using playback controls.Modify/delete/add ROIs that were not ' ...
'satisfactorily automated at this stage. If the results are '...
'satisfactory, click Accept toaccept the automated labels.'], ...
['Accept/Cancel: If results ofautomation are satisfactory, ' ...
'click Accept to accept all automated labelsand return to ' ...
'manual labeling. If results of automation are not ' ...
'satisfactory, click Cancel toreturn to manual labeling ' ...
'without saving automated labels.']};
end
properties
% PretrainedNetwork saves theSeriesNetwork object that does the semantic
% segmentation.
PretrainedNetwork
% Categories holds the default'background', 'road', and 'sky'
% categorical types.
AllCategories = {'background'};
% Store names for 'road' and 'sky'.
RoadName
SkyName
end
function TF= checkLabelDefinition(~, labelDef)
isValid = false;
if (strcmpi(labelDef.Name, 'road')&& labelDef.Type == labelType.PixelLabel)
isValid = true;
algObj.RoadName = labelDef.Name;
algObj.AllCategories{end+1} =labelDef.Name;
elseif (strcmpi(labelDef.Name,'sky') && labelDef.Type == labelType.PixelLabel)
isValid = true;
algObj.SkyName = labelDef.Name;
algObj.AllCategories{end+1} =labelDef.Name;
elseif(labelDef.Type ==labelType.PixelLabel)
isValid = true;
end
end
functioninitialize(algObj, ~, ~)
% Point to tempdir wherepretrainedSegNet was downloaded.
pretrainedFolder =fullfile(tempdir,'pretrainedSegNet');
pretrainedSegNet =fullfile(pretrainedFolder,'segnetVGG16CamVid.mat');
data = load(pretrainedSegNet);
% Store the network in the'PretrainedNetwork' property of this object.
algObj.PretrainedNetwork = data.net;
end
function autoLabels = run(algObj, I)
% Setup categorical matrix withcategories including road and
% sky
autoLabels =categorical(zeros(size(I,1), size(I,2)),0:2,algObj.AllCategories,'Ordinal',true);
pixelCat = semanticseg(I,this.PretrainedNetwork);
if ~isempty(pixelCat)
% Add the selected label at the bounding box position(s)
autoLabels(pixelCat =="Road") = algObj.RoadName;
autoLabels(pixelCat =="Sky") = algObj.SkyName;
end
end
这个算法不需要任何清理,所以终止函数是空的。
mkdir('+vision/+labeler');
copyfile('RoadAndSkySegmentation.m','+vision/+labeler');
打开groundTruthLabeler应用,自定义数据进行标注。为了便于说明,打开caltech_cordova1.avi视频。
groundTruthLabeler caltech_cordova1.avi
在左侧窗格中,单击 "定义新的ROI标签 "按钮,定义两个ROI标签,名称分别为道路和天空,类型为像素标签,如图所示:
- 点击算法 > 选择算法 > 刷新列表。
- 点击Algorithm > RoadAndSkySegmentation。如果你没有看到这个选项,请确保当前工作文件夹中有一个名为+vision/+labeler的文件夹,文件名为RoadAndSkySegmentation.m在其中。
- 一旦您对整个视频的像素标签类别感到满意,请单击 "接受"。
视频的像素标签的自动化就完成了。现在您可以继续标记其他感兴趣的对象,保存会话,或导出本次标记运行的结果。
这个例子展示了如何在真值标注应用中使用自动化算法接口,使用预先训练好的语义分割网络来加速道路和天空像素的标注。
联系客服