read image from different folders?
이전 댓글 표시
hi all, i have 20 folders which named with dates (i.e. 1.1.2016, 2.1.2016.....20.1.2016) and each folder has two images, please let me know how can i read each image. it is important for me to read images respectively. for example: read image 1 in day one then image 2 day one, image 1 day 2.....
채택된 답변
추가 답변 (1개)
Image Analyst
2016년 2월 4일
I know you already accepted a solution, but here's the code I usually post for questions like this, for whatever it's worth. Change the filepattern to get the file types you want, if they're not jpg:
% Start with a folder and get a list of all subfolders.
% Finds and prints names of all files in
% that folder and all of its subfolders.
% Similar to imageSet() function in the Computer Vision System Toolbox: http://www.mathworks.com/help/vision/ref/imageset-class.html
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
% Define a starting folder.
start_path = fullfile(matlabroot, '\toolbox');
if ~exist(start_path, 'dir')
start_path = matlabroot;
end
% Ask user to confirm or change.
uiwait(msgbox('Pick a starting folder on the next window that will come up.'));
topLevelFolder = uigetdir(start_path);
if topLevelFolder == 0
return;
end
% Get list of all subfolders.
allSubFolders = genpath(topLevelFolder);
% Parse into a cell array.
remain = allSubFolders;
listOfFolderNames = {};
while true
[singleSubFolder, remain] = strtok(remain, ';');
if isempty(singleSubFolder)
break;
end
listOfFolderNames = [listOfFolderNames singleSubFolder];
end
numberOfFolders = length(listOfFolderNames)
% Process all image files in those folders.
for k = 1 : numberOfFolders
% Get this folder and print it out.
thisFolder = listOfFolderNames{k};
fprintf('Processing folder %s\n', thisFolder);
% Get ALL files.
filePattern = sprintf('%s/*.jpg', thisFolder);
baseFileNames = dir(filePattern);
% % Get m files.
% filePattern = sprintf('%s/*.m', thisFolder);
% baseFileNames = dir(filePattern);
% % Add on FIG files.
% filePattern = sprintf('%s/*.fig', thisFolder);
% baseFileNames = [baseFileNames; dir(filePattern)];
% Now we have a list of all files in this folder.
numberOfImageFiles = length(baseFileNames);
if numberOfImageFiles >= 1
% Go through all those files.
for f = 1 : numberOfImageFiles
fullFileName = fullfile(thisFolder, baseFileNames(f).name);
fprintf(' Processing file %s\n', fullFileName);
figure; % Comment out this line if you want them all in the same axes.
imshow(fullFileName);
drawnow;
end
else
fprintf(' Folder %s has no files in it.\n', thisFolder);
end
end
By the way, if you're doing image analysis, you should not use jpg if at all possible because of JPG artifact corruption.
댓글 수: 6
Marry M
2016년 2월 4일
F Sp
2020년 6월 7일
Thank you, this was extremely helpful for me too!!
Image Analyst
2020년 6월 7일
Since this question was asked they've added the capability for dir() to go into subfolders. Just use **/*.png instead of */png:
filePattern = fullfile(folder, '**/*.png');
fileList = dir(filePattern); % Get all PNG files in folder and subfolders of that folder.
for k = 1 : length(fileList)
thisFileName = fullfile(fileList(k).folder, fileList(k).name)
% Now process it...
end
Walter Roberson
2020년 6월 7일
Or for batch construction of the names,
filenames = fullfile( {fileList.folder}, {fileList.name} );
lydie Nsangou
2021년 3월 25일
thanks a lot it was very helpful
@Image Analyst please is it possible to store all the images in a list or array? later on i would like to pick just a few a them .
Image Analyst
2021년 3월 25일
Yes, but you may run out of memory:
rgbImage{k, f} = imread(fullFileName); % Store in cell array
imshow(rgbImage{k, f});
카테고리
도움말 센터 및 File Exchange에서 Convert Image Type에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

