transform
Transform datastore
Syntax
Description
transforms one or more input datastores using the transformation functiondsnew
= transform(ds1,ds2,...,dsN
,@fcn
)fcn
and returns the transformed datastoredsnew
.fcn
can be placed before or after all of the input datastores in the call to thetransform
function.
uses an alternative definition of the transform functiondsnew
= transform(ds1,ds2,...,dsN
,@fcn
,'IncludeInfo',IncludeInfo
)fcn
. The alternative definition enables you to use the additional information returned by theread
function of the datastore.
Examples
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 anImageDatastore
with two images.
imd =成像atastore({'street1.jpg','peppers.png'})
imds = ImageDatastore with properties: Files: { ' .../devel/bat/Bdoc22a/build/matlab/toolbox/matlab/demos/street1.jpg'; ' .../devel/bat/Bdoc22a/build/matlab/toolbox/matlab/imagesci/peppers.png' } Folders: { '/mathworks/devel/bat/Bdoc22a/build/matlab/toolbox/matlab/demos'; '/mathworks/devel/bat/Bdoc22a/build/matlab/toolbox/matlab/imagesci' } 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 imageimg2 = read(imds);% reads the next imagewhosimg1img2
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); whosimgReSz1imgReSz2
Name Size Bytes Class Attributes imgReSz1 224x224x3 150528 uint8 imgReSz2 224x224x3 150528 uint8
Display the resized images.
subplot(121); imshow(imgReSz1); axison; title('Resized Street1.jpg'); subplot(122); imshow(imgReSz2); axison; title('Resized peppers.png');
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 anImageDatastore
with one image.
imds1 = imageDatastore({'ngc6543a.jpg'})
imds1 = ImageDatastore with properties: Files: { ' .../devel/bat/Bdoc22a/build/matlab/toolbox/matlab/demos/ngc6543a.jpg' } Folders: { '/mathworks/devel/bat/Bdoc22a/build/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 anImageDatastore
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); whosimg1img_redimg_greenimg_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 inimds1
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.
imshow(tile)
Input Arguments
ds1,ds2,...,dsN
—Input datastores
datastore objects
Input datastores. For a complete list of built-in datastores, seeSelect Datastore for File Format or Application. You also can specify a custom datastore.
@fcn
—Function that transforms data
function handle
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 infcn
.
The transform function must have this signature andds1_data,ds2_data,...dsN_data
must be of the same form as the data that is returned by using theread
function.
functiondataOut = transformFcn(ds1_data,ds2_data,...dsN_data)..end
Alternatively, you can define your transform functionfcn
to use additional information about the data returned by theread
function. To use this alternative definition, you must specify the value ofIncludeInfo
to betrue
. In this case, the transformation function must have this signature.
function[dataOut,infoOut] = transformFcn(ds1_data,ds2_data,...dsN_data,ds1_info,ds2_info...dsN_info)..end
Example:@transformFcn
Data Types:function_handle
IncludeInfo
—Include information fromread
function
true
|false
Include information fromread
function, specified as eithertrue
orfalse
. Theread
function returns information about the extracted data in aninfo
struct. For more information, see theread
function page.
When you set the value ofIncludeInfo
totrue
, you must use the alternative signature for your transform functionfcn
.
Output Arguments
dsnew
— New datastore with transformed data
TransformedDatastore
object
New datastore with transformed data, returned as aTransformedDatastore
object.
Extended Capabilities
Thread-Based Environment
Run code in the background using MATLAB®backgroundPool
or accelerate code with Parallel Computing Toolbox™ThreadPool
.
Usage notes and limitations:
In a thread-based environment, you can use
transform
only with the following datastores:ImageDatastore
objectsCombinedDatastore
orTransformedDatastore
objects you create fromImageDatastore
objects by usingcombine
ortransform
You can use
transform
with other datastores if you have Parallel Computing Toolbox™. To do so, run the function using a process-backed parallel pool instead of usingbackgroundPool
orThreadPool
(use eitherProcessPool
orClusterPool
).
For more information, seeRun MATLAB Functions in Thread-Based Environment.
Version History
Abrir ejemplo
Tiene una versión modificada de este ejemplo. ¿Desea abrir este ejemplo con sus modificaciones?
第一de MATLAB
Ha hecho clic en un enlace que corresponde a este comando de MATLAB:
Ejecute el comando introduciéndolo en la ventana de comandos de MATLAB. Los navegadores web no admiten comandos de MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina(Español)
- Canada(English)
- United States(English)
Europe
- Belgium(English)
- Denmark(English)
- Deutschland(Deutsch)
- España(Español)
- Finland(English)
- France(Français)
- Ireland(English)
- Italia(Italiano)
- Luxembourg(English)
- Netherlands(English)
- Norway(English)
- Österreich(Deutsch)
- Portugal(English)
- Sweden(English)
- Switzerland
- United Kingdom(English)