subset
创建数据存储或 FileSet 的子集
说明
示例
创建一个图像数据存储对象,然后创建该图像数据存储的子集。
为示例文件夹中的所有图像文件创建一个图像数据存储 imds
。然后,显示 imds
的 Files
属性。
folders = fullfile(matlabroot,'toolbox','matlab',{'demos','imagesci'}); exts = {'.jpg','.png','.tif'}; imds = imageDatastore(folders,'LabelSource','foldernames','FileExtensions',exts); imds.Files
ans = 8×1 cell array {'...\matlab\toolbox\matlab\demos\cloudCombined.jpg'} {'...\matlab\toolbox\matlab\demos\example.tif' } {'...\matlab\toolbox\matlab\demos\landOcean.jpg' } {'...\matlab\toolbox\matlab\demos\ngc6543a.jpg' } {'...\matlab\toolbox\matlab\demos\street1.jpg' } {'...\matlab\toolbox\matlab\demos\street2.jpg' } {'...\matlab\toolbox\matlab\imagesci\corn.tif' } {'...\matlab\toolbox\matlab\imagesci\peppers.png' }
创建一个包含 imds
的前四个文件的子集数据存储 subimds
,并检查 subimds
的 Files
属性。
indices = 1:4; subimds = subset(imds,indices); subimds.Files
ans = 4×1 cell array {'...\matlab\toolbox\matlab\demos\cloudCombined.jpg'} {'...\matlab\toolbox\matlab\demos\example.tif' } {'...\matlab\toolbox\matlab\demos\landOcean.jpg' } {'...\matlab\toolbox\matlab\demos\ngc6543a.jpg' }
创建一个图像数据存储,然后创建一个仅包含指定百分比数量文件的子集数据存储,这些文件是从原始数据存储中随机选择的。
为示例文件夹中的所有图像文件创建 imageDatastore
,并显示 Files
属性。此数据存储包含 8 个文件。
folders = fullfile(matlabroot,'toolbox','matlab',{'demos','imagesci'}); exts = {'.jpg','.png','.tif'}; imds = imageDatastore(folders,'LabelSource','foldernames','FileExtensions',exts); imds.Files
ans = 8×1 cell array {'...\matlab\toolbox\matlab\demos\cloudCombined.jpg'} {'...\matlab\toolbox\matlab\demos\example.tif' } {'...\matlab\toolbox\matlab\demos\landOcean.jpg' } {'...\matlab\toolbox\matlab\demos\ngc6543a.jpg' } {'...\matlab\toolbox\matlab\demos\street1.jpg' } {'...\matlab\toolbox\matlab\demos\street2.jpg' } {'...\matlab\toolbox\matlab\imagesci\corn.tif' } {'...\matlab\toolbox\matlab\imagesci\peppers.png' }
创建一组代表随机选择的文件子集的索引,该子集包含 60%
的文件。
nFiles = length(imds.Files); RandIndices = randperm(nFiles); nSixtyPercent = round(0.6*nFiles); indices = RandIndices(1:nSixtyPercent)
indices = 8 6 4 5 1
使用 indices
创建子集数据存储 submids
,并检查其 Files
属性。
subimds = subset(imds,indices); subimds.Files
ans = 5×1 cell array {'...\matlab\toolbox\matlab\imagesci\peppers.png' } {'...\matlab\toolbox\matlab\demos\street2.jpg' } {'...\matlab\toolbox\matlab\demos\ngc6543a.jpg' } {'...\matlab\toolbox\matlab\demos\street1.jpg' } {'...\matlab\toolbox\matlab\demos\cloudCombined.jpg'}
比较粗粒度分区和细粒度子集。
读取视频文件 xylophone.mp4
中的所有帧,并构造一个 ArrayDatastore
对象对其进行迭代。生成的对象有 141 个帧。
v = VideoReader("xylophone.mp4"); allFrames = read(v); arrds = arrayDatastore(allFrames,IterationDimension=4,OutputType="cell",ReadSize=4);
要提取一组特定的相邻帧,请创建四个粗粒度分区 arrds
。提取第二个分区,它有 35 个帧。
partds = partition(arrds,4,2); imshow(imtile(partds.readall()))
使用细粒度子集在指定索引处从 arrds
中提取六个不相邻的帧。
subds = subset(arrds,[67 79 82 69 89 33]); imshow(imtile(subds.readall()))
输入参数
输入数据存储或文件集,指定为 datastore
、FileSet
、DsFileSet
或 BlockedFileSet
对象。
要包含在子集中的文件的索引,指定为索引向量或逻辑向量。
索引向量必须包含要包含在子集
subds
中的文件的索引。逻辑向量的长度必须与输入
ds
中的文件数量相同。subset
方法将创建子集subds
,其中包含与逻辑向量中值为true
的元素对应的文件。
indices
的元素必须是唯一的。
数据类型: double
| logical
扩展功能
用法说明和限制:
在基于线程的环境中,您只能对以下数据存储使用
subset
:ImageDatastore
对象使用
combine
或transform
从ImageDatastore
对象创建的CombinedDatastore
、SequentialDatastore
或TransformedDatastore
对象
如果您有 Parallel Computing Toolbox™,则可以对其他数据存储使用
subset
。为此,请使用基于进程的并行池运行该函数,而不是使用backgroundPool
或ThreadPool
(使用ProcessPool
或ClusterPool
)。
有关详细信息,请参阅在基于线程的环境中运行 MATLAB 函数。
版本历史记录
在 R2019a 中推出
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)