How to read images sequentially from a folder?

sourcefolder = 'E:\images';
filepattern = fullfile(sourcefolder, '*.png');
outputfolder = 'E:\images2';
srcFiles = dir(filepattern);
numImages = length(srcFiles);
for k = 1 : numImages
inputfilename = fullfile(sourcefolder, srcFiles(k).name);
outputfilename = fullfile(outputfolder, sprintf('histo_%d.png',k));
.....
....
....
end
My folder containing 1400 images. Whenever I am reading images using the above code and after performing any image processing its not happening sequentially. How can read images by one by one maintaing the sequence and can write to them by maintaining the same order.

2 comentarios

Chunru
Chunru el 5 de Jul. de 2022
you need to sort the srcFiles first according to your need.
Zara Khan
Zara Khan el 5 de Jul. de 2022
how to do that

Iniciar sesión para comentar.

 Respuesta aceptada

Stephen23
Stephen23 el 5 de Jul. de 2022
Editada: Stephen23 el 6 de Jul. de 2022
You can download the file NATSORTFILES by clicking the big blue download button here:
then unzip the file onto the MATLAB search path (e.g. into the current directory), and then use it like this:
srcFiles = dir(filepattern);
srcFiles = natsortfiles(srcFiles);

Más respuestas (2)

Pooja Kumari
Pooja Kumari el 5 de Jul. de 2022
Editada: Pooja Kumari el 5 de Jul. de 2022
Dear Zara Khan,
It is my understanding that you want to access the images present in the folder sequentially and after performing some operations, you want to store the output image in ‘output filename’.
But the issue you were facing is that when you are reading images using the provided code and after getting the input file name and performing some image processing, you were not getting the output sequentially is because the files stored in input file name is not sequential.
You can refer to the updated code given below:
sourcefolder = 'E:\images'; %path of your source folder
filepattern = (fullfile(sourcefolder, '*.png'));
srcFiles = dir(filepattern);
numImages = length(srcFiles);
outputfolder = 'E:\images2';
for i= 1: numel(srcFiles)
I =(imread(strcat(sourcefolder,'/',srcFiles(i).name)));
outputfilename = fullfile(outputfolder, sprintf('histo_%d.png',i));
.....
....
...
end
Please find attached a reference code for the above issue you were facing:
% I have 50 images named numerically '15_1_s', '15_2_s', ... '15_50_s' but
% the order in which these files are stored is shown in the image below:
path = '/MATLAB Drive/CV_Assignment2/dd/dd';
folder = (path);
files = dir(fullfile(folder,'*.jpg'));
nfiles = length((files));
mkdir(['Outputfolder',sprintf('%d',outputfolder)]); % similar to outputfolder.
for i= 1: numel(files)
I =(imread(strcat(path,'/',files(i).name)));
imwrite(imgx,strcat(path_output,'/',files(i).name));
%output will be stored sequentially .
end
For more information on how to sort files stored in a directory , you can refer to the below link:
You can refer to this file, if you want to sort the input files naturally:
Sincerely,
Pooja Kumari

1 comentario

Zara Khan
Zara Khan el 6 de Jul. de 2022
Thank you madam for very nice explanation of my problem. Thanks once again for the response. I have used the natsortfiles in this case and its working well till now. I will also give a try of your answer also. Thanks.

Iniciar sesión para comentar.

Chunru
Chunru el 5 de Jul. de 2022
Editada: Chunru el 5 de Jul. de 2022
% create some files
system('touch a.png');
system('touch c.txt');
pause(1) % make sure b is newer in datenum
system('touch b.txt');
pause(.5)
% get the files
f = dir('*.*');
{f.name}
ans = 1×5 cell array
{'.'} {'..'} {'a.png'} {'b.txt'} {'c.txt'}
% sort according to name
[~, idx] = sort({f.name})
idx = 1×5
1 2 3 4 5
fname = {f(idx).name}
fname = 1×5 cell array
{'.'} {'..'} {'a.png'} {'b.txt'} {'c.txt'}
% sort according to datetime
[~, idx] = sort([f.datenum])
idx = 1×5
2 3 5 1 4
fname = {f(idx).name}
fname = 1×5 cell array
{'..'} {'a.png'} {'c.txt'} {'.'} {'b.txt'}

6 comentarios

Zara Khan
Zara Khan el 5 de Jul. de 2022
will this work for .png images ? As you are only creating text files?
Chunru
Chunru el 5 de Jul. de 2022
This works for any file. You can sort according to file name, size, created data, and so on.
Zara Khan
Zara Khan el 5 de Jul. de 2022
Editada: Zara Khan el 5 de Jul. de 2022
My images are already sorted. but after image processing whenever I am storing those images that is not happening in order. Then how I will be sure that reading of images are happening in order? As they are not in order so comparisons of images becoming difficult. I am using loop to read images.
Chunru
Chunru el 5 de Jul. de 2022
What do you mean "your images are already sorted"? How do you compare your images? Give some examples.
Zara Khan
Zara Khan el 5 de Jul. de 2022
Editada: Zara Khan el 5 de Jul. de 2022
Sorted by names . Like they are img1, img2,......img1400. I have tried with natsortfiles also. But the function is telling undefined
You need to use the Add-On Explorer to install natsortfiles .

Iniciar sesión para comentar.

Categorías

Preguntada:

el 5 de Jul. de 2022

Editada:

el 6 de Jul. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by