This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

pixelLabelTrainingData

Create training data for semantic segmentation from ground truth

Syntax

[imds,pxds] = pixelLabelTrainingData(gTruth)
[imds,pxds] = pixelLabelTrainingData(gTruth,Name,Value)

Description

example

[imds,pxds] = pixelLabelTrainingData(gTruth) creates image datastore imds and pixel label datastore pxds from the specified ground truth. Use these datastores with the trainNetwork function to train deep learning segmentation networks, or with the evaluateSemanticSegmentation function to evaluate the result from deep learning or classical segmentation methods.

This function supports parallel computing using multiple MATLAB® workers. Enable parallel computing using the Computer Vision System Toolbox Preferences dialog box.

[imds,pxds] = pixelLabelTrainingData(gTruth,Name,Value) returns image and pixel label datastores with additional options specified by one or more name-value pair arguments.

  • If the groundTruth objects in gTruth were created using a video file or a custom data source, then you can specify any combination of name-value pair arguments.

  • If the groundTruth objects were created from an image collection or image sequence data source, then you can specify only the SamplingFactor name-value pair argument

Examples

collapse all

Load a groundTruth object named gTruth. The ground truth contains pixel labels for triangles and background, annotated on a video with 100 frames.

visiondataPath = fullfile(matlabroot,'toolbox','vision','visiondata');
addpath(fullfile(visiondataPath,'triangleImages','testImages'));
load(fullfile(visiondataPath,'triangleImages','triangleGroundTruth.mat'));

Create a temporary folder.

tempf = 'C:\temp\';
mkdir(tempf)

Create an imageDatastore and a pixelLabelDatastore from the video file and corresponding pixel labels. Write every fifth image to the temporary folder.

[imds,pxdsTruth] = pixelLabelTrainingData(gTruth,...
    'SamplingFactor',5,'WriteLocation',tempf);
Write images extracted for training to folder: 
    C:\temp\

Writing 20 images extracted from triangleVideo.avi...Completed.

Confirm that the temporary folder contains every fifth image.

imds.Files
ans = 20×1 cell array
    {'C:\temp\triangleVideo01.png'}
    {'C:\temp\triangleVideo06.png'}
    {'C:\temp\triangleVideo11.png'}
    {'C:\temp\triangleVideo16.png'}
    {'C:\temp\triangleVideo21.png'}
    {'C:\temp\triangleVideo26.png'}
    {'C:\temp\triangleVideo31.png'}
    {'C:\temp\triangleVideo36.png'}
    {'C:\temp\triangleVideo41.png'}
    {'C:\temp\triangleVideo46.png'}
    {'C:\temp\triangleVideo51.png'}
    {'C:\temp\triangleVideo56.png'}
    {'C:\temp\triangleVideo61.png'}
    {'C:\temp\triangleVideo66.png'}
    {'C:\temp\triangleVideo71.png'}
    {'C:\temp\triangleVideo76.png'}
    {'C:\temp\triangleVideo81.png'}
    {'C:\temp\triangleVideo86.png'}
    {'C:\temp\triangleVideo91.png'}
    {'C:\temp\triangleVideo96.png'}

Remove the video and images from the path.

rmpath(fullfile(visiondataPath,'triangleImages','testImages'));
delete([tempf,'triangleVideo*.png'])

Input Arguments

collapse all

Ground truth data, specified as a scalar groundTruth object or an array of groundTruth objects. When gTruth is an array of groundTruth objects, the LabelDefinitions property of each object must contain the same pixel label names.

If you use custom data sources in gTruth with parallel computing enabled, then the reader function is expected to work with a pool of MATLAB workers to read images from the data source in parallel.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'SamplingFactor',5

Factor at which to subsample images in the ground truth data source, specified as the comma-separated pair consisting of 'SamplingFactor' and an integer or a vector of integers. For a sampling factor of N, the returned image datastore imds includes every Nth image in the ground truth data source. Ground truth images with empty pixel labels are ignored.

SamplingFactorSampling Factor Applied
IntegerAll ground truth data sources in gTruth are sampled with the same sampling factor, N.
Vector of integersThe kth ground truth data source in gTruth is sampled with a sampling factor of N(k).

Folder name to which extracted images are written, specified as the comma-separated pair consisting of 'WriteLocation' and a string scalar or character vector. The specified folder must exist and have write permissions. This argument applies only for groundTruth objects created using a video file or a custom data source.

Image file format, specified as the comma-separated pair consisting of 'ImageFormat' and a string scalar or character vector. File formats must be supported by imwrite. This argument applies only for groundTruth objects created using a video file or a custom data source.

Prefix applied to file names of the output images, specified as the comma-separated pair consisting of 'NamePrefix' and a string scalar or character vector. The image files are named as:

<name_prefix><image_number>.<image_format>

The default value of NamePrefix is the name of the video file or data source containing the images. This argument applies only for groundTruth objects created using a video file or a custom data source.

Display training progress on the MATLAB command line, specified as the comma-separated pair consisting of 'Verbose' and true or false. This argument applies only for groundTruth objects created using a video file or a custom data source.

Output Arguments

collapse all

Collection of images extracted from the ground truth, gTruth, returned as an ImageDatastore object. Each image in imds has annotations with at least one class of pixel labels. imds ignores images that with no annotations.

Collection of pixel-labeled images extracted from the ground truth, gTruth, returned as a PixelLabelDatastore object. The object contains a categorical matrix of pixel labels for each image contained in the image datastore, imds. Labels that do not correspond to pixel labels are ignored.

Introduced in R2018a