Reading the image sequency

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 comentarios

Walter Roberson
Walter Roberson el 31 de Oct. de 2023
Do you just need to process all of them in some order, or do you need a specific order? For example, would you proceed to process t002z001c1-2 then t003z001c1-2 ... then back to t001z002c1-2 then t002z002c1-2 and so on?
Bipul Biswas
Bipul Biswas el 31 de Oct. de 2023
Sorry, I was not enough clear about it.
First I want to calculate for a fixed z value with variable t and then the next value of z and it's corresponding ts, and so on. Also I want to save the images accordingly.
Image Analyst
Image Analyst el 31 de Oct. de 2023
Were you not able to use the FAQ I pointed you to below in my Answer? If you have a list of z's and t's you can use sprintf() like it shows in the FAQ to build the filenames in a loop.
Walter Roberson
Walter Roberson el 31 de Oct. de 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 ?
Bipul Biswas
Bipul Biswas el 31 de Oct. de 2023
Thanks.. It works.

Iniciar sesión para comentar.

Respuestas (2)

Walter Roberson
Walter Roberson el 31 de Oct. de 2023

0 votos

%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?

Preguntada:

el 31 de Oct. de 2023

Comentada:

el 31 de Oct. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by