为图像集合创建一个数据存储,并对数据存储中的所有图像应用相同的变换。例如,将集合中的所有图像调整到指定的目标大小。
创建一个包含两个图像的 ImageDatastore。
imds = imageDatastore({'street1.jpg','peppers.png'})
imds =
ImageDatastore with properties:
Files: {
' .../devel/bat/Bdoc20b/build/matlab/toolbox/matlab/demos/street1.jpg';
' .../devel/bat/Bdoc20b/build/matlab/toolbox/matlab/imagesci/peppers.png'
}
Folders: {
'/mathworks/devel/bat/Bdoc20b/build/matlab/toolbox/matlab/demos';
'/mathworks/devel/bat/Bdoc20b/build/matlab/toolbox/matlab/imagesci'
}
AlternateFileSystemRoots: {}
ReadSize: 1
Labels: {}
SupportedOutputFormats: ["png" "jpg" "jpeg" "tif" "tiff"]
DefaultOutputFormat: "png"
ReadFcn: @readDatastoreImage
读取所有图像。请注意,该数据存储包含不同大小的图像。
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
将数据存储中的所有图像变换为指定的目标大小。
targetSize = [224,224];
imdsReSz = transform(imds,@(x) imresize(x,targetSize));
读取图像并显示其大小。
imgReSz1 = read(imdsReSz);
imgReSz2 = read(imdsReSz);
whos imgReSz1 imgReSz2
Name Size Bytes Class Attributes
imgReSz1 224x224x3 150528 uint8
imgReSz2 224x224x3 150528 uint8
显示调整大小后的图像。
subplot(121); imshow(imgReSz1); axis on; title('Resized Street1.jpg');
subplot(122); imshow(imgReSz2); axis on; title('Resized peppers.png');