Effacer les filtres
Effacer les filtres

How to sort the files obtained by ImageDatastore?

28 vues (au cours des 30 derniers jours)
Canberk Suat Gurel
Canberk Suat Gurel le 26 Août 2018
Commenté : Nazmul Hasan le 6 Jan 2023
I used imageDatastore to obtain the directory of all the images in a folder.
Images = 'D:\Google Drive\MARYLAND\ENPM808 Independent Study\Independent Study\3D_map_poses\rgb';
rgb_image = imageDatastore(fullfile(Images),'IncludeSubfolders',false,'FileExtensions','.png','LabelSource','none');
But I realized that the order of the directories is not the same as the File Explorer and hence the generated video after processing the frames is erroneous.
Instead I need this to be in increasing order, (e.g. 1,3,7,9,10,16,...).
How can I do this? Thanks!
  5 commentaires
Image Analyst
Image Analyst le 26 Août 2018
So, did Stephen's utility, described below in his answer, work for you? (Did you even see it?) If so, you can mark it as "Accepted" to give him credit for helping you.
Canberk Suat Gurel
Canberk Suat Gurel le 26 Août 2018
I have just got the chance to try it. I "Accepted" the answer.

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 26 Août 2018
Modifié(e) : Stephen23 le 29 Déc 2022
I don't know if it is possible to sort the ImageDatastore object itself, but you can certainly sort the filenames yourself: one option is to download my FEX submission natsortfiles, which was written to solve the exact file-order problem that you are having:
natsortfiles has plenty of examples in its help, the online description, and the HTML documentation, so you should not have any problems using it. You will probably have to specify the order of the files when calling ImageFileStore, by supplying the first argument as the correctly sorted filenames, something like this (untested):
P = 'D:\Google Drive\MARYLAND\ENPM808 Independent Study\Independent Study\3D_map_poses\rgb';
S = dir(fullfile(P,'*.png'));
S = natsortfiles(S);
F = fullfile({S.folder},{S.name});
I = imageDatastore(F,...)
I don't have imageDatastore, so you will have to experiment with this yourself.
Note that you can avoid this whole problem by using sufficient leading zeros on all of the filenames, e.g. use (0001).png instead of (1).png, then the filenames will always be sorted into the correct order (for <10000 files).
  1 commentaire
emirhan toprak
emirhan toprak le 22 Jan 2021
it helped alot thnak you Stephen <3

Connectez-vous pour commenter.

Plus de réponses (1)

Stephen23
Stephen23 le 31 Août 2022
Modifié(e) : Stephen23 le 29 Déc 2022
A very neat and simple approach (from an answer by Pranay Koppula here) is to apply NATSORTFILES() to the FILES property of the IMAGEDATASTORE object:
obj = imageDatastore(..);
obj.Files = natsortfiles(obj.Files);
The function NATSORTFILES() can be downloaded here:
As Pranay Koppula wrote, it is unclear if this affects other attributes of the object.
  1 commentaire
Nazmul Hasan
Nazmul Hasan le 6 Jan 2023
I also used the same appraoch and got perfect results.

Connectez-vous pour commenter.

Produits


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by