Loading specific Images from a folder

I have a folder of images, I would like to extract it as it appears in an array of my choosing.
E.G I have an array: [1 2 3];
This would extract the name of the first 3 images in this order.
E.G [1 2 3 5];
This would extract the name of the first 3 images and the 5th image also in this order.
I would like these Imagenames to be presented in a folder.
Is there a way to do this?

 Accepted Answer

path_directory='ddb1_fundusimages'; % 'Folder name'
original_files=dir([path_directory '/*.png']);
array1=[5 10 15];
j=1;
for k=1:length(array1)
filename=[path_directory '/' original_files(array1(j)).name];
j=j+1;
image1=imread(filename);
figure, imshow(image1);
% Do the Image Operation as per your work
end
Before doing the above operations , please ensure the all images are sorted in namewise.
(For reference see the attach image)

10 Comments

When placing the images in my folder through matlab, a few URL's threw errors, as a result they were skipped. Therefore, the folder does not have a perfect sequence.
Could your code be based on the exact order it was read from the folder. As in the array [1 2] represents the first and second image read from the folder. Which may be the Image001 and Image003.
How I preserve the order in my folder?
Save the images in namewise im1, im2, im3..etc, code take images based on array order sequence.
The code os perfectly working in my case.
Doesnt work for my intended use yet.
Is there a way then to take those images and save them as 1,2,3 ect... while preserving the order they are in now?
Or must it be a manual process?
Are the images name are random in your case?
Yes you can do that, check here
They have a theme:
Pos1 - People.jpg
Pos3 - People.jpg
Some entries are just missing.
Can you share the some 10 (sequence from starting) images, so that I can try on it?
use google drive
Please confirm-you want to call the images from folder as per array indexing values (not random)?
Ive used certain images from that folder, perhaps the 1st,3rd and 5th. I want to call the names of these images and save them in a cell perhaps.
E.G Ive used the 1st,3rd and 5th Images
Their names are:
Image001.jpg, Image004.jpg and Image007.jpg respectively, I would like to save the names of these images in a cell on matlab presented like this perhaps:
'Image001.jpg' (1st Image)
'Image004.jpg' (3rd Image)
'Image007.jpg' (5th Image)
However I would like to do so, by referrencing their order within the folder so i dont have to manually check for their names, hence, the array ([1 3 5]).
Do the first part, then think on sencond task (save in a cell). It is OK in my case. Whi I have run the above code, from my database it call the 5th image, 10th image and 15th image.
If the images number are less do the renaming and apply the code.
When the files are saved in "origional_files" using dir, their ordering becomes changed.
From:
Pos1 - People.jpg, Pos2 - People.jpg, Pos3 - People.jpg
To:
Pos1 - People.jpg, Pos100 - People.jpg, Pos1000 - People.jpg
Thus the images are not provided in order and the iterating doesnt work.
Ive managed to find the resources to do this, here is the code:
path_directory='People'; % 'Folder name'
original_files=dir([path_directory '\*.jpg']);
Ordered = {original_files.name};
Ordered=natsortfiles(Ordered);
ImagesRequired = {};
for i = k'
ImagesRequired{i} = Ordered{i};
end
ImagesRequired = ImagesRequired';
ImagesRequired(cellfun('isempty', ImagesRequired)) = [];
The function natsortfiles(), is user generated from this link:
This provides the exact format I was looking for.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!