필터 지우기
필터 지우기

Select random images from each subfolder with ceiling

조회 수: 6 (최근 30일)
Younghoon Kim
Younghoon Kim 2022년 10월 13일
댓글: Younghoon Kim 2022년 10월 22일
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.

답변 (1개)

V Sairam Reddy
V Sairam Reddy 2022년 10월 20일
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
Younghoon Kim
Younghoon Kim 2022년 10월 22일
Looks more complicated than I thought but I think I could finally do it.
Thank you!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by