how to read images one by one

조회 수: 2 (최근 30일)
rui
rui 2015년 2월 18일
편집: Stephen23 2021년 4월 18일
There is multiple images in a folder, but the name is not in order, for example, 01.jpg, 21.jpg, 41.jpg,......101.jpg. How to read these images successively? Thanks very much.

채택된 답변

Stephen23
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
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
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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by