TransformedDatastore - Datastore to transform underlying datastore - MATLAB (original) (raw)

Datastore to transform underlying datastore

Description

Use a TransformedDatastore object to transform, or process, data read from an underlying datastore.

Creation

You can create a TransformedDatastore object using the transform function. For example, dsnew = transform(ds1_data,ds2_data,...dsN_data,@fcn) creates a datastore that transforms one or more datastores ds1_data,ds2_data,...dsN_data using the transformation function fcn.

Properties

expand all

UnderlyingDatastores — Underlying datastores

cell array of datastore objects

Underlying datastores, returned as a cell array of datastore objects.

Transforms — Set of transform functions

cell array

Set of transformation functions, specified as a cell array of function handles.

Data Types: cell

IncludeInfo — Include information from read function

logical vector

Include information from read function, specified as a logical vector. For each value of IncludeInfo that istrue, the transformed datastore uses the alternative signature of the corresponding transform function in transformSet.

The read function returns information about the extracted data in an info struct. For more information, see the read function page.

Data Types: logical

SupportedOutputFormats — Formats supported for writing

string row vector

This property is read-only.

Formats supported for writing, returned as a row vector of strings. This property specifies the possible output formats when using writeall to write output files from the datastore.

Object Functions

Examples

collapse all

Apply Transformation to Collection of Images

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: {
                          ' .../build/runnable/matlab/toolbox/matlab/demos/street1.jpg';
                          ' .../build/runnable/matlab/toolbox/matlab/matlab_images/png/peppers.png'
                          }
                 Folders: {
                          ' .../Bdoc24b.2855429/build/runnable/matlab/toolbox/matlab/demos';
                          ' .../build/runnable/matlab/toolbox/matlab/matlab_images/png'
                          }
AlternateFileSystemRoots: {}
                ReadSize: 1
                  Labels: {}
  SupportedOutputFormats: ["png"    "jpg"    "jpeg"    "tif"    "tiff"]
     DefaultOutputFormat: "png"
                 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.

tiledlayout(1,2); nexttile imshow(imgReSz1); axis on; title('Resized Street1.jpg') nexttile imshow(imgReSz2); axis on; title('Resized peppers.png')

Figure contains 2 axes objects. Axes object 1 with title Resized Street1.jpg contains an object of type image. Axes object 2 with title Resized peppers.png contains an object of type image.

Transform Multiple Datastores

Create multiple datastore objects and apply the same transformation to all the datastores. For instance, combine multiple images into one rectangular tiled image.

Create an ImageDatastore with one image.

imds1 = imageDatastore({'ngc6543a.jpg'})

imds1 = ImageDatastore with properties:

                   Files: {
                          ' .../build/runnable/matlab/toolbox/matlab/demos/ngc6543a.jpg'
                          }
                 Folders: {
                          ' .../Bdoc24b.2855429/build/runnable/matlab/toolbox/matlab/demos'
                          }
AlternateFileSystemRoots: {}
                ReadSize: 1
                  Labels: {}
  SupportedOutputFormats: ["png"    "jpg"    "jpeg"    "tif"    "tiff"]
     DefaultOutputFormat: "png"
                 ReadFcn: @readDatastoreImage

Read the image into the workspace to create an image file from each color channel in the original image.

rgbImage = imread('ngc6543a.jpg'); imwrite(rgbImage(:,:,1),'nebula_red.jpg'); imwrite(rgbImage(:,:,2),'nebula_green.jpg'); imwrite(rgbImage(:,:,3),'nebula_blue.jpg');

Create an ImageDatastore object for each single-channel image.

imdsR = imageDatastore({'nebula_red.jpg'}); imdsG = imageDatastore({'nebula_green.jpg'}); imdsB = imageDatastore({'nebula_blue.jpg'});

Read the image stored in each datastore and display their sizes.

imOriginal = read(imds1); img_red = read(imdsR); img_green = read(imdsG); img_blue = read(imdsB); whos img1 img_red img_green img_blue

Name Size Bytes Class Attributes

img_blue 650x600 390000 uint8
img_green 650x600 390000 uint8
img_red 650x600 390000 uint8

Transform all of the datastores by combining all the images into one rectangular tiled image. Convert the color image in imds1 to grayscale so that its dimensions match those of the other images.

tds1 = transform(imds1,imdsR,imdsG,imdsB, @(x1,x2,x3,x4) [rgb2gray(x1),x2;x3,x4]); tile = read(tds1);

Display the tiled image.

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

Version History

Introduced in R2019a