transform

Transform datastore

Syntax

dsnew = transform(ds,@fcn)
dsnew = transform(ds,@fcn,'IncludeInfo',IncludeInfo)

Description

example

dsnew = transform(ds,@fcn) transforms an input datastore ds using the transformation function fcn and returns the transformed datastore dsnew.

dsnew = transform(ds,@fcn,'IncludeInfo',IncludeInfo) uses an alternative definition of the transform function fcn. The alternative definition enables you to use the additional information returned by the read function of the datastore.

Examples

collapse all

Create a datastore for a collection of images and apply the same transformation to all the images in the datastore. For instance, resize all the images in a collection to a specified target size.

Create an ImageDatastore with two images.

imds = imageDatastore({'street1.jpg','peppers.png'})
imds = 
  ImageDatastore with properties:

                       Files: {
                              ' .../devel/bat/Bdoc19a/build/matlab/toolbox/matlab/demos/street1.jpg';
                              ' .../devel/bat/Bdoc19a/build/matlab/toolbox/matlab/imagesci/peppers.png'
                              }
    AlternateFileSystemRoots: {}
                    ReadSize: 1
                      Labels: {}
                     ReadFcn: @readDatastoreImage

Read all the images. Notice that the datastore contains images of different sizes.

img1 = read(imds); % reads the first image
img2 = read(imds); % reads the next image
whos img1 img2
  Name        Size                Bytes  Class    Attributes

  img1      480x640x3            921600  uint8              
  img2      384x512x3            589824  uint8              

Transform all the images in the datastore to a specified target size.

targetSize = [224,224];
imdsReSz = transform(imds,@(x) imresize(x,targetSize));

Read the images and display their sizes.

imgReSz1 = read(imdsReSz);
imgReSz2 = read(imdsReSz);
whos imgReSz1 imgReSz2
  Name            Size                Bytes  Class    Attributes

  imgReSz1      224x224x3            150528  uint8              
  imgReSz2      224x224x3            150528  uint8              

Display the resized images.

subplot(121); imshow(imgReSz1); axis on; title('Resized Street1.jpg');
subplot(122); imshow(imgReSz2); axis on; title('Resized peppers.png');

Input Arguments

collapse all

Input datastore. For a complete list of built-in datastores, see Select Datastore for File Format or Application. You can also specify a custom datastore.

Function that transforms the data, specified as a function handle. The function takes data as an input and returns the transformed data, based on the transformations defined in fcn.

The transform function must have this signature and dataIn must be of the same form as the data that is returned by using the read function.

function dataOut = transformFcn(dataIn)
..
end

Alternatively, you can define your transform function fcn to use additional information about the data returned by the read function. To use this alternative definition, you must specify the value of IncludeInfo to be true. In this case, the transformation function must have this signature.

function [dataOut,infoOut] = transformFcn(dataIn,infoIn)
..
end

Example: @transformFcn

Data Types: function_handle

Include information from read function, specified as either true or false. The read function returns information about the extracted data in an info struct. For more information, see the read function page.

When you set the value of IncludeInfo to true, you must use the alternative signature for your transform function fcn.

Output Arguments

collapse all

New datastore with transformed data, returned as a TransformedDatastore object.

Introduced in R2019a