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!