1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                          License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
16 // Third party copyrights are property of their respective owners.
17 //
18 // Redistribution and use in source and binary forms, with or without modification,
19 // are permitted provided that the following conditions are met:
20 //
21 //   * Redistribution's of source code must retain the above copyright notice,
22 //     this list of conditions and the following disclaimer.
23 //
24 //   * Redistribution's in binary form must reproduce the above copyright notice,
25 //     this list of conditions and the following disclaimer in the documentation
26 //     and/or other materials provided with the distribution.
27 //
28 //   * The name of the copyright holders may not be used to endorse or promote products
29 //     derived from this software without specific prior written permission.
30 //
31 // This software is provided by the copyright holders and contributors "as is" and
32 // any express or implied warranties, including, but not limited to, the implied
33 // warranties of merchantability and fitness for a particular purpose are disclaimed.
34 // In no event shall the Intel Corporation or contributors be liable for any direct,
35 // indirect, incidental, special, exemplary, or consequential damages
36 // (including, but not limited to, procurement of substitute goods or services;
37 // loss of use, data, or profits; or business interruption) however caused
38 // and on any theory of liability, whether in contract, strict liability,
39 // or tort (including negligence or otherwise) arising in any way out of
40 // the use of this software, even if advised of the possibility of such damage.
41 //
42 //M*/
43 
44 #ifndef __OPENCV_TRACKING_C_H__
45 #define __OPENCV_TRACKING_C_H__
46 
47 #include "opencv2/imgproc/types_c.h"
48 
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
53 /** @addtogroup video_c
54   @{
55 */
56 
57 /****************************************************************************************\
58 *                                  Motion Analysis                                       *
59 \****************************************************************************************/
60 
61 /************************************ optical flow ***************************************/
62 
63 #define CV_LKFLOW_PYR_A_READY       1
64 #define CV_LKFLOW_PYR_B_READY       2
65 #define CV_LKFLOW_INITIAL_GUESSES   4
66 #define CV_LKFLOW_GET_MIN_EIGENVALS 8
67 
68 /* It is Lucas & Kanade method, modified to use pyramids.
69    Also it does several iterations to get optical flow for
70    every point at every pyramid level.
71    Calculates optical flow between two images for certain set of points (i.e.
72    it is a "sparse" optical flow, which is opposite to the previous 3 methods) */
73 CVAPI(void)  cvCalcOpticalFlowPyrLK( const CvArr*  prev, const CvArr*  curr,
74                                      CvArr*  prev_pyr, CvArr*  curr_pyr,
75                                      const CvPoint2D32f* prev_features,
76                                      CvPoint2D32f* curr_features,
77                                      int       count,
78                                      CvSize    win_size,
79                                      int       level,
80                                      char*     status,
81                                      float*    track_error,
82                                      CvTermCriteria criteria,
83                                      int       flags );
84 
85 
86 /* Modification of a previous sparse optical flow algorithm to calculate
87    affine flow */
88 CVAPI(void)  cvCalcAffineFlowPyrLK( const CvArr*  prev, const CvArr*  curr,
89                                     CvArr*  prev_pyr, CvArr*  curr_pyr,
90                                     const CvPoint2D32f* prev_features,
91                                     CvPoint2D32f* curr_features,
92                                     float* matrices, int  count,
93                                     CvSize win_size, int  level,
94                                     char* status, float* track_error,
95                                     CvTermCriteria criteria, int flags );
96 
97 /* Estimate rigid transformation between 2 images or 2 point sets */
98 CVAPI(int)  cvEstimateRigidTransform( const CvArr* A, const CvArr* B,
99                                       CvMat* M, int full_affine );
100 
101 /* Estimate optical flow for each pixel using the two-frame G. Farneback algorithm */
102 CVAPI(void) cvCalcOpticalFlowFarneback( const CvArr* prev, const CvArr* next,
103                                         CvArr* flow, double pyr_scale, int levels,
104                                         int winsize, int iterations, int poly_n,
105                                         double poly_sigma, int flags );
106 
107 /********************************* motion templates *************************************/
108 
109 /****************************************************************************************\
110 *        All the motion template functions work only with single channel images.         *
111 *        Silhouette image must have depth IPL_DEPTH_8U or IPL_DEPTH_8S                   *
112 *        Motion history image must have depth IPL_DEPTH_32F,                             *
113 *        Gradient mask - IPL_DEPTH_8U or IPL_DEPTH_8S,                                   *
114 *        Motion orientation image - IPL_DEPTH_32F                                        *
115 *        Segmentation mask - IPL_DEPTH_32F                                               *
116 *        All the angles are in degrees, all the times are in milliseconds                *
117 \****************************************************************************************/
118 
119 /* Updates motion history image given motion silhouette */
120 CVAPI(void)    cvUpdateMotionHistory( const CvArr* silhouette, CvArr* mhi,
121                                       double timestamp, double duration );
122 
123 /* Calculates gradient of the motion history image and fills
124    a mask indicating where the gradient is valid */
125 CVAPI(void)    cvCalcMotionGradient( const CvArr* mhi, CvArr* mask, CvArr* orientation,
126                                      double delta1, double delta2,
127                                      int aperture_size CV_DEFAULT(3));
128 
129 /* Calculates average motion direction within a selected motion region
130    (region can be selected by setting ROIs and/or by composing a valid gradient mask
131    with the region mask) */
132 CVAPI(double)  cvCalcGlobalOrientation( const CvArr* orientation, const CvArr* mask,
133                                         const CvArr* mhi, double timestamp,
134                                         double duration );
135 
136 /* Splits a motion history image into a few parts corresponding to separate independent motions
137    (e.g. left hand, right hand) */
138 CVAPI(CvSeq*)  cvSegmentMotion( const CvArr* mhi, CvArr* seg_mask,
139                                 CvMemStorage* storage,
140                                 double timestamp, double seg_thresh );
141 
142 /****************************************************************************************\
143 *                                       Tracking                                         *
144 \****************************************************************************************/
145 
146 /* Implements CAMSHIFT algorithm - determines object position, size and orientation
147    from the object histogram back project (extension of meanshift) */
148 CVAPI(int)  cvCamShift( const CvArr* prob_image, CvRect  window,
149                         CvTermCriteria criteria, CvConnectedComp* comp,
150                         CvBox2D* box CV_DEFAULT(NULL) );
151 
152 /* Implements MeanShift algorithm - determines object position
153    from the object histogram back project */
154 CVAPI(int)  cvMeanShift( const CvArr* prob_image, CvRect  window,
155                          CvTermCriteria criteria, CvConnectedComp* comp );
156 
157 /*
158 standard Kalman filter (in G. Welch' and G. Bishop's notation):
159 
160   x(k)=A*x(k-1)+B*u(k)+w(k)  p(w)~N(0,Q)
161   z(k)=H*x(k)+v(k),   p(v)~N(0,R)
162 */
163 typedef struct CvKalman
164 {
165     int MP;                     /* number of measurement vector dimensions */
166     int DP;                     /* number of state vector dimensions */
167     int CP;                     /* number of control vector dimensions */
168 
169     /* backward compatibility fields */
170 #if 1
171     float* PosterState;         /* =state_pre->data.fl */
172     float* PriorState;          /* =state_post->data.fl */
173     float* DynamMatr;           /* =transition_matrix->data.fl */
174     float* MeasurementMatr;     /* =measurement_matrix->data.fl */
175     float* MNCovariance;        /* =measurement_noise_cov->data.fl */
176     float* PNCovariance;        /* =process_noise_cov->data.fl */
177     float* KalmGainMatr;        /* =gain->data.fl */
178     float* PriorErrorCovariance;/* =error_cov_pre->data.fl */
179     float* PosterErrorCovariance;/* =error_cov_post->data.fl */
180     float* Temp1;               /* temp1->data.fl */
181     float* Temp2;               /* temp2->data.fl */
182 #endif
183 
184     CvMat* state_pre;           /* predicted state (x'(k)):
185                                     x(k)=A*x(k-1)+B*u(k) */
186     CvMat* state_post;          /* corrected state (x(k)):
187                                     x(k)=x'(k)+K(k)*(z(k)-H*x'(k)) */
188     CvMat* transition_matrix;   /* state transition matrix (A) */
189     CvMat* control_matrix;      /* control matrix (B)
190                                    (it is not used if there is no control)*/
191     CvMat* measurement_matrix;  /* measurement matrix (H) */
192     CvMat* process_noise_cov;   /* process noise covariance matrix (Q) */
193     CvMat* measurement_noise_cov; /* measurement noise covariance matrix (R) */
194     CvMat* error_cov_pre;       /* priori error estimate covariance matrix (P'(k)):
195                                     P'(k)=A*P(k-1)*At + Q)*/
196     CvMat* gain;                /* Kalman gain matrix (K(k)):
197                                     K(k)=P'(k)*Ht*inv(H*P'(k)*Ht+R)*/
198     CvMat* error_cov_post;      /* posteriori error estimate covariance matrix (P(k)):
199                                     P(k)=(I-K(k)*H)*P'(k) */
200     CvMat* temp1;               /* temporary matrices */
201     CvMat* temp2;
202     CvMat* temp3;
203     CvMat* temp4;
204     CvMat* temp5;
205 } CvKalman;
206 
207 /* Creates Kalman filter and sets A, B, Q, R and state to some initial values */
208 CVAPI(CvKalman*) cvCreateKalman( int dynam_params, int measure_params,
209                                  int control_params CV_DEFAULT(0));
210 
211 /* Releases Kalman filter state */
212 CVAPI(void)  cvReleaseKalman( CvKalman** kalman);
213 
214 /* Updates Kalman filter by time (predicts future state of the system) */
215 CVAPI(const CvMat*)  cvKalmanPredict( CvKalman* kalman,
216                                       const CvMat* control CV_DEFAULT(NULL));
217 
218 /* Updates Kalman filter by measurement
219    (corrects state of the system and internal matrices) */
220 CVAPI(const CvMat*)  cvKalmanCorrect( CvKalman* kalman, const CvMat* measurement );
221 
222 #define cvKalmanUpdateByTime  cvKalmanPredict
223 #define cvKalmanUpdateByMeasurement cvKalmanCorrect
224 
225 /** @} video_c */
226 
227 #ifdef __cplusplus
228 } // extern "C"
229 #endif
230 
231 
232 #endif // __OPENCV_TRACKING_C_H__
233