- “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 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Turbulence Analysis
el 27 de Nov. de 2020
Respondida: Image Analyst
el 21 de Ag. de 2024 a las 15: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 comentarios
Respuestas (2)
Arjun
el 21 de Ag. de 2024 a las 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 comentarios
Ver también
Categorías
Más información sobre Environment and Settings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!