include/opencv2/video/tracking.hpp
Go to the documentation of this file.
00001 
00005 /*M///////////////////////////////////////////////////////////////////////////////////////
00006 //
00007 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
00008 //
00009 //  By downloading, copying, installing or using the software you agree to this license.
00010 //  If you do not agree to this license, do not download, install,
00011 //  copy or use the software.
00012 //
00013 //
00014 //                           License Agreement
00015 //                For Open Source Computer Vision Library
00016 //
00017 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
00018 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
00019 // Third party copyrights are property of their respective owners.
00020 //
00021 // Redistribution and use in source and binary forms, with or without modification,
00022 // are permitted provided that the following conditions are met:
00023 //
00024 //   * Redistribution's of source code must retain the above copyright notice,
00025 //     this list of conditions and the following disclaimer.
00026 //
00027 //   * Redistribution's in binary form must reproduce the above copyright notice,
00028 //     this list of conditions and the following disclaimer in the documentation
00029 //     and/or other materials provided with the distribution.
00030 //
00031 //   * The name of the copyright holders may not be used to endorse or promote products
00032 //     derived from this software without specific prior written permission.
00033 //
00034 // This software is provided by the copyright holders and contributors "as is" and
00035 // any express or implied warranties, including, but not limited to, the implied
00036 // warranties of merchantability and fitness for a particular purpose are disclaimed.
00037 // In no event shall the Intel Corporation or contributors be liable for any direct,
00038 // indirect, incidental, special, exemplary, or consequential damages
00039 // (including, but not limited to, procurement of substitute goods or services;
00040 // loss of use, data, or profits; or business interruption) however caused
00041 // and on any theory of liability, whether in contract, strict liability,
00042 // or tort (including negligence or otherwise) arising in any way out of
00043 // the use of this software, even if advised of the possibility of such damage.
00044 //
00045 //M*/
00046 
00047 #ifndef __OPENCV_TRACKING_HPP__
00048 #define __OPENCV_TRACKING_HPP__
00049 
00050 #include "opencv2/core/core.hpp"
00051 #include "opencv2/imgproc/imgproc.hpp"
00052 
00053 #ifdef __cplusplus
00054 extern "C" {
00055 #endif
00056 
00057 /****************************************************************************************\
00058 *                                  Motion Analysis                                       *
00059 \****************************************************************************************/
00060 
00061 /************************************ optical flow ***************************************/
00062 
00063 #define  CV_LKFLOW_PYR_A_READY       1
00064 #define  CV_LKFLOW_PYR_B_READY       2
00065 #define  CV_LKFLOW_INITIAL_GUESSES   4
00066 #define  CV_LKFLOW_GET_MIN_EIGENVALS 8
00067 
00068 /* It is Lucas & Kanade method, modified to use pyramids.
00069    Also it does several iterations to get optical flow for
00070    every point at every pyramid level.
00071    Calculates optical flow between two images for certain set of points (i.e.
00072    it is a "sparse" optical flow, which is opposite to the previous 3 methods) */
00073 CVAPI(void)  cvCalcOpticalFlowPyrLK( const CvArr*  prev, const CvArr*  curr,
00074                                      CvArr*  prev_pyr, CvArr*  curr_pyr,
00075                                      const CvPoint2D32f* prev_features,
00076                                      CvPoint2D32f* curr_features,
00077                                      int       count,
00078                                      CvSize    win_size,
00079                                      int       level,
00080                                      char*     status,
00081                                      float*    track_error,
00082                                      CvTermCriteria criteria,
00083                                      int       flags );
00084 
00085 
00086 /* Modification of a previous sparse optical flow algorithm to calculate
00087    affine flow */
00088 CVAPI(void)  cvCalcAffineFlowPyrLK( const CvArr*  prev, const CvArr*  curr,
00089                                     CvArr*  prev_pyr, CvArr*  curr_pyr,
00090                                     const CvPoint2D32f* prev_features,
00091                                     CvPoint2D32f* curr_features,
00092                                     float* matrices, int  count,
00093                                     CvSize win_size, int  level,
00094                                     char* status, float* track_error,
00095                                     CvTermCriteria criteria, int flags );
00096 
00097 /* Estimate rigid transformation between 2 images or 2 point sets */
00098 CVAPI(int)  cvEstimateRigidTransform( const CvArr* A, const CvArr* B,
00099                                       CvMat* M, int full_affine );
00100 
00101 /* Estimate optical flow for each pixel using the two-frame G. Farneback algorithm */
00102 CVAPI(void) cvCalcOpticalFlowFarneback( const CvArr* prev, const CvArr* next,
00103                                         CvArr* flow, double pyr_scale, int levels,
00104                                         int winsize, int iterations, int poly_n,
00105                                         double poly_sigma, int flags );
00106 
00107 /********************************* motion templates *************************************/
00108 
00109 /****************************************************************************************\
00110 *        All the motion template functions work only with single channel images.         *
00111 *        Silhouette image must have depth IPL_DEPTH_8U or IPL_DEPTH_8S                   *
00112 *        Motion history image must have depth IPL_DEPTH_32F,                             *
00113 *        Gradient mask - IPL_DEPTH_8U or IPL_DEPTH_8S,                                   *
00114 *        Motion orientation image - IPL_DEPTH_32F                                        *
00115 *        Segmentation mask - IPL_DEPTH_32F                                               *
00116 *        All the angles are in degrees, all the times are in milliseconds                *
00117 \****************************************************************************************/
00118 
00119 /* Updates motion history image given motion silhouette */
00120 CVAPI(void)    cvUpdateMotionHistory( const CvArr* silhouette, CvArr* mhi,
00121                                       double timestamp, double duration );
00122 
00123 /* Calculates gradient of the motion history image and fills
00124    a mask indicating where the gradient is valid */
00125 CVAPI(void)    cvCalcMotionGradient( const CvArr* mhi, CvArr* mask, CvArr* orientation,
00126                                      double delta1, double delta2,
00127                                      int aperture_size CV_DEFAULT(3));
00128 
00129 /* Calculates average motion direction within a selected motion region
00130    (region can be selected by setting ROIs and/or by composing a valid gradient mask
00131    with the region mask) */
00132 CVAPI(double)  cvCalcGlobalOrientation( const CvArr* orientation, const CvArr* mask,
00133                                         const CvArr* mhi, double timestamp,
00134                                         double duration );
00135 
00136 /* Splits a motion history image into a few parts corresponding to separate independent motions
00137    (e.g. left hand, right hand) */
00138 CVAPI(CvSeq*)  cvSegmentMotion( const CvArr* mhi, CvArr* seg_mask,
00139                                 CvMemStorage* storage,
00140                                 double timestamp, double seg_thresh );
00141 
00142 /****************************************************************************************\
00143 *                                       Tracking                                         *
00144 \****************************************************************************************/
00145 
00146 /* Implements CAMSHIFT algorithm - determines object position, size and orientation
00147    from the object histogram back project (extension of meanshift) */
00148 CVAPI(int)  cvCamShift( const CvArr* prob_image, CvRect  window,
00149                         CvTermCriteria criteria, CvConnectedComp* comp,
00150                         CvBox2D* box CV_DEFAULT(NULL) );
00151 
00152 /* Implements MeanShift algorithm - determines object position
00153    from the object histogram back project */
00154 CVAPI(int)  cvMeanShift( const CvArr* prob_image, CvRect  window,
00155                          CvTermCriteria criteria, CvConnectedComp* comp );
00156 
00157 /*
00158 standard Kalman filter (in G. Welch' and G. Bishop's notation):
00159 
00160   x(k)=A*x(k-1)+B*u(k)+w(k)  p(w)~N(0,Q)
00161   z(k)=H*x(k)+v(k),   p(v)~N(0,R)
00162 */
00163 typedef struct CvKalman
00164 {
00165     int MP;                     /* number of measurement vector dimensions */
00166     int DP;                     /* number of state vector dimensions */
00167     int CP;                     /* number of control vector dimensions */
00168 
00169     /* backward compatibility fields */
00170 #if 1
00171     float* PosterState;         /* =state_pre->data.fl */
00172     float* PriorState;          /* =state_post->data.fl */
00173     float* DynamMatr;           /* =transition_matrix->data.fl */
00174     float* MeasurementMatr;     /* =measurement_matrix->data.fl */
00175     float* MNCovariance;        /* =measurement_noise_cov->data.fl */
00176     float* PNCovariance;        /* =process_noise_cov->data.fl */
00177     float* KalmGainMatr;        /* =gain->data.fl */
00178     float* PriorErrorCovariance;/* =error_cov_pre->data.fl */
00179     float* PosterErrorCovariance;/* =error_cov_post->data.fl */
00180     float* Temp1;               /* temp1->data.fl */
00181     float* Temp2;               /* temp2->data.fl */
00182 #endif
00183 
00184     CvMat* state_pre;           /* predicted state (x'(k)):
00185                                     x(k)=A*x(k-1)+B*u(k) */
00186     CvMat* state_post;          /* corrected state (x(k)):
00187                                     x(k)=x'(k)+K(k)*(z(k)-H*x'(k)) */
00188     CvMat* transition_matrix;   /* state transition matrix (A) */
00189     CvMat* control_matrix;      /* control matrix (B)
00190                                    (it is not used if there is no control)*/
00191     CvMat* measurement_matrix;  /* measurement matrix (H) */
00192     CvMat* process_noise_cov;   /* process noise covariance matrix (Q) */
00193     CvMat* measurement_noise_cov; /* measurement noise covariance matrix (R) */
00194     CvMat* error_cov_pre;       /* priori error estimate covariance matrix (P'(k)):
00195                                     P'(k)=A*P(k-1)*At + Q)*/
00196     CvMat* gain;                /* Kalman gain matrix (K(k)):
00197                                     K(k)=P'(k)*Ht*inv(H*P'(k)*Ht+R)*/
00198     CvMat* error_cov_post;      /* posteriori error estimate covariance matrix (P(k)):
00199                                     P(k)=(I-K(k)*H)*P'(k) */
00200     CvMat* temp1;               /* temporary matrices */
00201     CvMat* temp2;
00202     CvMat* temp3;
00203     CvMat* temp4;
00204     CvMat* temp5;
00205 } CvKalman;
00206 
00207 /* Creates Kalman filter and sets A, B, Q, R and state to some initial values */
00208 CVAPI(CvKalman*) cvCreateKalman( int dynam_params, int measure_params,
00209                                  int control_params CV_DEFAULT(0));
00210 
00211 /* Releases Kalman filter state */
00212 CVAPI(void)  cvReleaseKalman( CvKalman** kalman);
00213 
00214 /* Updates Kalman filter by time (predicts future state of the system) */
00215 CVAPI(const CvMat*)  cvKalmanPredict( CvKalman* kalman,
00216                                       const CvMat* control CV_DEFAULT(NULL));
00217 
00218 /* Updates Kalman filter by measurement
00219    (corrects state of the system and internal matrices) */
00220 CVAPI(const CvMat*)  cvKalmanCorrect( CvKalman* kalman, const CvMat* measurement );
00221 
00222 #define cvKalmanUpdateByTime  cvKalmanPredict
00223 #define cvKalmanUpdateByMeasurement cvKalmanCorrect
00224 
00225 #ifdef __cplusplus
00226 }
00227 
00228 namespace cv
00229 {
00230 
00232 CV_EXPORTS_W void updateMotionHistory( InputArray silhouette, InputOutputArray mhi,
00233                                        double timestamp, double duration );
00234 
00236 CV_EXPORTS_W void calcMotionGradient( InputArray mhi, OutputArray mask,
00237                                       OutputArray orientation,
00238                                       double delta1, double delta2,
00239                                       int apertureSize=3 );
00240 
00242 CV_EXPORTS_W double calcGlobalOrientation( InputArray orientation, InputArray mask,
00243                                            InputArray mhi, double timestamp,
00244                                            double duration );
00245 
00246 CV_EXPORTS_W void segmentMotion(InputArray mhi, OutputArray segmask,
00247                                 CV_OUT vector<Rect>& boundingRects,
00248                                 double timestamp, double segThresh);
00249 
00251 CV_EXPORTS_W RotatedRect CamShift( InputArray probImage, CV_OUT CV_IN_OUT Rect& window,
00252                                    TermCriteria criteria );
00253 
00255 CV_EXPORTS_W int meanShift( InputArray probImage, CV_OUT CV_IN_OUT Rect& window,
00256                             TermCriteria criteria );
00257 
00265 class CV_EXPORTS_W KalmanFilter
00266 {
00267 public:
00269     CV_WRAP KalmanFilter();
00271     CV_WRAP KalmanFilter(int dynamParams, int measureParams, int controlParams=0, int type=CV_32F);
00273     void init(int dynamParams, int measureParams, int controlParams=0, int type=CV_32F);
00274 
00276     CV_WRAP const Mat& predict(const Mat& control=Mat());
00278     CV_WRAP const Mat& correct(const Mat& measurement);
00279 
00280     Mat statePre;           
00281     Mat statePost;          
00282     Mat transitionMatrix;   
00283     Mat controlMatrix;      
00284     Mat measurementMatrix;  
00285     Mat processNoiseCov;    
00286     Mat measurementNoiseCov;
00287     Mat errorCovPre;        
00288     Mat gain;               
00289     Mat errorCovPost;       
00290 
00291     // temporary matrices
00292     Mat temp1;
00293     Mat temp2;
00294     Mat temp3;
00295     Mat temp4;
00296     Mat temp5;
00297 };
00298 
00299 enum
00300 {
00301     OPTFLOW_USE_INITIAL_FLOW = CV_LKFLOW_INITIAL_GUESSES,
00302     OPTFLOW_LK_GET_MIN_EIGENVALS = CV_LKFLOW_GET_MIN_EIGENVALS,
00303     OPTFLOW_FARNEBACK_GAUSSIAN = 256
00304 };
00305 
00307 CV_EXPORTS_W int buildOpticalFlowPyramid(InputArray img, OutputArrayOfArrays pyramid,
00308                                          Size winSize, int maxLevel, bool withDerivatives = true,
00309                                          int pyrBorder = BORDER_REFLECT_101, int derivBorder = BORDER_CONSTANT,
00310                                          bool tryReuseInputImage = true);
00311 
00313 CV_EXPORTS_W void calcOpticalFlowPyrLK( InputArray prevImg, InputArray nextImg,
00314                            InputArray prevPts, CV_OUT InputOutputArray nextPts,
00315                            OutputArray status, OutputArray err,
00316                            Size winSize=Size(21,21), int maxLevel=3,
00317                            TermCriteria criteria=TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 0.01),
00318                            int flags=0, double minEigThreshold=1e-4);
00319 
00321 CV_EXPORTS_W void calcOpticalFlowFarneback( InputArray prev, InputArray next,
00322                            CV_OUT InputOutputArray flow, double pyr_scale, int levels, int winsize,
00323                            int iterations, int poly_n, double poly_sigma, int flags );
00324 
00326 // that maps one 2D point set to another or one image to another.
00327 CV_EXPORTS_W Mat estimateRigidTransform( InputArray src, InputArray dst,
00328                                          bool fullAffine);
00329 
00331 CV_EXPORTS_W void calcOpticalFlowSF(Mat& from,
00332                                     Mat& to,
00333                                     Mat& flow,
00334                                     int layers,
00335                                     int averaging_block_size,
00336                                     int max_flow);
00337 
00338 CV_EXPORTS_W void calcOpticalFlowSF(Mat& from,
00339                                     Mat& to,
00340                                     Mat& flow,
00341                                     int layers,
00342                                     int averaging_block_size,
00343                                     int max_flow,
00344                                     double sigma_dist,
00345                                     double sigma_color,
00346                                     int postprocess_window,
00347                                     double sigma_dist_fix,
00348                                     double sigma_color_fix,
00349                                     double occ_thr,
00350                                     int upscale_averaging_radius,
00351                                     double upscale_sigma_dist,
00352                                     double upscale_sigma_color,
00353                                     double speed_up_thr);
00354 
00355 }
00356 
00357 #endif
00358 
00359 #endif