Clear Filters
Clear Filters

Select random images from each subfolder with ceiling

6 views (last 30 days)
From a single imagedatastore, I am trying to select 10000 random images from 100 subfolders, each containing 400 to 10000 images, with
each subfolder providing no more than 500 images.
As I read others questions, it looks like I have to
  1. Make a new imagedatastore that contains 500 random images from each subfolder (so about 500 images x 100 folders).
  2. Select 10000 random images from 1. using randperm().
However, I cannot find a way to do the first task.

Answers (1)

V Sairam Reddy
V Sairam Reddy on 20 Oct 2022
I understand that you want a new imageDataStore that contains 500 random images from each of the 100 subfolders.
You can split this task into 2 subtasks :
  1. Get a imageDataStore from a single subfolder that contains 500 random images from that subfolder.
  2. Combine all such imageDataStores into one big ImageDataStore.
Please follow the below mentioned code :
% Create Image Data Store for a single subfolder.
subFolder1 = fullfile(matlabroot,'toolbox','matlab',{'demos','imagesci'});
exts = {'.jpg','.png','.tif'};
imds_subFolder1 = imageDatastore(subFolder1,'LabelSource','foldernames','FileExtensions',exts);
% Randomly select 500 Images from single subfolder.
total_no_of_Images = length(imds_subFolder1.Files);
RandIndices = randperm(total_no_of_Images);
no_of_reqImages = 500;
% Get first 500 random indices.
indices = RandIndices(1:no_of_reqImages);
% Use 'subset' to extract subset of a DataStore.
imds_subFolder1_with500Images = subset(imds_subFolder1,indices);
% Create Image Data store for another subfolder in similar way.
imds_subFolder2_with500Images = imageDatastore({'street1.jpg','street2.jpg','peppers.png'});
% Combine both Image Datastores.
imdsCombined = combine(imds_subFolder2_with500Images,imds_subFolder1_with500Images);
%% Automate for 100 Subfolders
% Place all the 100 different Image Datastores in a cell array.
imds_subFolder = cell(100,1);
for i=1:100
imds_subFolder{i} = imds_ith_subFolder_with500Images;
end
% Combine all the Image Datastores present in the cell array.
CombinedImds = imds_subFolder{1};
for i=2:100
CombinedImds = combine(CombinedImds,imds_subFolder{i});
end
Please refer to Data Store to know more about 'subset' and 'combine' functions, mainly 'Create Subset Datastore with Randomly Selected Files' section in subset documentation.
  1 Comment
Younghoon Kim
Younghoon Kim on 22 Oct 2022
Looks more complicated than I thought but I think I could finally do it.
Thank you!

Sign in to comment.

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!