This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

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

2 posts · 2059 views

here is the code:
// 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:
          cvAcc( Iscratch, IavgF );
          cvAbsDiff( Iscratch, IprevF, Iscratch2 );
          cvAcc( Iscratch2, IdiffF );
          Icount += 1.0;
In Lisp Im trying to use:
                (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
   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?

Last edited by nuntius on , edited 1 time in total. Reason: added [code] tags

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

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.
(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.
(defparameter *first* t)
(defun foo-bar ()
  (when *first*
    (print "first"))
  (setf *first* nil))