Borrar filtros
Borrar filtros

Function and file name as an input argument

8 visualizaciones (últimos 30 días)
jad aoun
jad aoun el 10 de Jun. de 2020
Comentada: Faisal Sani Bala el 28 de Mayo de 2023
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

Respuestas (1)

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

Iniciar sesión para comentar.

Categorías

Más información sobre File Operations 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