Read Images After Sorting with natsortfiles

7 visualizaciones (últimos 30 días)
iontrap
iontrap el 22 de Ag. de 2020
Comentada: iontrap el 22 de Ag. de 2020
I have a sequence of images titled "0_b.tif", "0_g.tif", "0_r.tif", "1_b.tif", ... "640_b.tif", "640_g.tif", "640_r.tif". I first sorted these so that they appear in numbered order using natsortfiles. Now I am trying to make simple mean, max, min, std calculations for each b, g, and r sequence of 640 images, separately. Below is an example of just the mean calculation. I am able to generate the correct mean values, however they are still listed in the original, incorrect order before sorting.
The end product is supposed to be a 640 x 1 column vector of mean values in file numerical order.
function [X,ndx,dbg] = natsortfiles(X,rgx,varargin);
MeanNew = zeros(640,1);
for k = 1:640
D = 'C:\mypath\';
S = dir(fullfile(D,'*_b*.tif')); % selecting only the files ending with _b
N = natsortfiles({S.name});
I suspect that from here, I am accidentally bypassing the natsort function, reverting back to the original order.
F = fullfile(D,N{k});
FileNames=S(k).name;
I = imread(FileNames);
MeanNew(k,:) = mean(I , [1 2] )
end
end
  1 comentario
Stephen23
Stephen23 el 22 de Ag. de 2020
Editada: Stephen23 el 22 de Ag. de 2020
Your code has a few very unusual features:
1- If you want to use natsortfiles (the FEX submission), why are you redefining the function? Once you do that, you will not be able to use natsortfiles (the FEX submission) to sort your filenames. In fact, what you show in your question is a recursive function, which is very unlikely to do anything useful for you.
2- Why are you calilng dir, natsortfiles, etc inside the loop? The way you repeatedly call them inside the loop is not very efficient. The usual, efficient, simple, recommended approach shown in the documentation is to call dir, natsortfiles, etc. once before the loop. Which is also what the MATLAB documentation recommends:
3- Your comment "selecting only the files ending with b" does not actually reflect what that name string does: it actually matches names with "_b" anywhere in the filename.
4- Why are you hard-coding the number of files? One of the benefits of using dir is that you get a list of any/all actual existing files, which makes it trivial to write your code in such a way that it automatically adjusts to the number of actual files.
5- The reason that the file are imported in an unsorted order is simply that you do not use the sorted filenames: the sorted filenames are allocated to a variable named N which are joined with the filepath to create variable F. And then you don't use F for anything at all. You totally ignore it. Instead of using the elements of F (i.e. the sorted absolute filenames) you use the unsorted filenames from the original structure returned by dir. Which clearly are not sorted.
"I suspect that from here, I am accidentally bypassing the natsort function, reverting back to the original order.'"
Note that natsortfiles documentation has plenty examples, including in the M-file itself, and on the FEX page, and as HTML documentation accesible via the MATLAB help. These include complete examples showing how to use it with dir, etc., to make it easy for anyone who wants to use natsortfiles. I recommend that you follow those examples (they work, I tried them).

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 22 de Ag. de 2020
Do NOT redefine the natsortfiles function! Do NOT save this in a file named natsortfiles!
Simply following the examples in the natsortfiles documentation and the MATLAB documentation:
D = 'C:\mypath\';
S = dir(fullfile(D,'*_b.tif'));
C = natsortfiles({S.name});
N = numel(C);
M = zeros(N,1);
for k = 1:N
F = fullfile(D,C{k});
I = imread(F);
M(k) = mean(I,[1,2]);
end
  1 comentario
iontrap
iontrap el 22 de Ag. de 2020
Thanks for the comments and the assistance, Stephen. I'm new to Matlab, so I'll work on cleaning up my work.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Just for fun 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!

Translated by