;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; 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")



#? *** moments

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

;; #? * (dummy_moments)
;; #? * (cvmoments <array> <moments> <binary>)
;; #? * (cvgetspatialmoment <moments> <xorder> <yorder>)
;; #? * (cvgetcentralmoment <moments> <xorder> <yorder>)
;; #? * (cvgetnormalizedcentralmoment <moments> <xorder> <yorder>)
;; #? * (cvgethumoments <moments> <humoments>)
;; #? * (cvnorm <imga> <imgb> <normtype> <mask>)

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

;;  /****************************************************************************************\
;;  *                                         Moments                                        *
;;  \****************************************************************************************/

#? (cvMoments  <array> <moments> <binary>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvMoments
;;  //    Purpose:
;;  //      Calculates moments(up to third order) of the image ROI.
;;  //      It fills moments state and after that, it is possible to
;;  //      return concrete moments using
;;  //        cvGetSpatialMoment, cvGetCentralMoment or
;;  //        cvGetNormalizedCentralMoment
;;  //    Context:
;;  //    Parameters:
;;  //        img - input image
;;  //        moments - output moments state.
;;  //        binary - if non zero, function treats non-zero pixels as 1s.
;;  //    Returns:
;;  //F*/
;;  OPENCVAPI void cvMoments( const CvArr* array, CvMoments* moments, int binary CV_DEFAULT( 0 ));

(de cvMoments (array moments binary)
    ((-gptr- "const CvArr *") array)
    ((-gptr- "CvMoments *") moments)
    ((-int-) binary)
    #{ cvMoments($array, $moments, $binary);  #}
    ())

#? (cvGetSpatialMoment  <moments> <xorder> <yorder>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvGetSpatialMoment, cvGetCentralMoment, cvGetCentralNormalizedMoment
;;  //    Purpose:
;;  //      Returns different moments(up to third order) from moments state.
;;  //        for raster image, these moments are defined as:
;;  //        mij = spatial_moment(i,j) = sum(y=0,H-1) sum(x=0,W-1) [I(x,y) *(x^i) *(y^j)]
;;  //       (where I(x,y) means pixel value at point(x,y). x^y means x power y).
;;  //
;;  //        muij = central_moment(i,j) = sum(y=0,H-1) sum(x=0,W-1)
;;  //                                     [I(x,y) *(x-mean_x)^i) *((y-mean_y)^j)]
;;  //       (where mean_x = m10/m00, mean_y = m01/m00.
;;  //         it's easy to see that mu00 = m00, mu10 = mu01 = 0)
;;  //
;;  //        nu_ij = central_normalized_moment(i,j) = muij/(m00^((i+j)/2+1))
;;  //    Context:
;;  //    Parameters:
;;  //        moments - moment state( filled by cvMoments or cvContourMoments )
;;  //        x_order - x order of the moment
;;  //        y_order - y order of the moment.
;;  //        The following condition has to be satifsied:
;;  //          0 <= x_order + y_order <= 3
;;  //    Returns:
;;  //        Required moment
;;  //F*/
;;  OPENCVAPI  double  cvGetSpatialMoment( CvMoments* moments, int x_order, int y_order );

(de cvGetSpatialMoment (moments xorder yorder)
    ((-gptr- "CvMoments *") moments)
    ((-int-) xorder)
    ((-int-) yorder)
    (let ((s 0))
      ((-double-) s)
      #{ $s = cvGetSpatialMoment($moments,$xorder, $yorder); #}
      s))

#? (cvGetCentralMoment  <moments> <xorder> <yorder>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvGetSpatialMoment, cvGetCentralMoment, cvGetCentralNormalizedMoment
;;  //    Purpose:
;;  //      Returns different moments(up to third order) from moments state.
;;  //        for raster image, these moments are defined as:
;;  //        mij = spatial_moment(i,j) = sum(y=0,H-1) sum(x=0,W-1) [I(x,y) *(x^i) *(y^j)]
;;  //       (where I(x,y) means pixel value at point(x,y). x^y means x power y).
;;  //
;;  //        muij = central_moment(i,j) = sum(y=0,H-1) sum(x=0,W-1)
;;  //                                     [I(x,y) *(x-mean_x)^i) *((y-mean_y)^j)]
;;  //       (where mean_x = m10/m00, mean_y = m01/m00.
;;  //         it's easy to see that mu00 = m00, mu10 = mu01 = 0)
;;  //
;;  //        nu_ij = central_normalized_moment(i,j) = muij/(m00^((i+j)/2+1))
;;  //    Context:
;;  //    Parameters:
;;  //        moments - moment state( filled by cvMoments or cvContourMoments )
;;  //        x_order - x order of the moment
;;  //        y_order - y order of the moment.
;;  //        The following condition has to be satifsied:
;;  //          0 <= x_order + y_order <= 3
;;  //    Returns:
;;  //        Required moment
;;  //F*/
;;  OPENCVAPI  double  cvGetCentralMoment( CvMoments* moments, int x_order, int y_order );

(de cvGetCentralMoment (moments xorder yorder)
    ((-gptr- "CvMoments *") moments)
    ((-int-) xorder)
    ((-int-) yorder)
    (let ((s 0))
      ((-double-) s)
      #{ $s = cvGetCentralMoment($moments,$xorder, $yorder); #}
      s))

#? (cvGetNormalizedCentralMoment  <moments> <xorder> <yorder>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvGetSpatialMoment, cvGetCentralMoment, cvGetCentralNormalizedMoment
;;  //    Purpose:
;;  //      Returns different moments(up to third order) from moments state.
;;  //        for raster image, these moments are defined as:
;;  //        mij = spatial_moment(i,j) = sum(y=0,H-1) sum(x=0,W-1) [I(x,y) *(x^i) *(y^j)]
;;  //       (where I(x,y) means pixel value at point(x,y). x^y means x power y).
;;  //
;;  //        muij = central_moment(i,j) = sum(y=0,H-1) sum(x=0,W-1)
;;  //                                     [I(x,y) *(x-mean_x)^i) *((y-mean_y)^j)]
;;  //       (where mean_x = m10/m00, mean_y = m01/m00.
;;  //         it's easy to see that mu00 = m00, mu10 = mu01 = 0)
;;  //
;;  //        nu_ij = central_normalized_moment(i,j) = muij/(m00^((i+j)/2+1))
;;  //    Context:
;;  //    Parameters:
;;  //        moments - moment state( filled by cvMoments or cvContourMoments )
;;  //        x_order - x order of the moment
;;  //        y_order - y order of the moment.
;;  //        The following condition has to be satifsied:
;;  //          0 <= x_order + y_order <= 3
;;  //    Returns:
;;  //        Required moment
;;  //F*/
;;  OPENCVAPI  double  cvGetNormalizedCentralMoment( CvMoments* moments,
;;                                                int x_order, int y_order );

(de cvGetNormalizedCentralMoment (moments xorder yorder)
    ((-gptr- "CvMoments *") moments)
    ((-int-) xorder)
    ((-int-) yorder)
    (let ((s 0))
      ((-double-) s)
      #{ $s = cvGetNormalizedCentralMoment($moments,$xorder, $yorder); #}
      s))


#? (cvGetHuMoments <moments> <humoments>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvGetHuMoments
;;  //    Purpose:
;;  //      Calculates seven Hu invariants from normalized moments
;;  //    Context:
;;  //    Parameters:
;;  //        moments - moments state.
;;  //        hu_moments - Hu moments
;;  //    Returns:
;;  //F*/
;;  typedef struct CvHuMoments
;;  {
;;      double hu1, hu2, hu3, hu4, hu5, hu6, hu7; /* Hu invariants */
;;  } CvHuMoments;
;;  OPENCVAPI void cvGetHuMoments( CvMoments*  moments, CvHuMoments*  hu_moments );

(de cvGetHuMoments(moments humoments)
    ((-gptr- "CvMoments *") moments)
    ((-gptr- "CvHuMoments *") humoments)
    #{  cvGetHuMoments($moments, $humoments);  #}
    ())


#? (cvNorm <imga> <imgb> <normtype> <mask>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvNorm, cvNormMask
;;  //    Purpose:
;;  //      Calculates different types of norm for single or a pair of images
;;  //    Context:
;;  //    Parameters:
;;  //        imgA - first input image
;;  //        imgB - second input image
;;  //        mask - determine pixels that are considered in norm calculation
;;  //        norm_type - type of the norm.
;;  //                                imgB == 0           imgB != 0
;;  //         ---------------------------------------------------------------------------
;;  //          CV_C:               ||imgA||_inf      ||imgA - imgB||_inf
;;  //          CV_L1:              ||imgA||_L1       ||imgA - imgB||_L1
;;  //          CV_L2:              ||imgA||_L2       ||imgA - imgB||_L2
;;  //         ---------------------------------------------------------------------------
;;  //          CV_RELATIVE_C:       forbidden       ||imgA - imgB||_inf/||imgB||_inf
;;  //          CV_RELATIVE_L1:      forbidden       ||imgA - imgB||_L1/||imgB||_L1
;;  //          CV_RELATIVE_L2:      forbidden       ||imgA - imgB||_L2/||imgB||_L2
;;  //    Returns:
;;  //      required norm
;;  //F*/
;;  OPENCVAPI  double  cvNorm( const CvArr* imgA, const CvArr* imgB, int normType,
;;                             const CvArr* mask CV_DEFAULT(0) );
(de cvNorm(imga imgb normtype mask)
    ((-gptr- "const CvArr *") imga)
    ((-gptr- "const CvArr *") imgb)
    ((-int-) normtype)
    ((-gptr- "const CvArr *") mask)
    (let (( s 0))
      ((-double-) s)
      #{ $s = cvNorm($imga, $imgb, $normtype, $mask);  #}
      s))

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

(dhc-make ()
  dummy_moments
  cvmoments
  cvgetspatialmoment
  cvgetcentralmoment
  cvgetnormalizedcentralmoment
  cvgethumoments
  cvnorm
)
