考虑一下OpenCV 教程 8 - 第 9 章中的这个 C 代码
// Learn the background statistics for one more frame
void accumulateBackground( IplImage *I ){
static int first = 1;
cvCvtScale( I, Iscratch, 1, 0 );
if( !first ){
cvAcc( Iscratch, IavgF );
cvAbsDiff( Iscratch, IprevF, Iscratch2 );
cvAcc( Iscratch2, IdiffF );
Icount += 1.0;
}
first = 0;
cvCopy( Iscratch, IprevF );
}
似乎代码的设计方式是因为
if( !first )
程序永远不会执行:
cvAcc( Iscratch, IavgF );
cvAbsDiff( Iscratch, IprevF, Iscratch2 );
cvAcc( Iscratch2, IdiffF );
Icount += 1.0;
在 Lisp 中,我试图将其翻译为:
(defun accumulate-background (i)
(setf 1st 1)
(cvt-scale i i-scratch-1 1 0) ;; To float
(if (not 1st)
(progn (acc i-scratch-1 i-avg-f)
(abs-diff i-scratch-1 i-prev-f i-scratch-2)
(acc i-scratch-2 i-diff-f)
(setf i-count (+ i-count 1.0))))
(setf 1st 0)
(copy i-scratch-1 i-prev-f))
对于等效功能,使用(not 1st)
for !first
,我认为这是正确的。在 C++ 中,我这样做:
static int first = 1;
if( first ){
cout << "reached this part of code " << endl << " " << first << endl << endl;
}
但由于代码设计,似乎永远不会产生任何输出。为什么教程的设计者会这样编码?他正在从Learning OpenCV复制。