Filter löschen
Filter löschen

Function and file name as an input argument

8 Ansichten (letzte 30 Tage)
jad aoun
jad aoun am 10 Jun. 2020
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

Antworten (1)

Walter Roberson
Walter Roberson am 10 Jun. 2020
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 Kommentare
jad aoun
jad aoun am 15 Jun. 2020
I figured it out myself . Thank you a lot again.
Faisal Sani Bala
Faisal Sani Bala am 28 Mai 2023
Hi, please can you share the working function which you got finally?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu File Operations 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!

Translated by