Effacer les filtres
Effacer les filtres

read image from different folders?

20 vues (au cours des 30 derniers jours)
Marry M
Marry M le 2 Fév 2016
Commenté : Image Analyst le 25 Mar 2021
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.....

Réponse acceptée

Stephen23
Stephen23 le 2 Fév 2016
Modifié(e) : Stephen23 le 3 Fév 2016
This should get you started:
S = dir('*.2016');
D = [S.isdir];
N = {S(D).name};
V = datenum(N,'dd.mm.yyyy');
[~,X] = sort(V);
for m = X(:).'
S = dir(fullfile(N{m},'*.jpg'));
F = {S.name};
for n = 1:numel(F)
str = fullfile(N{m},F{n});
X = imread(str);
... do something with image data X
end
end
  9 commentaires
Stephen23
Stephen23 le 4 Fév 2016
Modifié(e) : Stephen23 le 4 Fév 2016
This is my third and final attempt to get information on the image files:
  • what are their file extensions?
  • please show us a screenshot of inside one of the directories containing images.
I have clearly asked three times for information on the image files, in particular on their file extensions and file formats. Without this information I cannot help you any more. You don't have to use MATLAB to get this information.
I have no idea how you got that last workspace, so it does not tell my anything. Note that you do not need to run my code from inside those subdirectories, just from their root/parent directory. Running ls would be nice though, but that seems to be a disappearing dream.
Marry M
Marry M le 4 Fév 2016
thank you for your help.

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 4 Fév 2016
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 commentaires
lydie Nsangou
lydie Nsangou le 25 Mar 2021
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
Image Analyst le 25 Mar 2021
Yes, but you may run out of memory:
rgbImage{k, f} = imread(fullFileName); % Store in cell array
imshow(rgbImage{k, f});

Connectez-vous pour commenter.

Catégories

En savoir plus sur Image Processing Toolbox dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by