how to read images one by one
조회 수: 2 (최근 30일)
이전 댓글 표시
채택된 답변
Stephen23
2015년 2월 18일
편집: Stephen23
2021년 4월 18일
If you have a cell array of the names, then use that. If you don't know the names, then use dir to read the names from the directory where they are saved. The MATLAB Wiki gives an complete working example of how to do this:
(Just scroll down a little to find the example using dir.)
One way to sort filenames into alphanumeric order is to download my FEX submission NATSORTFILES:
>> S = dir('*.txt');
>> S.name
ans =
'1.txt'
ans =
'10.txt'
ans =
'2.txt'
>> S = natsortfiles(S); % alphanumeric sort by filename
>> S.name
ans =
'1.txt'
ans =
'2.txt'
ans =
'10.txt'
댓글 수: 3
Joe Perkins
2017년 9월 14일
Unfortunately dir can't find the file pattern for my files. I have used the example straight from the FAQ Image Analyst posted. The images are not labelled in a sequential way. (Particle_3),.. (Particle_11),.. etc
myFolder = 'c:/Users/ezxjp1/Documents/MATLAB/ParticleImages';
% Upload particles, measure size & shape
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, 'Particle_%d.tif'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
Image Analyst
2017년 9월 14일
Here is what the FAQ correctly says:
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.jpg'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
and here is how you incorrectly modified it:
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, 'Particle_%d.tif'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
Take note that * is a wildcard character and can be used in functions like dir. The "%d" is not a wildcard symbol and is intended to be used in the functions sprintf() or fprintf().
Change the "%d" to "*" and it should work.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!