MATLAB Answers

How to load a set of images which have the same name in different subfolders for further image processing

1 view (last 30 days)
gui li
gui li on 3 Sep 2020
Commented: gui li on 7 Sep 2020
I have a parent folder.
The parent folder has several subfolders named 01, 02, 03, ...
Each subfolder have several images named tile_x001_y001, tile_x001_y002, tile_x001_y003, tile_x002_y001, tile_x002_y002, tile_x002_y003 ....
As you can see, the name of image indicates the information of image position. It would be used for image stitch after image processing.
It should be noted that the name of images in all subfolders is the same, as shown in the pictures.
Now I want to load the images with the same name in different subfolders for further image processing.
How can I do that? Does anybody can give me any suggestion?
Thank you very much in advance.
Gui

  0 Comments

Sign in to comment.

Answers (1)

Mohammad Sami
Mohammad Sami on 3 Sep 2020
You can use the dir function to get the list of all the files in the folder and subfolder
listofallpngfiles = dir(fullfile(pwd,'**','*.png'));
listofallpngfiles = struct2table(listofallpngfiles);
fileswithsamename = unique(listofallpngfiles.name);
for i = 1:length(fileswithsamename)
name = fileswithsamename{i};
idx = strcmp(listofallpngfiles.name,name);
files = listofallpngfiles(idx,:);
filepaths = fullfile(files.folder,files.name);
% do something with the file
end

  3 Comments

gui li
gui li on 3 Sep 2020
Thank you very much, Mohammad.
If I want to show the images (which have the same name) based on the sequence of subfolder, how should I do?
Thank you again.
Gui
Mohammad Sami
Mohammad Sami on 3 Sep 2020
You can use the montage to display multiple images on a figure. https://www.mathworks.com/help/images/ref/montage.html
Another option could be to use imtile function to create a tiled image from multiple images and then use imshow to show it.
https://www.mathworks.com/help/matlab/ref/imtile.html

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!