How to take image from multi folders?

조회 수: 3 (최근 30일)
D. Frank
D. Frank 2020년 6월 19일
댓글: D. Frank 2020년 6월 22일
Hi everybody
I have a few thousand folders, each folder contains 2 images with information in their name, i want to take randomly 1 image from each folder and put all the taken images into one new folder in desktop for further calculation, please help, thank you very much.
  댓글 수: 4
Rik
Rik 2020년 6월 21일
You mean you want to read an image? If you know the file names you can just use imread.
D. Frank
D. Frank 2020년 6월 22일
Read image is the next step of computing these files, the main thing i want to do is taking 1 random image from each subfolder, and there are 2900 subfolders in 1 folder to add to path in matlab :((((((

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

채택된 답변

Monalisa Pal
Monalisa Pal 2020년 6월 21일
편집: Monalisa Pal 2020년 6월 22일
Hi,
'fetching a random image' part of the following code may help you
clearvars -global
clearvars
close all
clc
% creating destination folder
destination = 'destinationfolder';
mkdir(destination);
n_folders = 10; % say there are 10 folders
for i = 1:n_folders
% fetching the contents of the folders
foldername = ['folder_', num2str(i), '/'];
path = [foldername, '*.png']; % or other imgfile extensions
contents = dir(path);
% fetching a random image
if numel(contents) < 2
error([foldername, ' has only ', num2str(numel(contents)), ' image files']);
end
img_index = randi([1, numel(contents)], 1);
disp(['From ', foldername, ' copying ', contents(img_index).name]);
source = [foldername, contents(img_index).name];
copyfile(source, destination);
end
  댓글 수: 20
Monalisa Pal
Monalisa Pal 2020년 6월 22일
Mr. Frank, I misunderstood the directory structure earlier. Lets see if this works.
clearvars -global
clearvars
close all
clc
% creating destination folder
destination = 'Images_place';
mkdir(destination);
folder = 'Bigfolder';
sub_folders = dir(folder);
%n_folders = 2950; % say there are 2950 folders
% n_folders = numel(sub_folders) - 2;
exts = {'*.jpg', '*.gif', '*.png', '*.jpeg', '*.bmp'};
for i = 3:numel(sub_folders)
% fetching the contents of the folders
foldername = ['Bigfolder/', sub_folders(i).name, '/'];
contents_parts = cellfun(@(E) dir( fullfile(foldername, E) ), exts, 'uniform', 0);
contents = vertcat(contents_parts{:});
% fetching a random image
if isempty(contents)
fprintf(1, 'caution: folder "%s" had no useable images\n', foldername);
continue;
end
img_index = randi([1, numel(contents)], 1);
disp(['From ', foldername, ' copying ', contents(img_index).name]);
filename = fullfile(foldername, contents(img_index).name);
copyfile(filename, destination);
end
D. Frank
D. Frank 2020년 6월 22일
Dear Ms. Monalisa Pal and Mr. Roberson, the code ran smoothly, thank you very much for your patience and your help.

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

추가 답변 (0개)

카테고리

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