PixelLabelDatastore - Datastore for pixel label data - MATLAB (original) (raw)

Datastore for pixel label data

Description

You can use a PixelLabelDatastore object to read pixel label data for semantic segmentation.

To read pixel label data from a PixelLabelDatastore, use theread function. This function returns a categorical array that contains a categorical label assigned to every (i,j) pixel location or (i,j,k) voxel location.

Creation

Create a PixelLabelDatastore object using thepixelLabelDatastore function described here. Once the object is created, you can use functions that access and manage the data. You can use dot notation to modify the [ReadSize](#butueui-1-ReadSize) and [ReadFcn](#butueui-1-ReadFcn) properties.

Syntax

Description

pxds = pixelLabelDatastore([gTruth](#d126e115123)) creates a PixelLabelDatastore object from a groundTruth object or an array ofgroundTruth objects.

example

pxds = pixelLabelDatastore([location](#d126e115156),[classNames](#d126e115258),[pixelLabelIDs](#d126e115287)) creates a PixelLabelDatastore object from image files that store pixel label data, in the folder or files specified bylocation. The function creates the object using pixel IDs that map image pixel label values to class names.

pxds = pixelLabelDatastore(___,[Name=Value](#namevaluepairarguments)) creates a PixelLabelDatastore object using name-value arguments to set one or both of the ReadSize orAlternateFileSystemRoots properties. For image file input, you can also use name-value pair arguments to set theReadFcn property or to specify options about files to include in the datastore. You can specify multiple name-value pairs. Enclose each property name in quotes.

For example,pixelLabelDatastore(loc,names,ids,ReadSize=8,FileExtensions="png") creates a pixel label datastore that includes only PNG images and that reads eight images during each call to the read function.

Input Arguments

expand all

Ground truth data, specified as a groundTruth object or as an array of groundTruth objects. Each groundTruth object contains information about the data source, the list of label definitions, and all marked labels for a set of ground truth labels.

Folder or image file names, specified as a character vector, string array, or cell array of character vectors. Images must containuint8 data.

location Description
character vector, string scalar Name of a folder. Files within subfolders of the specified folder are not automatically included in the datastore. The datastore includes only images with supported file formats and ignores any other format. See a list of supported file formats by using the imformats function.
cell array of character vectors, array of strings File names of multiple images.

pixelLabelDatastore expands the file names and stores the full file paths in the [Files](#butueui-1-Files) property.

You can use the wildcard character (*) when specifyinglocation. This character indicates that all matching files or all files in the matching folders are included in the datastore.

If the files are not in the current folder, then you must include the full or relative path.

If the files are not available locally, then the full path of the files or folders must be an internationalized resource identifier (IRI), such ashdfs://_`hostname`_:_`portnumber`_/_`pathtofile`_. For information on using a datastore with Amazon S3™ and HDFS™, see Work with Remote Data.

Example: "file1.jpg"

Example: "../dir/data/file1.png"

Example: ["C:\dir\data\file1.tif","C:\dir\data\file2.tif"]

Example: 'C:\dir\data\*.jpg'

Data Types: char | string

Class names, specified as a cell array of strings or character vectors. pixelLabelDatastore converts the names to a cell array and stores the names in the [ClassNames](#butueui-1-ClassNames) property.

Example: ["sky" "grass" "building" "sidewalk"]

IDs to map pixel labels to [ClassNames](#butueui-1-ClassNames), specified as a vector, an_m_-by-3 matrix, a cell array of column vectors, or a cell array of _m_-by-3 matrices. Values must be integers in the range [0, 255]. _m_-by-3 matrices are only supported for RGB images.

Format Description
vector The length of the vector must equal the number of class names. Values must be unique.
_m_-by-3 matrix m corresponds to the number of class names. Each row contains a 3-element vector representing the RGB pixel value to associate with each class name. Vectors must be unique. Use this format for pixel label data stored as RGB images.
cell array of column vectorscell array of_m_-by-3 matrices Use a cell array to map multiple pixel label IDs to one class name.

Name-Value Arguments

expand all

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, where 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.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: pixelLabelDatastore(IncludeSubfolders=true) specifies to include all files and subfolders within each folder.

Include subfolders, specified as false (0), or true (1). Specify true to include all files and subfolders within each folder orfalse to include only the files within each folder.

The IncludeSubfolders argument is not supported when you specify ground truth datagTruth.

File extensions of files to include in the datastore, specified as a character vector, cell array of character vectors, string scalar, or string array. By default, the datastore includes files with all extensions supported by the imformats function.

The FileExtensions argument is not supported when you specify ground truth datagTruth.

Example: ["png" "gif" "bmp"]

Properties

expand all

This property is read-only.

Files included in the datastore, specified as a character vector or cell array of character vectors. Each character vector is a full path to a file. When you create a PixelLabelDatastore object, use thelocation argument to set this property.

This property is read-only.

Class names, specified as a cell array of character vectors.

Maximum number of image files to read in each call to the read function, specified as a positive integer.

Alternate file system root paths, specified as the name-value argument consisting of"AlternateFileSystemRoots" and a string vector or a cell array. Use"AlternateFileSystemRoots" when you create a datastore on a local machine, but need to access and process the data on another machine (possibly of a different operating system). Also, when processing data using the Parallel Computing Toolbox™ and the MATLAB® Parallel Server™, and the data is stored on your local machines with a copy of the data available on different platform cloud or cluster machines, you must use"AlternateFileSystemRoots" to associate the root paths.

The value of "AlternateFileSystemRoots" must satisfy these conditions:

For more information, see Set Up Datastore for Processing on Different Machines or Clusters.

Example: ["Z:\datasets","/mynetwork/datasets"]

Data Types: string | cell

Function that reads pixel labeled image data, specified as a function handle. The custom read function must take an image file name as input, and then output the corresponding pixel labeled data as a categorical array. For example, if customreader is the specified function to read the image data, then it must have a signature similar to the following:

function C = customreader(filename) ... end

If the read function has more than one output argument, then only the first one is used. The rest are ignored.

The ReadFcn property is not supported when you specify ground truth data gTruth.

Example: @customreader

Object Functions

combine Combine data from multiple datastores
countEachLabel Count occurrence of pixel or box labels
hasdata Determine if data is available to read from label datastore
numpartitions Number of partitions for label datastore
partition Partition label datastore
preview Read first row of data in datastore
read Read data from label datastore
readall Read all data in label datastore
readimage Read specified pixel label data file
reset Reset label datastore to initial state
shuffle Return shuffled version of label datastore
transform Transform datastore
subset Create subset of datastore or FileSet
isPartitionable Determine whether datastore is partitionable
isShuffleable Determine whether datastore is shuffleable

Examples

collapse all

Overlay pixel label data on an image.

Set the location of the image and pixel label data.

dataDir = fullfile(toolboxdir('vision'),'visiondata'); imDir = fullfile(dataDir,'building'); pxDir = fullfile(dataDir,'buildingPixelLabels');

Create an image datastore.

imds = imageDatastore(imDir);

Create a pixel label datastore.

classNames = ["sky" "grass" "building" "sidewalk"]; pixelLabelID = [1 2 3 4]; pxds = pixelLabelDatastore(pxDir,classNames,pixelLabelID);

Read the image and pixel label data. read(pxds) returns a categorical matrix, C. The element C(i,j) in the matrix is the categorical label assigned to the pixel at the location l(i,j).

I = read(imds); C = read(pxds);

Display the label categories in C.

ans = 4×1 cell {'sky' } {'grass' } {'building'} {'sidewalk'}

Overlay and display the pixel label data onto the image.

B = labeloverlay(I,C{1}); figure imshow(B)

Figure contains an axes object. The hidden axes object contains an object of type image.

Specify the location of 3-D volume and pixel label data. This data is a labeled 3-D MRI scan of a brain.

dataDir = fullfile(toolboxdir("images"),"imdata"); imDir = fullfile(dataDir,"BrainMRILabeled","images"); pxDir = fullfile(dataDir,"BrainMRILabeled","labels");

Specify a custom read function. This example specifies a function called samplePXDSMatReader (defined at the end of the example) that read 3-D image data from MAT image files.

matReader = @samplePXDSMatReader;

Create an image datastore.

imds = imageDatastore(imDir,"FileExtensions",".mat","ReadFcn",matReader);

Create a pixel label datastore.

classNames = ["edema","nonEnhancingTumor","enhancingTumour"]; pixelLabelID = [1 2 3]; pxds = pixelLabelDatastore(pxDir,classNames,pixelLabelID,"FileExtensions",".mat","ReadFcn",matReader);

Read volume and pixel label data. L is a categorical matrix, where L(i,j,k) is the categorical label assigned to V(i,j,k).

V = read(imds); L = read(pxds);

Display the label categories.

C = 3×1 cell {'edema' } {'nonEnhancingTumor'} {'enhancingTumour' }

Visualize the result using the viewer3d and volshow functions. Display the labels by specifying the OverlayData and OverlayAlpha name-value arguments.

viewer = viewer3d(BackgroundColor="white",BackgroundGradient="off",CameraZoom=2);

h = volshow(V,Parent=viewer, ... RenderingStyle="GradientOpacity", ... GradientOpacityValue=0.8, ... Alphamap=linspace(0,0.2,256), ... OverlayData=L{1}, ... OverlayAlpha=0.8);

This example defines a helper function, samplePXDSMatReader, to read the 3-D image data from the image files. This function loads a MAT file and returns the first variable saved in that file.

function data = samplePXDSMatReader(filename) inp = load(filename); f = fields(inp); data = inp.(f{1}); end

Tips

Version History

Introduced in R2017b

See Also

Apps

Functions

Objects

Topics