Reading the image sequency
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello,
In a folder, I have series of images and name of the first image is "Field on_1ms_4_8V_60sec-02-Deskew_t001z001c1-2". The file name is changing "t001z001c1-2" from here. Number after t and number after z and sometimes number after c. How can I read the sequence of images using a loop ?
5 Kommentare
Walter Roberson
am 31 Okt. 2023
To confirm, you would process in the order t001z001c1-2 then t001z002c1-2 then t001z003c1-2 and so on, then t002z001c1-2 t002z002c1-2 and so on ?
Antworten (2)
Walter Roberson
am 31 Okt. 2023
%sample name "Field on_1ms_4_8V_60sec-02-Deskew_t001z001c1-2"
projectdir = 'name/of/directory/data/files/are/in';
basefilename = 'Field*';
qualifiedname = fullfile(projectdir, basefilename);
dinfo = dir(qualifiedname);
filenames = fullfile({dinfo.folder}, {dinfo.name});
numfiles = length(filenames);
if numfiles == 0
error('No files found for "%s"', qualifiedname);
end
%extract the last component of the file names
[~, basenames] = fileparts(filenames);
lastparts = regexp(basenames, '(?<=.*_)[^_]+$', 'match', 'once');
%now for each one, extract the groups of digits, still as characters
numpart_strings = regexp(lastpart, '\d+', 'match'); %not 'once' !
%convert them to numbers
numpart_numerics = cellfun(@str2double, numpart_strings, 'uniform', 0);
%do not assume that we have the same number of groups for each.
%for example there might perhaps be t001z001c2 without a -* component
%so find the longest group and pad the others to that size.
longest_group = max(cellfun(@length, numpart_numerics));
numpart_numerics = cellfun(@(V) [V, zeros(1, longest_group - numel(V))], 'uniform', 0);
%having done that, create a numeric array in which each row is the groups
numpart_numerics = vertcat(numpart_numerics{:});
%the rule seems to be that each further component right is a subgroup of
%the one further left
[~, order_of_files] = sortrows(numpart_numerics);
%and reorder the filenames to match the wanted numeric order
filenames = filenames(order_of_files);
imgs = cell(numfiles, 1);
failed_files = false(numfiles,1);
%now we can loop over the files
for K = 1 : numfiles
thisfile = filenames{K};
try
img = imread(thisfile);
imgs{K} = img;
catch ME
fprintf(2, 'could not read image file: "%s"\n', thisfile);
failed_files(K) = true;
end
end
%all of the files are now read into "imgs".
%any failures have empty cell and failed_files is true at that location
%now what?
Siehe auch
Kategorien
Mehr zu Convert Image Type finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!