How to read input images from the folder sequentially

Hello, In my work I'm giving input as a folder of facial photos which consists of 10 images. The output enhanced images are getting stored in separate folder But the problem is, it is not getting stored in the same order as that of the images passed through input folder. The images are read randomly.

回答 (2 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2018 年 4 月 9 日
編集済み: KALYAN ACHARJYA 2018 年 4 月 9 日

0 投票

% Save all images name in a sequence manner before doing the operation
% names for example im1,im2,im3...
%Save the folder of images in the current directory
path_directory='folder_name'; % 'Folder name'
original_files=dir([path_directory '/*.jpg']);
for k=1:length(original_files)
filename=[path_directory '/' original_files(k).name];
image_orginal=imread(filename);
% Image read is done
%%Image Operation as per your work
end

5 件のコメント

Stephen23
Stephen23 2018 年 8 月 15 日
Note that using fullfile is simpler than concatenating strings and file separators.
KALYAN ACHARJYA
KALYAN ACHARJYA 2018 年 8 月 15 日
Agree @Stephan Sir, Thank u
Jan
Jan 2018 年 8 月 15 日
Using fullfile is safer as [a, '/', b], because it considers existing file separators.
Halee Scott
Halee Scott 2022 年 3 月 18 日
Where would fullfile be used in the script then? Sorry I am new to coding and adapting this to create a for loop that changes file types (.oib to .tiff) and saves the new files in a different folder.

サインインしてコメントする。

Stephen23
Stephen23 2018 年 8 月 15 日
編集済み: Stephen23 2020 年 4 月 23 日

0 投票

Presumably the filenames have numbers in them, and you are getting them listed not in numeric order...One solution is to download my FEX submission natsortfiles, which was written to solve that exact problem:
There are plenty of examples in the Mfile, in the online description, and in the HTML documentation, so you should not have any problems using it. You will probably want something like this:
D = 'path of the folder where the files are';
S = dir(fullfile(D,'*.txt'));
F = natsortfiles({S.name});
for k = 1:numel(F)
im = imread(fullfile(D,F{k}))
...
end

5 件のコメント

Manuella Juwita
Manuella Juwita 2020 年 4 月 23 日
Hello Sir, can I ask how to read a video file and save it to multiple frames in numerical order? Because I keep getting back random frames not in order of the video progression, my code is this:
vid = VideoReader('vid.mp4');
numFrames = vid.NumberofFrames;
n = numFrames;
for i=1:1:n
frames = read(vid,i);
imwrite(frames,['C:mydirectory\'int2str(i),'.jpg']);
im(i)=image(frames);
end
Thanks in advance
Stephen23
Stephen23 2020 年 4 月 23 日
編集済み: Stephen23 2020 年 4 月 25 日
"Hello Sir, can I ask how to read a video file and save it to multiple frames in numerical order?"
They already are saved in numeric order.
The problem is that the OS does not sort them alphanumerically when returning a list of files, e.g. when queried via dir. In order to get the filenames sorted in numeric order, you basically have two choices:
  1. use a tool that can sort them into alphanumeric order when importing or processing the files, e.g. by downloading my FEX submission natsortfiles and following its examples.
  2. use adequate leading zeros so that a character sort gives the expected order. This would be a simple choice for your code because you know the total number of frames, e.g.:
D = 'C:mydirectory';
V = VideoReader('vid.mp4');
N = V.NumberofFrames;
Z = 1+floor(log10(N));
for k = 1:N
I = read(V,k);
F = sprintf('%0*u.jpg',Z,k);
imwrite(I,fullfile(D,F));
end
Manuella Juwita
Manuella Juwita 2020 年 4 月 23 日
Thank you very much for the prompt reply, Stephen. It was really helpful :)
Manuella Juwita
Manuella Juwita 2020 年 6 月 29 日
Hello,
I want to ask, how to use natsortfiles to sort alphanumerically overlaying plots in one figure? I find it difficult to match one plot to another because they have different directories and names.
Jan
Jan 2022 年 3 月 19 日
@Manuella Juwita: What are "alphanumerically overlaying plots in one figure"? Plots do not have directories.

サインインしてコメントする。

質問済み:

S.A
2018 年 4 月 9 日

コメント済み:

Jan
2022 年 3 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by