;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; LUSH Lisp Universal Shell
;;;   Copyright (C) 2002 Leon Bottou, Yann Le Cun, AT&T Corp, NECI.
;;; Includes parts of TL3:
;;;   Copyright (C) 1987-1999 Leon Bottou and Neuristique.
;;; Includes selected parts of SN3.2:
;;;   Copyright (C) 1991-2001 AT&T Corp.
;;;
;;; Includes information derived from the GSL (Gnu Scientific Library)
;;;   header files which are copyrighted and distributed under the GPL
;;;   by their respective authors.
;;;
;;; This program is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 2 of the License, or
;;; (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program; if not, write to the Free Software
;;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(libload "opencv/cv-config")



#? *** opticalflow

;; dummy function that adds OpenCV C header files in C file
(eval `(de dummy_opticalflow () ,cv-cpheader-cmd ()))

;; #? * (dummy_opticalflow)
;; #? * (cvcalcopticalflowlk <srca> <srcb> <winsize> <velx> <vely>)
;; #? * (cvcalcopticalflowbm <srca> <srcb> <blocksize> <shiftsize> <maxrange> <useprevious> <velx> <vely>)
;; #? * (cvcalcopticalflowhs <srca> <srcb> <useprevious> <velx> <vely> <lambda> <criteria>)
;; #? * (cvcalcopticalflowpyrlk <imga> <imgb> <pyra> <pyrb> <featuresa> <featuresb> <count> <winsize> <level> <status> <error> <criteria> <flags>)
;; #? * (cvcalcaffineflowpyrlk <imga> <imgb> <pyra> <pyrb> <featuresa> <featuresb> <matrices> <count> <winsize> <level> <status> <error> <criteria> <flags>)

;; ********************************************************

;;  /****************************************************************************************\
;;  *                              Optical Flow functions                                    *
;;  \****************************************************************************************/

#? (cvCalcOpticalFlowLK <srca> <srcb> <winsize> <velx> <vely>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //    Name: cvCalcOpticalFlowLK( Lucas & Kanade method )
;;  //    Purpose: calculate Optical flow for 2 images using Lucas & Kanade algorithm
;;  //    Context:
;;  //    Parameters:
;;  //            srcA,         // first image
;;  //            srcB,         // second image
;;  //            winSize,      // size of the averaging window used for grouping
;;  //            velx,         //  horizontal
;;  //            vely          //  vertical components of optical flow
;;  //
;;  //    Returns:
;;  //
;;  //    Notes:  1.Optical flow to be computed for every pixel in ROI
;;  //            2.For calculating spatial derivatives we use 3x3 Sobel operator.
;;  //            3.We use the following border mode.
;;  //              The last row or column is replicated for the border
;;  //            ( IPL_BORDER_REPLICATE in IPL ).
;;  //
;;  //F*/
;;  OPENCVAPI  void  cvCalcOpticalFlowLK( const CvArr* srcA, const CvArr* srcB,
;;                                        CvSize winSize, CvArr* velx, CvArr* vely );

(de cvCalcOpticalFlowLK(srca srcb winsize velx vely)
    ((-gptr- "const CvArr *") srca srcb)
    ((-gptr- "CvSize *") winsize)
    ((-gptr- "CvArr *") velx vely)
    #{  cvCalcOpticalFlowLK($srca, $srcb, *$winsize, $velx, $vely);  #}
    ())

#? (cvCalcOpticalFlowBM <srca> <srcb> <blocksize> <shiftsize> <maxrange> <useprevious> <velx> <vely>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //    Name: cvCalcOpticalFlowBM
;;  //    Purpose: calculate Optical flow for 2 images using block matching algorithm
;;  //    Context:
;;  //    Parameters:
;;  //            srcA,         // first image
;;  //            srcB,         // second image
;;  //            blockSize,    // size of basic blocks which are compared
;;  //            shiftSize,    // coordinates increments.
;;  //            maxRange,     // size of the scanned neighborhood.
;;  //            usePrevious,  // use previous(input) velocity field.
;;  //            velx,         //  horizontal
;;  //            vely          //  vertical components of optical flow
;;  //
;;  //    Returns:
;;  //    Notes:
;;  //F*/
;;  OPENCVAPI  void  cvCalcOpticalFlowBM( const CvArr* srcA, const CvArr* srcB,
;;                                        CvSize blockSize, CvSize shiftSize,
;;                                        CvSize maxRange, int usePrevious,
;;                                        CvArr* velx, CvArr* vely );

(de cvCalcOpticalFlowBM(srca srcb blocksize shiftsize maxrange useprevious velx vely)
    ((-gptr- "const CvArr *") srca srcb)
    ((-gptr- "CvSize *") blocksize shiftsize maxrange)
    ((-int-) useprevious)
    ((-gptr- "CvArr *") velx vely)
    #{  cvCalcOpticalFlowBM( $srca, $srcb, *$blocksize, *$shiftsize, *$maxrange, $useprevious, $velx, $vely); #}
    ())

#? (cvCalcOpticalFlowHS <srca> <srcb> <useprevious> <velx> <vely> <lambda> <criteria>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //    Name: cvCalcOpticalFlowHS(Horn & Schunck method )
;;  //    Purpose: calculate Optical flow for 2 images using Horn & Schunck algorithm
;;  //    Context:
;;  //    Parameters:
;;  //            srcA,         // first image
;;  //            srcB,         // second image
;;  //            int usePrevious, // use previous(input) velocity field.
;;  //            velx,         //  horizontal
;;  //            vely          //  vertical components of optical flow
;;  //            double lambda, // Lagrangian multiplier
;;  //            criteria       // criteria of process termination
;;  //
;;  //    Returns:
;;  //
;;  //    Notes:  1.Optical flow to be computed for every pixel in ROI
;;  //            2.For calculating spatial derivatives we use 3x3 Sobel operator.
;;  //            3.We use the following border mode.
;;  //              The first and last rows and columns are replicated for the border
;;  //            ( IPL_BORDER_REPLICATE in IPL ).
;;  //F*/
;;  OPENCVAPI  void  cvCalcOpticalFlowHS( const CvArr* srcA, const CvArr* srcB,
;;                                        int usePrevious, CvArr* velx, CvArr* vely,
;;                                        double lambda, CvTermCriteria criteria );

(de cvCalcOpticalFlowHS(srca srcb useprevious velx vely lambda criteria)
    ((-gptr- "const CvArr *") srca srcb)
    ((-int-) useprevious)
    ((-gptr- "CvArr *") velx vely)
    ((-double-) lambda)
    ((-gptr- "CvTermCriteria *") criteria)
    #{  cvCalcOpticalFlowHS($srca, $srcb, $useprevious, $velx, $vely, $lambda, *$criteria); #}
    ())

#? (cvCalcOpticalFlowPyrLK <imga> <imgb> <pyra> <pyrb> <featuresa> <featuresb> <count> <winsize> <level> <status> <error> <criteria> <flags>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //    Name: cvCalcOpticalFlowPyrLK
;;  //    Purpose:
;;  //      It is Lucas & Kanade method, modified to use pyramids.
;;  //      Also it does several iterations to get optical flow for
;;  //      every point at every pyramid level.
;;  //      Calculates optical flow between two images for certain set of points.
;;  //    Context:
;;  //    Parameters:
;;  //            imgA     - first frame(time t)
;;  //            imgB     - second frame(time t+1)
;;  //            pyrA     - buffer for pyramid for the first frame.
;;  //                       if the pointer is not NULL, the buffer must have size enough to
;;  //                       store pyramid(from level 1 to level #<level>(see below))
;;  //                      (total size of(imgSize.width+8)*imgSize.height/3
;;  //                        bytes will be enough)).
;;  //            pyrB     - similar to pyrA, but for the second frame.
;;  //
;;  //                       for both parameters above the following rules work:
;;  //                           If image pointer is 0, the function allocates the buffer
;;  //                           internally, calculates pyramid and releases the buffer after
;;  //                           processing.
;;  //                           Else(image should be large enough then) the function calculates
;;  //                           pyramid and stores it in the buffer unless the
;;  //                           CV_LKFLOW_PYR_A[B]_READY flag is set. After function call
;;  //                           both pyramids are calculated and ready-flag for corresponding
;;  //                           image can be set.
;;  //
;;  //            count    - number of feature points
;;  //            winSize  - size of search window on each pyramid level
;;  //            level    - maximal pyramid level number
;;  //                        (if 0, pyramids are not used(single level),
;;  //                          if 1, two levels are used etc.)
;;  //
;;  //            next parameters are arrays of <count> elements.
;;  //            ------------------------------------------------------
;;  //            featuresA - array of points, for which the flow needs to be found
;;  //            featuresB - array of 2D points, containing calculated
;;  //                       new positions of input features(in the second image).
;;  //            status   - array, every element of which will be set to 1 if the flow for the
;;  //                       corresponding feature has been found, 0 else.
;;  //            error    - array of double numbers, containing difference between
;;  //                       patches around the original and moved points
;;  //                      (it is optional parameter, can be NULL).
;;  //            ------------------------------------------------------
;;  //            criteria   - specifies when to stop the iteration process of finding flow
;;  //                         for each point on each pyramid level
;;  //
;;  //            flags      - miscellaneous flags:
;;  //                            CV_LKFLOW_PYR_A_READY - pyramid for the first frame
;;  //                                                    is precalculated before call
;;  //                            CV_LKFLOW_PYR_B_READY - pyramid for the second frame
;;  //                                                    is precalculated before call
;;  //                            CV_LKFLOW_INITIAL_GUESSES - featuresB array holds initial
;;  //                                                        guesses about new features'
;;  //                                                        locations before function call.
;;  //    Returns:
;;  //    Notes:  For calculating spatial derivatives 3x3 Sharr operator is used.
;;  //            The values of pixels beyond the image are determined using border
;;  //            replication.
;;  //F*/
;;  #define  CV_LKFLOW_PYR_A_READY       1
;;  #define  CV_LKFLOW_PYR_B_READY       2
;;  #define  CV_LKFLOW_INITIAL_GUESSES   4
;;  OPENCVAPI  void  cvCalcOpticalFlowPyrLK( const CvArr*  imgA, const CvArr*  imgB,
;;                                           CvArr*  pyrA, CvArr*  pyrB,
;;                                           CvPoint2D32f* featuresA,
;;                                           CvPoint2D32f* featuresB,
;;                                           int       count,
;;                                           CvSize    winSize,
;;                                           int       level,
;;                                           char*     status,
;;                                           float*    error,
;;                                           CvTermCriteria criteria,
;;                                           int       flags );
(de cvCalcOpticalFlowPyrLK(imga imgb pyra pyrb featuresa featuresb count winsize level status error criteria flags)
    ((-gptr- "const CvArr *") imga imgb)
    ((-gptr- "CvArr *") pyra pyrb)
    ((-gptr- "CvPoint2D32f *") featuresa featuresb)
    ((-int-) count level flags)
    ((-gptr- "CvSize *") winsize)
    ((-gptr- "char *") status)
    ((-gptr- "float *") error)
    ((-gptr- "CvTermCriteria *") criteria)
    #{  cvCalcOpticalFlowPyrLK($imga,$imgb,$pyra,$pyrb,$featuresa,$featuresb,$count,*$winsize,$level,$status,$error,*$criteria,$flags); #}
    ())


;; #? (cvCalcAffineFlowPyrLK <imga> <imgb> <pyra> <pyrb> <featuresa> <featuresb> <matrices> <count> <winsize> <level> <status> <error> <criteria> <flags>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //    Name: cvCalcAffineFlowPyrLK
;;  //    Purpose:
;;  //      It is Lucas & Kanade affine tracking method, modified to use pyramids.
;;  //      Also it does several iterations to find flow for
;;  //      every point at every pyramid level.
;;  //      Calculates affine flow between two images for certain set of points.
;;  //    Context:
;;  //    Parameters:
;;  //            imgA     - first frame(time t)
;;  //            imgB     - second frame(time t+1)
;;  //            pyrA     - buffer for pyramid for the first frame.
;;  //                       if the pointer is not NULL, the buffer must have size enough to
;;  //                       store pyramid(from level 1 to level #<level>(see below))
;;  //                      (total size of(imgSize.width+8)*imgSize.height/3
;;  //                        bytes will be enough)).
;;  //            pyrB     - similar to pyrA, but for the second frame.
;;  //
;;  //                       for both parameters above the following rules work:
;;  //                           If image pointer is 0, the function allocates the buffer
;;  //                           internally, calculates pyramid and releases the buffer after
;;  //                           processing.
;;  //                           Else(image should be large enough then) the function calculates
;;  //                           pyramid and stores it in the buffer unless the
;;  //                           CV_LKFLOW_PYR_A[B]_READY flag is set. After function call
;;  //                           both pyramids are calculated and ready-flag for corresponding
;;  //                           image can be set.
;;  //
;;  //            count    - number of feature points
;;  //            winSize  - size of search window on each pyramid level
;;  //            level    - maximal pyramid level number
;;  //                        (if 0, pyramids are not used(single level),
;;  //                          if 1, two levels are used etc.)
;;  //
;;  //            next parameters are arrays of <count> elements.
;;  //            ------------------------------------------------------
;;  //            featuresA - array of points, for which the flow needs to be found
;;  //            featuresB - array of 2D points, containing calculated
;;  //                       new positions of input features(in the second image).
;;  //            matrices - affine transformation matrices,
;;  //            status   - array, every element of which will be set to 1 if the flow for the
;;  //                       corresponding feature has been found, 0 else.
;;  //            error    - array of double numbers, containing difference between
;;  //                       patches around the original and moved points
;;  //                      (it is optional parameter, can be NULL).
;;  //            ------------------------------------------------------
;;  //            criteria   - specifies when to stop the iteration process of finding flow
;;  //                         for each point on each pyramid level
;;  //
;;  //            flags      - miscellaneous flags:
;;  //                            CV_LKFLOW_PYR_A_READY - pyramid for the first frame
;;  //                                                    is precalculated before call
;;  //                            CV_LKFLOW_PYR_B_READY - pyramid for the second frame
;;  //                                                    is precalculated before call
;;  //                            CV_LKFLOW_INITIAL_GUESSES - featuresB array holds initial
;;  //                                                        guesses about new features'
;;  //                                                        locations before function call,
;;  //                                                        matrices array contains guesses
;;  //                                                        about local transformations in
;;  //                                                        the features' neighborhoods.
;;  //    Returns:
;;  //    Notes:  For calculating spatial derivatives 3x3 Sharr operator is used.
;;  //            The values of pixels beyond the image are determined using border
;;  //            replication.
;;  //F*/
;;  OPENCVAPI  void  cvCalcAffineFlowPyrLK( const CvArr*  imgA, const CvArr*  imgB,
;;                                          CvArr*  pyrA, CvArr*  pyrB,
;;                                          CvPoint2D32f* featuresA,
;;                                          CvPoint2D32f* featuresB,
;;                                          float*  matrices, int  count,
;;                                          CvSize  winSize, int  level,
;;                                          char*  status, float* error,
;;                                          CvTermCriteria criteria, int flags );
;; (de cvCalcAffineFlowPyrLK(imga imgb pyra pyrb featuresa featuresb matrices count winsize level status error criteria flags)
;;     ((-gptr- "const CvArr *") imga imgb)
;;     ((-gptr- "CvArr *") pyra pyrb)
;;     ((-gptr- "CvPoint2D32f *") featuresa featuresb)
;;     ((-gptr- "float *") matrices)
;;     ((-int-) count level flags)
;;     ((-gptr- "CvSize *") winsize)
;;     ((-gptr- "char *") status)
;;     ((-gptr- "float *") error)
;;     ((-gptr- "CvTermCriteria *") criteria)
;;     #{  cvCalcAffineFlowPyrLK($imga,$imgb,$pyra,$pyrb,$featuresa,$featuresb,$matrices,$count,*$winsize,$level,$status,$error,*$criteria,$flags); #}
;;     ())


;; ********************************************************

(dhc-make ()
  dummy_opticalflow
  cvcalcopticalflowlk
  cvcalcopticalflowbm
  cvcalcopticalflowhs
  cvcalcopticalflowpyrlk
  ;;cvcalcaffineflowpyrlk
)
