Page 1 of 1

? about the not operator in this code, Converting 2 Lisp

Posted: Fri Sep 27, 2013 7:30 pm
by joeish80829
here is the code:

Code: Select all

// 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 );
  }
I got it from this link:

http://dasl.mem.drexel.edu/~noahKuntz/o ... l#Step%201

It seems the way the code is designed that becuse the:

if( !first )

the program will never reach this part of the code:

Code: Select all

          cvAcc( Iscratch, IavgF );
          cvAbsDiff( Iscratch, IprevF, Iscratch2 );
          cvAcc( Iscratch2, IdiffF );
          Icount += 1.0;
In Lisp Im trying to use:

Code: Select all

                (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))
for the equivelant function and (not 1st) for ( !first ) ...i think thats correct

in c i do

Code: Select all

   static int first = 1;

     if( first ){
        cout << "reached this part of code " << endl << " " << first << endl << endl;

     } 
but it never cout's because of the code design it seems....so why would the designer of the tutorial code like this....he is copying from the book Learning OpenCV..... or am i doing something wrong?

Re: ? about the not operator in this code, Converting 2 Lis

Posted: Fri Sep 27, 2013 8:59 pm
by nuntius
In C, "static" variables are initialized when the program begins and retain any changes between function calls.
So the first time accumulateBackground is called, first=1. Every other time it is called, first=0.

There are a few ways of achieving this in CL. One cute method is to leverage lexical closure.

Code: Select all

(let ((first t))
  (defun foo-bar ()
    (when first
      (print "first"))
    (setf first nil)))
Generally, it is more appropriate to use defconstant or defparam (depending on whether the value should reset when the source file is reloaded).
This allows you to reset the value during development.

Code: Select all

(defparameter *first* t)
(defun foo-bar ()
  (when *first*
    (print "first"))
  (setf *first* nil))