필터 지우기
필터 지우기

Function and file name as an input argument

조회 수: 8 (최근 30일)
jad aoun
jad aoun 2020년 6월 10일
댓글: Faisal Sani Bala 2023년 5월 28일
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

답변 (1개)

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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by