Randomly selecting images from a directory
2 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
J. Alex Lee
2021-5-25
Use "dir" and "randperm"
4 个评论
J. Alex Lee
2021-5-25
eagle eyes! yea, i have nothing really to add to what Jan answered in previous post.
更多回答(1 个)
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!