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

Marry M
Marry M le 3 Fév 2016
thank you for your respond, as i run your recommended code, it gives the S=[]!! so the numel(S) would be 0
Stephen23
Stephen23 le 3 Fév 2016
Modifié(e) : Stephen23 le 3 Fév 2016
You are right, I fixed my answer.
Marry M
Marry M le 3 Fév 2016
thank you, but have the same problem!!!
Stephen23
Stephen23 le 4 Fév 2016
Modifié(e) : Stephen23 le 4 Fév 2016
There could be many reasons why this is not working, but I can only guess if you do not give more precise information about what you are doing. My mind reading ability is a bit rusty, so you actually need to tell us more information than just "but have the same problem!"
  • What file extension do the image files have? You did not tell us anything about the file format, so I wrote the code to locate the .jpg file extension. You can change this to suit your image format.
  • Often a description is not adequate: please show us the exact output of this command: ls (both for the current directory and for one the directories containing the images.
  • Are these sub-directories located within the current directory, or somewhere else?
I tested my code on some directories (named DD.MM.2016, exactly as per your question) containing .jpg images, and it successfully found them and read their data.
Marry M
Marry M le 4 Fév 2016
Modifié(e) : Stephen23 le 4 Fév 2016
thank you for helping me and sorry for my short comments. the code and results attached here
S = dir('C:\Users\Ma\Documents\MATLAB\*.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);
figure, imshow (X)
end
end
the screenshot of workspace:
Stephen23
Stephen23 le 4 Fév 2016
Modifié(e) : Stephen23 le 4 Fév 2016
Judging by the screenshot of your workspace the code has identified three subdirectories, and looped over them without error. We know this because the loop variable m is 3, equal to the numel(N) (the directory names). So identifying the subdirectoreis worked okay. It would seem that the code cannot identify any image files within those subdirectories.
You have not answered my earlier questions:
  • tell us the file extensions of the image files
  • please show us the output of ls when run inside one of those subdirectories.
I don't just write questions for my own entertainment, it is because we need that information so that we can help you.
Marry M
Marry M le 4 Fév 2016
thank you for your time and consideration. your right, the code can not identify images, i have 3 folders (22.01.2016, 23.01.2016 and 24.01.2016) and each one has one jpg image and as i run the code for both for 'the current directory and for one the directories containing the images' the out put for the first one i attached already, for the second one (one directory):
i hope to give you the best reply
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

Marry M
Marry M le 4 Fév 2016
thank you, its working, regards
F Sp
F Sp le 7 Juin 2020
Thank you, this was extremely helpful for me too!!
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
Or for batch construction of the names,
filenames = fullfile( {fileList.folder}, {fileList.name} );
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 .
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 Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by