Read Images After Sorting with natsortfiles

6 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2020-8-22
编辑:Stephen23 2020-8-22
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).

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2020-8-22
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 个评论
iontrap
iontrap 2020-8-22
Thanks for the comments and the assistance, Stephen. I'm new to Matlab, so I'll work on cleaning up my work.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Just for fun 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by