Documentation

This is machine translation

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

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

ImageDatastore

Datastore for image data

Description

An ImageDatastore object manages a collection of image files, where each individual image fits in memory, but the entire collection of images does not necessarily fit. To create an ImageDatastore object, use either the imageDatastore function or the datastore function. Once the object is created, you can specify ImageDatastore properties using dot notation and use functions that access and manage the data.

For example, you can create an ImageDatastore object and specify the 'ReadFcn' parameter:

imds = imageDatastore('peppers.png','ReadFcn',@customreader);
Alternatively, you can assign 'ReadFcn' to @customreader after you create the ImageDatastore:
imds = imageDatastore('peppers.png');
imds.ReadFcn = @customreader;

Creation

Create ImageDatastore objects using the imageDatastore function or the datastore function.

Properties

expand all

Files included in the datastore, resolved as a character vector or cell array of character vectors. Each character vector is a full path to a file. The location argument in the imageDatastore and datastore functions defines Files when the datastore is created.

Example: {'C:\dir\data\file1.jpg';'C:\dir\data\file2.jpg'}

Data Types: char | cell

Number of image files to read in a call to the read function, specified as a positive integer scalar. Each call to the read function reads at most ReadSize images.

File labels, specified as a vector or cell array of label names, with one name for each file in the datastore. The order of the labels in the array corresponds to the order of the associated files in the datastore. If you specify 'LabelSource','foldernames' when creating the ImageDatastore object, then the label name for a file is the name of the folder containing it. If you do not specify 'LabelSource','foldernames', then Labels is an empty cell array. If you change the Files property after the datastore is created, then the Labels property is not automatically updated to incorporate the added files.

Data Types: categorical | cell | logical | double | single

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

function data = customreader(filename)
...
end
If there is more than one output argument, then only the first is used and the rest are ignored.

Example: @customreader

Data Types: function_handle

Object Functions

countEachLabelCount files in ImageDatastore labels
hasdataDetermine if data is available to read
numpartitionsNumber of datastore partitions
partitionPartition a datastore
previewSubset of data in datastore
readRead data in datastore
readallRead all data in datastore
readimageRead specified image from datastore
resetReset datastore to initial state
shuffleShuffle files in ImageDatastore
splitEachLabelSplit ImageDatastore labels by proportions

Examples

expand all

Create an ImageDatastore object containing four images, and preview the first image.

imds = imageDatastore({'street1.jpg','street2.jpg','peppers.png','corn.tif'})
imds = 

  ImageDatastore with properties:

       Files: {
              ' ...\matlab\toolbox\matlab\demos\street1.jpg';
              ' ...\matlab\toolbox\matlab\demos\street2.jpg';
              ' ...\matlab\toolbox\matlab\imagesci\peppers.png'
               ... and 1 more
              }
    ReadSize: 1
      Labels: {}
     ReadFcn: @readDatastoreImage

imshow(preview(imds));

Read only the second and third images, one at a time.

for i = 2:3
    img = readimage(imds,i);
end

Read all four images and view the third image.

imgs = readall(imds);
imshow(imgs{3})

Introduced in R2015b

Was this topic helpful?