Randomly selecting images from a directory

조회 수: 13 (최근 30일)
AK
AK 2021년 5월 25일
답변: Image Analyst 2021년 5월 25일
Hello,
I have a directory of 10000 JPEG files. I want to write a script that can randomly select 100 images from this directory and save these images to a new folder on my desktop. How do I do this?
Thank you!

채택된 답변

J. Alex Lee
J. Alex Lee 2021년 5월 25일
Use "dir" and "randperm"
  댓글 수: 4
J. Alex Lee
J. Alex Lee 2021년 5월 25일
eagle eyes! yea, i have nothing really to add to what Jan answered in previous post.
AK
AK 2021년 5월 25일
Thank you I was able to get it working!

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

추가 답변 (1개)

Image Analyst
Image Analyst 2021년 5월 25일
This is how I'd do it:
fileList = dir('*.jp*');
numFiles = length(fileList)
orderToUse = randperm(numFiles);
for k = 1 : numFiles
% Get what random index this one is.
index = orderToUse(k);
% Construct the full filename.
fullFileName = fullfile(fileList(index).folder, fileList(index).name);
fprintf('Processing #%d of %d:\n which is file #%d in the list : %s\n',...
k, numFiles, index, fullFileName);
% Now do something with the file....
end

카테고리

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