Function and file name as an input argument
8 次查看(过去 30 天)
显示 更早的评论
Hi everyone ,
Can anyone help me ? I am writing a function which is supposed to have a filename as an input argument. This fucntion is supposed to read all the raw files ( 2D gray images) from the file and stack the images . The output argument is the mean of the stacked image.
When i do it in a normal.m script it works but it does not when i try to do it in a function with the filename as an input argument.
Thx in advanve
function moy =Moyenne_Dark(filelocation)
Nrow = 2048;
NCol = 2040;
dir (filelocation);
matfiles = dir(fullfile(filelocation, '*.raw'));
nfiles = length(matfiles)
sum=uint16((zeros(2040,2048)));
for i = 1 : nfiles
filename = matfiles(i).name ;
image=fopen(filelocation, 'rb');
M = uint16(fread(image, [NLigne, NColonne], 'uint16'));
N = swapbytes(M)';
sum=imadd(sum,N);
fclose(image);
end
sum=imdivide(sum,nfiles);
moy= mean2(sum);
end
0 个评论
回答(1 个)
Walter Roberson
2020-6-10
dir (filelocation);
That line is not contributing anything; you fetch the directory information and then throw it away.
matfiles = dir(fullfile(filelocation, '*.raw'));
misleading variable name for raw files ;-)
filename = matfiles(i).name ;
No, that implies the files are in the current directory, but they are in the directory indicated by filelocation. Instead use
filename = fullfile(filelocation, matfiles(i).name);
Also,
sum=uint16((zeros(2040,2048)));
We recommend against naming any variable sum : chances are too high that you will want to also use sum() as a function in the same code. And it confuses readers.
sum=imadd(sum,N);
There is a high risk that you are going to saturate uint16 when you add a bunch of uint16 together.
N = swapbytes(M)';
%...
moy= mean2(sum);
mean2() does not care about the order the values are in, so there is no point in doing the transpose. Just accumulate into a [NLigne, NColonne] array without transposing.
4 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!