- “imread”: https://www.mathworks.com/help/releases/R2020a/matlab/ref/imread.html?searchHighlight=imread&s_tid=doc_srchtitle
- “imagesc”: https://www.mathworks.com/help/releases/R2020a/matlab/ref/imagesc.html?searchHighlight=imagesc&s_tid=doc_srchtitle
- “dir”: https://www.mathworks.com/help/releases/R2020a/matlab/ref/dir.html?searchHighlight=dir&s_tid=doc_srchtitle
Read multiple images from folder and subfolder within a for loop
14 次查看(过去 30 天)
显示 更早的评论
Hi,
I intend to read image files placed inside folder (.jpg) and subfolder (.bmp) within a for loop opeartion. Could someone please help me out with this ??
for f = 1:1:1
if (f>=1) && (f<=9)
fname_strt = 'B0000' ;
elseif (f>=10) && (f<=99)
fname_strt='B000';
elseif (f>=100) && (f<=999)
fname_strt='B00';
else
fname_strt='B0';
end
fname_end = num2str(f);
fname = strcat(fname_strt,fname_end,'.jpg');
I=imread(fname);
G = imagesc(x,y,flipud(I));
G.AlphaData = 0.5;
hold on;
%%%%%%% Needs to be read from subfolder
if (f>=1) && (f<=9)
fname_strt = 'B0000' ;
elseif (f>=10) && (f<=99)
fname_strt='B000';
elseif (f>=100) && (f<=999)
fname_strt='B00';
else
fname_strt='B0';
end
fname_end = num2str(f);
fname = strcat(fname_strt,fname_end,'.jpg');
h=readimx(fname);
H1 = imagesc(flipud(h));
H1.AlphaData = 0.5;
axis equal
hold on;
end
0 个评论
回答(2 个)
Arjun
2024-8-21,10:24
Hi,
As per my understanding, you want to read image data stored in a folder and a subfolder within a for loop.
I am assuming that you know the path to main folder, subfolder and number of files ending in .jpg are more in number.
To read image files from a folder and subfolder in MATLAB, you can use the “dir” function to list the files and construct the paths accordingly. Using the “imread” function, you can read the image file being referred by the corresponding path.
Kindly refer to the sample code below to read image files.
% Define directories
mainDir = 'path/to/main/folder'; % Replace with your main folder path
subDir = 'path/to/main/folder/subfolder'; % Replace with your subfolder path
% Get list of .jpg files in the main directory
jpgFiles = dir(fullfile(mainDir, '*.jpg'));
% Get list of .bmp files in the subdirectory
bmpFiles = dir(fullfile(subDir, '*.bmp'));
% Loop through the number of files
numFiles = length(jpgFiles);
for f = 1:numFiles
% Construct full file path for .jpg files
jpgFilePath = fullfile(mainDir, jpgFiles(f).name);
% Read and process the .jpg image
I = imread(jpgFilePath);
G = imagesc(flipud(I)); % Assuming x and y are defined elsewhere
G.AlphaData = 0.5;
hold on;
% Construct full file path for .bmp files
if f <= length(bmpFiles)
bmpFilePath = fullfile(subDir, bmpFiles(f).name);
% Read and process the .bmp image
h = imread(bmpFilePath); % Use imread for .bmp files as well
H1 = imagesc(flipud(h));
H1.AlphaData = 0.5;
axis equal;
hold on;
end
end
hold off;
Please refer to the below MATLAB documentation links for “imread”, “imagesc” and “dir” function for better understanding.
I hope this will help!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!