opencv 2.2.0

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tracking.hpp
Go to the documentation of this file.
1
5 /*M///////////////////////////////////////////////////////////////////////////////////////
6 //
7 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
8 //
9 // By downloading, copying, installing or using the software you agree to this license.
10 // If you do not agree to this license, do not download, install,
11 // copy or use the software.
12 //
13 //
14 // License Agreement
15 // For Open Source Computer Vision Library
16 //
17 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
18 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
19 // Third party copyrights are property of their respective owners.
20 //
21 // Redistribution and use in source and binary forms, with or without modification,
22 // are permitted provided that the following conditions are met:
23 //
24 // * Redistribution's of source code must retain the above copyright notice,
25 // this list of conditions and the following disclaimer.
26 //
27 // * Redistribution's in binary form must reproduce the above copyright notice,
28 // this list of conditions and the following disclaimer in the documentation
29 // and/or other materials provided with the distribution.
30 //
31 // * The name of the copyright holders may not be used to endorse or promote products
32 // derived from this software without specific prior written permission.
33 //
34 // This software is provided by the copyright holders and contributors "as is" and
35 // any express or implied warranties, including, but not limited to, the implied
36 // warranties of merchantability and fitness for a particular purpose are disclaimed.
37 // In no event shall the Intel Corporation or contributors be liable for any direct,
38 // indirect, incidental, special, exemplary, or consequential damages
39 // (including, but not limited to, procurement of substitute goods or services;
40 // loss of use, data, or profits; or business interruption) however caused
41 // and on any theory of liability, whether in contract, strict liability,
42 // or tort (including negligence or otherwise) arising in any way out of
43 // the use of this software, even if advised of the possibility of such damage.
44 //
45 //M*/
46
47 #ifndef __OPENCV_TRACKING_HPP__
48 #define __OPENCV_TRACKING_HPP__
49
50 #include "opencv2/core/core.hpp"
51 #include "opencv2/imgproc/imgproc_c.h"
52
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
56
57 /****************************************************************************************\
58 * Motion Analysis *
59 \****************************************************************************************/
60
61 /************************************ optical flow ***************************************/
62
63 /* Calculates optical flow for 2 images using classical Lucas & Kanade algorithm */
64 CVAPI(void) cvCalcOpticalFlowLK( const CvArr* prev, const CvArr* curr,
65 CvSize win_size, CvArr* velx, CvArr* vely );
66
67 /* Calculates optical flow for 2 images using block matching algorithm */
68 CVAPI(void) cvCalcOpticalFlowBM( const CvArr* prev, const CvArr* curr,
69 CvSize block_size, CvSize shift_size,
70 CvSize max_range, int use_previous,
71 CvArr* velx, CvArr* vely );
72
73 /* Calculates Optical flow for 2 images using Horn & Schunck algorithm */
74 CVAPI(void) cvCalcOpticalFlowHS( const CvArr* prev, const CvArr* curr,
75 int use_previous, CvArr* velx, CvArr* vely,
76 double lambda, CvTermCriteria criteria );
77
78 #define CV_LKFLOW_PYR_A_READY 1
79 #define CV_LKFLOW_PYR_B_READY 2
80 #define CV_LKFLOW_INITIAL_GUESSES 4
81 #define CV_LKFLOW_GET_MIN_EIGENVALS 8
82
83 /* It is Lucas & Kanade method, modified to use pyramids.
84 Also it does several iterations to get optical flow for
85 every point at every pyramid level.
86 Calculates optical flow between two images for certain set of points (i.e.
87 it is a "sparse" optical flow, which is opposite to the previous 3 methods) */
88 CVAPI(void) cvCalcOpticalFlowPyrLK( const CvArr* prev, const CvArr* curr,
89 CvArr* prev_pyr, CvArr* curr_pyr,
90 const CvPoint2D32f* prev_features,
91 CvPoint2D32f* curr_features,
92 int count,
93 CvSize win_size,
94 int level,
95 char* status,
96 float* track_error,
97 CvTermCriteria criteria,
98 int flags );
99
100
101 /* Modification of a previous sparse optical flow algorithm to calculate
102 affine flow */
103 CVAPI(void) cvCalcAffineFlowPyrLK( const CvArr* prev, const CvArr* curr,
104 CvArr* prev_pyr, CvArr* curr_pyr,
105 const CvPoint2D32f* prev_features,
106 CvPoint2D32f* curr_features,
107 float* matrices, int count,
108 CvSize win_size, int level,
109 char* status, float* track_error,
110 CvTermCriteria criteria, int flags );
111
112 /* Estimate rigid transformation between 2 images or 2 point sets */
113 CVAPI(int) cvEstimateRigidTransform( const CvArr* A, const CvArr* B,
114 CvMat* M, int full_affine );
115
116 /* Estimate optical flow for each pixel using the two-frame G. Farneback algorithm */
117 CVAPI(void) cvCalcOpticalFlowFarneback( const CvArr* prev, const CvArr* next,
118 CvArr* flow, double pyr_scale, int levels,
119 int winsize, int iterations, int poly_n,
120 double poly_sigma, int flags );
121
122 /********************************* motion templates *************************************/
123
124 /****************************************************************************************\
125 * All the motion template functions work only with single channel images. *
126 * Silhouette image must have depth IPL_DEPTH_8U or IPL_DEPTH_8S *
127 * Motion history image must have depth IPL_DEPTH_32F, *
128 * Gradient mask - IPL_DEPTH_8U or IPL_DEPTH_8S, *
129 * Motion orientation image - IPL_DEPTH_32F *
130 * Segmentation mask - IPL_DEPTH_32F *
131 * All the angles are in degrees, all the times are in milliseconds *
132 \****************************************************************************************/
133
134 /* Updates motion history image given motion silhouette */
135 CVAPI(void) cvUpdateMotionHistory( const CvArr* silhouette, CvArr* mhi,
136 double timestamp, double duration );
137
138 /* Calculates gradient of the motion history image and fills
139 a mask indicating where the gradient is valid */
140 CVAPI(void) cvCalcMotionGradient( const CvArr* mhi, CvArr* mask, CvArr* orientation,
141 double delta1, double delta2,
142 int aperture_size CV_DEFAULT(3));
143
144 /* Calculates average motion direction within a selected motion region
145 (region can be selected by setting ROIs and/or by composing a valid gradient mask
146 with the region mask) */
147 CVAPI(double) cvCalcGlobalOrientation( const CvArr* orientation, const CvArr* mask,
148 const CvArr* mhi, double timestamp,
149 double duration );
150
151 /* Splits a motion history image into a few parts corresponding to separate independent motions
152 (e.g. left hand, right hand) */
153 CVAPI(CvSeq*) cvSegmentMotion( const CvArr* mhi, CvArr* seg_mask,
154 CvMemStorage* storage,
155 double timestamp, double seg_thresh );
156
157 /****************************************************************************************\
158 * Tracking *
159 \****************************************************************************************/
160
161 /* Implements CAMSHIFT algorithm - determines object position, size and orientation
162 from the object histogram back project (extension of meanshift) */
163 CVAPI(int) cvCamShift( const CvArr* prob_image, CvRect window,
164 CvTermCriteria criteria, CvConnectedComp* comp,
165 CvBox2D* box CV_DEFAULT(NULL) );
166
167 /* Implements MeanShift algorithm - determines object position
168 from the object histogram back project */
169 CVAPI(int) cvMeanShift( const CvArr* prob_image, CvRect window,
170 CvTermCriteria criteria, CvConnectedComp* comp );
171
172 /*
173 standard Kalman filter (in G. Welch' and G. Bishop's notation):
174
175 x(k)=A*x(k-1)+B*u(k)+w(k) p(w)~N(0,Q)
176 z(k)=H*x(k)+v(k), p(v)~N(0,R)
177 */
178 typedef struct CvKalman
179 {
180 int MP; /* number of measurement vector dimensions */
181 int DP; /* number of state vector dimensions */
182 int CP; /* number of control vector dimensions */
183
184 /* backward compatibility fields */
185 #if 1
186 float* PosterState; /* =state_pre->data.fl */
187 float* PriorState; /* =state_post->data.fl */
188 float* DynamMatr; /* =transition_matrix->data.fl */
189 float* MeasurementMatr; /* =measurement_matrix->data.fl */
190 float* MNCovariance; /* =measurement_noise_cov->data.fl */
191 float* PNCovariance; /* =process_noise_cov->data.fl */
192 float* KalmGainMatr; /* =gain->data.fl */
193 float* PriorErrorCovariance;/* =error_cov_pre->data.fl */
194 float* PosterErrorCovariance;/* =error_cov_post->data.fl */
195 float* Temp1; /* temp1->data.fl */
196 float* Temp2; /* temp2->data.fl */
197 #endif
198
199 CvMat* state_pre; /* predicted state (x'(k)):
200 x(k)=A*x(k-1)+B*u(k) */
201 CvMat* state_post; /* corrected state (x(k)):
202 x(k)=x'(k)+K(k)*(z(k)-H*x'(k)) */
203 CvMat* transition_matrix; /* state transition matrix (A) */
204 CvMat* control_matrix; /* control matrix (B)
205 (it is not used if there is no control)*/
206 CvMat* measurement_matrix; /* measurement matrix (H) */
207 CvMat* process_noise_cov; /* process noise covariance matrix (Q) */
208 CvMat* measurement_noise_cov; /* measurement noise covariance matrix (R) */
209 CvMat* error_cov_pre; /* priori error estimate covariance matrix (P'(k)):
210 P'(k)=A*P(k-1)*At + Q)*/
211 CvMat* gain; /* Kalman gain matrix (K(k)):
212 K(k)=P'(k)*Ht*inv(H*P'(k)*Ht+R)*/
213 CvMat* error_cov_post; /* posteriori error estimate covariance matrix (P(k)):
214 P(k)=(I-K(k)*H)*P'(k) */
215 CvMat* temp1; /* temporary matrices */
216 CvMat* temp2;
217 CvMat* temp3;
218 CvMat* temp4;
219 CvMat* temp5;
220 } CvKalman;
221
222 /* Creates Kalman filter and sets A, B, Q, R and state to some initial values */
223 CVAPI(CvKalman*) cvCreateKalman( int dynam_params, int measure_params,
224 int control_params CV_DEFAULT(0));
225
226 /* Releases Kalman filter state */
227 CVAPI(void) cvReleaseKalman( CvKalman** kalman);
228
229 /* Updates Kalman filter by time (predicts future state of the system) */
230 CVAPI(const CvMat*) cvKalmanPredict( CvKalman* kalman,
231 const CvMat* control CV_DEFAULT(NULL));
232
233 /* Updates Kalman filter by measurement
234 (corrects state of the system and internal matrices) */
235 CVAPI(const CvMat*) cvKalmanCorrect( CvKalman* kalman, const CvMat* measurement );
236
237 #define cvKalmanUpdateByTime cvKalmanPredict
238 #define cvKalmanUpdateByMeasurement cvKalmanCorrect
239
240 #ifdef __cplusplus
241 }
242
243 namespace cv
244 {
245
247 CV_EXPORTS_W void updateMotionHistory( const Mat& silhouette, Mat& mhi,
248 double timestamp, double duration );
249
251 CV_EXPORTS_W void calcMotionGradient( const Mat& mhi, CV_OUT Mat& mask,
252 CV_OUT Mat& orientation,
253 double delta1, double delta2,
254 int apertureSize=3 );
255
257 CV_EXPORTS_W double calcGlobalOrientation( const Mat& orientation, const Mat& mask,
258 const Mat& mhi, double timestamp,
259 double duration );
260 // TODO: need good API for cvSegmentMotion
261
263 CV_EXPORTS_W RotatedRect CamShift( const Mat& probImage, CV_IN_OUT Rect& window,
264 TermCriteria criteria );
265
267 CV_EXPORTS_W int meanShift( const Mat& probImage, CV_IN_OUT Rect& window,
268 TermCriteria criteria );
269
277 class CV_EXPORTS_W KalmanFilter
278 {
279 public:
281 CV_WRAP KalmanFilter();
283 CV_WRAP KalmanFilter(int dynamParams, int measureParams, int controlParams=0, int type=CV_32F);
285 void init(int dynamParams, int measureParams, int controlParams=0, int type=CV_32F);
286
288 CV_WRAP const Mat& predict(const Mat& control=Mat());
290 CV_WRAP const Mat& correct(const Mat& measurement);
291
292 Mat statePre;
293 Mat statePost;
294 Mat transitionMatrix;
295 Mat controlMatrix;
296 Mat measurementMatrix;
297 Mat processNoiseCov;
298 Mat measurementNoiseCov;
299 Mat errorCovPre;
300 Mat gain;
301 Mat errorCovPost;
302
303 // temporary matrices
304 Mat temp1;
305 Mat temp2;
306 Mat temp3;
307 Mat temp4;
308 Mat temp5;
309 };
310
311
312 enum { OPTFLOW_USE_INITIAL_FLOW=4, OPTFLOW_FARNEBACK_GAUSSIAN=256 };
313
315 CV_EXPORTS_W void calcOpticalFlowPyrLK( const Mat& prevImg, const Mat& nextImg,
316 const vector<Point2f>& prevPts, CV_OUT vector<Point2f>& nextPts,
317 CV_OUT vector<uchar>& status, CV_OUT vector<float>& err,
318 Size winSize=Size(15,15), int maxLevel=3,
319 TermCriteria criteria=TermCriteria(
320 TermCriteria::COUNT+TermCriteria::EPS,
321 30, 0.01),
322 double derivLambda=0.5,
323 int flags=0 );
324
326 CV_EXPORTS_W void calcOpticalFlowFarneback( const Mat& prev, const Mat& next,
327 CV_OUT Mat& flow, double pyr_scale, int levels, int winsize,
328 int iterations, int poly_n, double poly_sigma, int flags );
329
330 }
331
332 #endif
333
334 #endif