]> rtime.felk.cvut.cz Git - opencv.git/blob - opencv/doc/cv_motion_tracking.tex
Many fixes for Python found by new tool; added Set?D and Get?D for Python
[opencv.git] / opencv / doc / cv_motion_tracking.tex
1 \section{Motion Analysis and Object Tracking}
2
3 \ifCPy
4
5 \cvCPyFunc{Acc}
6 Adds a frame to an accumulator.
7
8 \cvdefC{
9 void cvAcc( \par const CvArr* image,\par CvArr* sum,\par const CvArr* mask=NULL );
10 }\cvdefPy{Acc(image,smu,mask=NULL)-> None}
11
12 \begin{description}
13 \cvarg{image}{Input image, 1- or 3-channel, 8-bit or 32-bit floating point. (each channel of multi-channel image is processed independently)}
14 \cvarg{sum}{Accumulator with the same number of channels as input image, 32-bit or 64-bit floating-point}
15 \cvarg{mask}{Optional operation mask}
16 \end{description}
17
18 The function adds the whole image \texttt{image} or its selected region to the accumulator \texttt{sum}:
19
20 \[ \texttt{sum}(x,y) \leftarrow \texttt{sum}(x,y) + \texttt{image}(x,y) \quad \text{if} \quad \texttt{mask}(x,y) \ne 0 \]
21
22 \cvCPyFunc{CalcGlobalOrientation}
23 Calculates the global motion orientation of some selected region.
24
25 \cvdefC{
26 double cvCalcGlobalOrientation( \par const CvArr* orientation,\par const CvArr* mask,\par const CvArr* mhi,\par double timestamp,\par double duration );
27 }\cvdefPy{CalcGlobalOrientation(orientation,mask,mhi,timestamp,duration)-> float}
28
29 \begin{description}
30 \cvarg{orientation}{Motion gradient orientation image; calculated by the function \cvCPyCross{CalcMotionGradient}}
31 \cvarg{mask}{Mask image. It may be a conjunction of a valid gradient mask, obtained with \cvCPyCross{CalcMotionGradient} and the mask of the region, whose direction needs to be calculated}
32 \cvarg{mhi}{Motion history image}
33 \cvarg{timestamp}{Current time in milliseconds or other units, it is better to store time passed to \cvCPyCross{UpdateMotionHistory} before and reuse it here, because running \cvCPyCross{UpdateMotionHistory} and \cvCPyCross{CalcMotionGradient} on large images may take some time}
34 \cvarg{duration}{Maximal duration of motion track in milliseconds, the same as \cvCPyCross{UpdateMotionHistory}}
35 \end{description}
36
37 The function calculates the general
38 motion direction in the selected region and returns the angle between
39 0 degrees  and 360 degrees . At first the function builds the orientation histogram
40 and finds the basic orientation as a coordinate of the histogram
41 maximum. After that the function calculates the shift relative to the
42 basic orientation as a weighted sum of all of the orientation vectors: the more
43 recent the motion, the greater the weight. The resultant angle is
44 a circular sum of the basic orientation and the shift.
45
46 \cvCPyFunc{CalcMotionGradient}
47 Calculates the gradient orientation of a motion history image.
48
49 \cvdefC{
50 void cvCalcMotionGradient( \par const CvArr* mhi,\par CvArr* mask,\par CvArr* orientation,\par double delta1,\par double delta2,\par int apertureSize=3 );
51 }\cvdefPy{CalcMotionGradient(mhi,mask,orientation,delta1,delta2,apertureSize=3)-> None}
52
53 \begin{description}
54 \cvarg{mhi}{Motion history image}
55 \cvarg{mask}{Mask image; marks pixels where the motion gradient data is correct; output parameter}
56 \cvarg{orientation}{Motion gradient orientation image; contains angles from 0 to ~360 degrees }
57 \cvarg{delta1, delta2}{The function finds the minimum ($m(x,y)$) and maximum ($M(x,y)$) mhi values over each pixel $(x,y)$ neighborhood and assumes the gradient is valid only if
58 \[
59 \min(\texttt{delta1} , \texttt{delta2} ) \le M(x,y)-m(x,y) \le \max(\texttt{delta1} ,\texttt{delta2} ).
60 \]}
61 \cvarg{apertureSize}{Aperture size of derivative operators used by the function: CV\_SCHARR, 1, 3, 5 or 7 (see \cvCPyCross{Sobel})}
62 \end{description}
63
64 The function calculates the derivatives $Dx$ and $Dy$ of \texttt{mhi} and then calculates gradient orientation as:
65
66 \[
67 \texttt{orientation}(x,y)=\arctan{\frac{Dy(x,y)}{Dx(x,y)}}
68 \]
69
70 where both $Dx(x,y)$ and $Dy(x,y)$ signs are taken into account (as in the \cvCPyCross{CartToPolar} function). After that \texttt{mask} is filled to indicate where the orientation is valid (see the \texttt{delta1} and \texttt{delta2} description).
71
72 \cvCPyFunc{CalcOpticalFlowBM}
73 Calculates the optical flow for two images by using the block matching method.
74
75 \cvdefC{
76 void cvCalcOpticalFlowBM( \par const CvArr* prev,\par const CvArr* curr,\par CvSize blockSize,\par CvSize shiftSize,\par CvSize max\_range,\par int usePrevious,\par CvArr* velx,\par CvArr* vely );
77 }\cvdefPy{CalcOpticalFlowBM(prev,curr,blockSize,shiftSize,max\_range,usePrevious,velx,vely)-> None}
78
79 \begin{description}
80 \cvarg{prev}{First image, 8-bit, single-channel}
81 \cvarg{curr}{Second image, 8-bit, single-channel}
82 \cvarg{blockSize}{Size of basic blocks that are compared}
83 \cvarg{shiftSize}{Block coordinate increments}
84 \cvarg{max\_range}{Size of the scanned neighborhood in pixels around the block}
85 \cvarg{usePrevious}{Uses the previous (input) velocity field}
86 \cvarg{velx}{Horizontal component of the optical flow of
87 \[
88 \left\lfloor \frac{\texttt{prev->width} - \texttt{blockSize.width}}{\texttt{shiftSize.width}} \right\rfloor
89 \times
90 \left\lfloor \frac{\texttt{prev->height} - \texttt{blockSize.height}}{\texttt{shiftSize.height}} \right\rfloor
91 \]
92 size, 32-bit floating-point, single-channel}
93 \cvarg{vely}{Vertical component of the optical flow of the same size \texttt{velx}, 32-bit floating-point, single-channel}
94 \end{description}
95
96 The function calculates the optical
97 flow for overlapped blocks $\texttt{blockSize.width} \times \texttt{blockSize.height}$ pixels each, thus the velocity
98 fields are smaller than the original images. For every block in \texttt{prev} the functions tries to find a similar block in
99 \texttt{curr} in some neighborhood of the original block or shifted by (velx(x0,y0),vely(x0,y0)) block as has been calculated by previous
100 function call (if \texttt{usePrevious=1})
101
102 \cvCPyFunc{CalcOpticalFlowHS}
103 Calculates the optical flow for two images.
104
105 \cvdefC{
106 void cvCalcOpticalFlowHS( \par const CvArr* prev,\par const CvArr* curr,\par int usePrevious,\par CvArr* velx,\par CvArr* vely,\par double lambda,\par CvTermCriteria criteria );
107 }\cvdefPy{CalcOpticalFlowHS(prev,curr,usePrevious,velx,vely,lambda,criteria)-> None}
108
109 \begin{description}
110 \cvarg{prev}{First image, 8-bit, single-channel}
111 \cvarg{curr}{Second image, 8-bit, single-channel}
112 \cvarg{usePrevious}{Uses the previous (input) velocity field}
113 \cvarg{velx}{Horizontal component of the optical flow of the same size as input images, 32-bit floating-point, single-channel}
114 \cvarg{vely}{Vertical component of the optical flow of the same size as input images, 32-bit floating-point, single-channel}
115 \cvarg{lambda}{Lagrangian multiplier}
116 \cvarg{criteria}{Criteria of termination of velocity computing}
117 \end{description}
118
119 The function computes the flow for every pixel of the first input image using the Horn and Schunck algorithm
120 \cvCPyCross{Horn81}.
121
122 \cvCPyFunc{CalcOpticalFlowLK}
123 Calculates the optical flow for two images.
124
125 \cvdefC{
126 void cvCalcOpticalFlowLK( \par const CvArr* prev,\par const CvArr* curr,\par CvSize winSize,\par CvArr* velx,\par CvArr* vely );
127 }\cvdefPy{CalcOpticalFlowLK(prev,curr,winSize,velx,vely)-> None}
128 \begin{description}
129
130 \cvarg{prev}{First image, 8-bit, single-channel}
131 \cvarg{curr}{Second image, 8-bit, single-channel}
132 \cvarg{winSize}{Size of the averaging window used for grouping pixels}
133 \cvarg{velx}{Horizontal component of the optical flow of the same size as input images, 32-bit floating-point, single-channel}
134 \cvarg{vely}{Vertical component of the optical flow of the same size as input images, 32-bit floating-point, single-channel}
135 \end{description}
136
137 The function computes the flow for every pixel of the first input image using the Lucas and Kanade algorithm
138 \cvCPyCross{Lucas81}.
139
140 \cvCPyFunc{CalcOpticalFlowPyrLK}
141 Calculates the optical flow for a sparse feature set using the iterative Lucas-Kanade method with pyramids.
142
143 \cvdefC{
144 void cvCalcOpticalFlowPyrLK( \par const CvArr* prev,\par const CvArr* curr,\par CvArr* prevPyr,\par CvArr* currPyr,\par const CvPoint2D32f* prevFeatures,\par CvPoint2D32f* currFeatures,\par int count,\par CvSize winSize,\par int level,\par char* status,\par float* track\_error,\par CvTermCriteria criteria,\par int flags );
145 }\cvdefPy{
146 CalcOpticalFlowPyrLK(  prev, curr, prevPyr, currPyr, prevFeatures, CvSize winSize, int level, criteria, flags, guesses = None) -> (currFeatures, status, track\_error)
147 }
148
149 \begin{description}
150 \cvarg{prev}{First frame, at time \texttt{t}}
151 \cvarg{curr}{Second frame, at time \texttt{t + dt} }
152 \cvarg{prevPyr}{Buffer for the pyramid for the first frame. If the pointer is not \texttt{NULL} , the buffer must have a sufficient size to store the pyramid from level \texttt{1} to level \texttt{level} ; the total size of \texttt{(image\_width+8)*image\_height/3} bytes is sufficient}
153 \cvarg{currPyr}{Similar to \texttt{prevPyr}, used for the second frame}
154 \cvarg{prevFeatures}{Array of points for which the flow needs to be found}
155 \cvarg{currFeatures}{Array of 2D points containing the calculated new positions of the input features in the second image}
156 \cvarg{count}{Number of feature points}
157 \cvarg{winSize}{Size of the search window of each pyramid level}
158 \cvarg{level}{Maximal pyramid level number. If \texttt{0} , pyramids are not used (single level), if \texttt{1} , two levels are used, etc}
159 \cvarg{status}{Array. Every element of the array is set to \texttt{1} if the flow for the corresponding feature has been found, \texttt{0} otherwise}
160 \cvarg{error}{Array of double numbers containing the difference between patches around the original and moved points. Optional parameter; can be \texttt{NULL }}
161 \cvarg{criteria}{Specifies when the iteration process of finding the flow for each point on each pyramid level should be stopped}
162 \cvarg{flags}{Miscellaneous flags:
163 \begin{description}
164   \cvarg{CV\_LKFLOWPyr\_A\_READY}{pyramid for the first frame is precalculated before the call}
165   \cvarg{CV\_LKFLOWPyr\_B\_READY}{ pyramid for the second frame is precalculated before the call}
166   \cvC{\cvarg{CV\_LKFLOW\_INITIAL\_GUESSES}{array B contains initial coordinates of features before the function call}}
167 \end{description}}
168 \cvPy{\cvarg{guesses}{optional array of estimated coordinates of features in second frame, with same length as \texttt{prevFeatures}}}
169 \end{description}
170
171 The function implements the sparse iterative version of the Lucas-Kanade optical flow in pyramids
172 \cvCPyCross{Bouguet00}
173 . It calculates the coordinates of the feature points on the current video
174 frame given their coordinates on the previous frame. The function finds
175 the coordinates with sub-pixel accuracy.
176
177 Both parameters \texttt{prevPyr} and \texttt{currPyr} comply with the
178 following rules: if the image pointer is 0, the function allocates the
179 buffer internally, calculates the pyramid, and releases the buffer after
180 processing. Otherwise, the function calculates the pyramid and stores
181 it in the buffer unless the flag \texttt{CV\_LKFLOWPyr\_A[B]\_READY}
182 is set. The image should be large enough to fit the Gaussian pyramid
183 data. After the function call both pyramids are calculated and the
184 readiness flag for the corresponding image can be set in the next call
185 (i.e., typically, for all the image pairs except the very first one
186 \texttt{CV\_LKFLOWPyr\_A\_READY} is set).
187
188
189 \cvCPyFunc{CamShift}
190 Finds the object center, size, and orientation.
191
192 \cvdefC{
193 int cvCamShift( \par const CvArr* prob\_image,\par CvRect window,\par CvTermCriteria criteria,\par CvConnectedComp* comp,\par CvBox2D* box=NULL );
194 }\cvdefPy{CamShift(prob\_image,window,criteria,box=NULL)-> comp}
195
196 \begin{description}
197 \cvarg{prob\_image}{Back projection of object histogram (see \cvCPyCross{CalcBackProject})}
198 \cvarg{window}{Initial search window}
199 \cvarg{criteria}{Criteria applied to determine when the window search should be finished}
200 \cvarg{comp}{Resultant structure that contains the converged search window coordinates (\texttt{comp->rect} field) and the sum of all of the pixels inside the window (\texttt{comp->area} field)}
201 \cvarg{box}{Circumscribed box for the object. If not \texttt{NULL}, it contains object size and orientation}
202 \end{description}
203
204 The function implements the CAMSHIFT object tracking algrorithm
205 \cvCPyCross{Bradski98}.
206 First, it finds an object center using \cvCPyCross{MeanShift} and, after that, calculates the object size and orientation. The function returns number of iterations made within \cvCPyCross{MeanShift}.
207
208 The \cvCPyCross{CvCamShiftTracker} class declared in cv.hpp implements the color object tracker that uses the function.
209
210 \ifC % {
211 \subsection{CvConDensation}
212 ConDenstation state.
213
214 \begin{lstlisting}
215     typedef struct CvConDensation
216     {
217         int MP;     //Dimension of measurement vector
218         int DP;     // Dimension of state vector
219         float* DynamMatr;       // Matrix of the linear Dynamics system
220         float* State;           // Vector of State
221         int SamplesNum;         // Number of the Samples
222         float** flSamples;      // array of the Sample Vectors
223         float** flNewSamples;   // temporary array of the Sample Vectors
224         float* flConfidence;    // Confidence for each Sample
225         float* flCumulative;    // Cumulative confidence
226         float* Temp;            // Temporary vector
227         float* RandomSample;    // RandomVector to update sample set
228         CvRandState* RandS;     // Array of structures to generate random vectors
229     } CvConDensation;
230
231 \end{lstlisting}
232 The structure \texttt{CvConDensation} stores the CONditional DENSity propagATION tracker state. The information about the algorithm can be found at \url{http://www.dai.ed.ac.uk/CVonline/LOCAL\_COPIES/ISARD1/condensation.html}.
233
234 \cvCPyFunc{CreateConDensation}
235 Allocates the ConDensation filter structure.
236
237 \cvdefC{
238 CvConDensation* cvCreateConDensation( \par int dynam\_params,\par int measure\_params,\par int sample\_count );
239 }
240
241 \begin{description}
242 \cvarg{dynam\_params}{Dimension of the state vector}
243 \cvarg{measure\_params}{Dimension of the measurement vector}
244 \cvarg{sample\_count}{Number of samples}
245 \end{description}
246
247 The function creates a \texttt{CvConDensation} structure and returns a pointer to the structure.
248
249 \cvCPyFunc{ConDensInitSampleSet}
250 Initializes the sample set for the ConDensation algorithm.
251
252 \cvdefC{
253 void cvConDensInitSampleSet( CvConDensation* condens, \par CvMat* lower\_bound, \par CvMat* upper\_bound );
254 }
255
256 \begin{description}
257 \cvarg{condens}{Pointer to a structure to be initialized}
258 \cvarg{lower\_bound}{Vector of the lower boundary for each dimension}
259 \cvarg{upper\_bound}{Vector of the upper boundary for each dimension}
260 \end{description}
261
262 The function fills the samples arrays in the structure \cvCPyCross{CvConDensation} with values within the specified ranges.
263 \fi
264
265 \cvfunc{CvKalman}\label{CvKalman}
266 Kalman filter state.
267
268 \begin{lstlisting}
269 typedef struct CvKalman
270 {
271     int MP;                     /* number of measurement vector dimensions */
272     int DP;                     /* number of state vector dimensions */
273     int CP;                     /* number of control vector dimensions */
274
275     /* backward compatibility fields */
276 #if 1
277     float* PosterState;         /* =state_pre->data.fl */
278     float* PriorState;          /* =state_post->data.fl */
279     float* DynamMatr;           /* =transition_matrix->data.fl */
280     float* MeasurementMatr;     /* =measurement_matrix->data.fl */
281     float* MNCovariance;        /* =measurement_noise_cov->data.fl */
282     float* PNCovariance;        /* =process_noise_cov->data.fl */
283     float* KalmGainMatr;        /* =gain->data.fl */
284     float* PriorErrorCovariance;/* =error_cov_pre->data.fl */
285     float* PosterErrorCovariance;/* =error_cov_post->data.fl */
286     float* Temp1;               /* temp1->data.fl */
287     float* Temp2;               /* temp2->data.fl */
288 #endif
289
290     CvMat* state_pre;           /* predicted state (x'(k)):
291                                     x(k)=A*x(k-1)+B*u(k) */
292     CvMat* state_post;          /* corrected state (x(k)):
293                                     x(k)=x'(k)+K(k)*(z(k)-H*x'(k)) */
294     CvMat* transition_matrix;   /* state transition matrix (A) */
295     CvMat* control_matrix;      /* control matrix (B)
296                                    (it is not used if there is no control)*/
297     CvMat* measurement_matrix;  /* measurement matrix (H) */
298     CvMat* process_noise_cov;   /* process noise covariance matrix (Q) */
299     CvMat* measurement_noise_cov; /* measurement noise covariance matrix (R) */
300     CvMat* error_cov_pre;       /* priori error estimate covariance matrix (P'(k)):
301                                     P'(k)=A*P(k-1)*At + Q*/
302     CvMat* gain;                /* Kalman gain matrix (K(k)):
303                                     K(k)=P'(k)*Ht*inv(H*P'(k)*Ht+R)*/
304     CvMat* error_cov_post;      /* posteriori error estimate covariance matrix (P(k)):
305                                     P(k)=(I-K(k)*H)*P'(k) */
306     CvMat* temp1;               /* temporary matrices */
307     CvMat* temp2;
308     CvMat* temp3;
309     CvMat* temp4;
310     CvMat* temp5;
311 }
312 CvKalman;
313 \end{lstlisting}
314
315 The structure \texttt{CvKalman} is used to keep the Kalman filter
316 state. It is created by the \cvCPyCross{CreateKalman} function, updated
317 by the \cvCPyCross{KalmanPredict} and \cvCPyCross{KalmanCorrect} functions
318 and released by the \cvCPyCross{ReleaseKalman} function. Normally, the
319 structure is used for the standard Kalman filter (notation and the
320 formulas below are borrowed from the excellent Kalman tutorial
321 \cite{Welch95})
322
323 \[
324 \begin{array}{l}
325 x_k=A \cdot x_{k-1}+B \cdot u_k+w_k\\
326 z_k=H \cdot x_k+v_k
327 \end{array}
328 \]
329
330 where:
331
332 \[
333 \begin{array}{l l}
334 x_k\;(x_{k-1})& \text{state of the system at the moment \emph{k} (\emph{k-1})}\\
335 z_k & \text{measurement of the system state at the moment \emph{k}}\\
336 u_k & \text{external control applied at the moment \emph{k}}
337 \end{array}
338 \]
339
340 $w_k$ and $v_k$ are normally-distributed process and measurement noise, respectively:
341
342 \[
343 \begin{array}{l}
344 p(w) \sim N(0,Q)\\
345 p(v) \sim N(0,R)
346 \end{array}
347 \]
348
349 that is,
350
351 $Q$ process noise covariance matrix, constant or variable,
352
353 $R$ measurement noise covariance matrix, constant or variable
354
355 In the case of the standard Kalman filter, all of the matrices: A, B, H, Q and R are initialized once after the \cvCPyCross{CvKalman} structure is allocated via \cvCPyCross{CreateKalman}. However, the same structure and the same functions may be used to simulate the extended Kalman filter by linearizing the extended Kalman filter equation in the current system state neighborhood, in this case A, B, H (and, probably, Q and R) should be updated on every step.
356
357 \cvCPyFunc{CreateKalman}
358 Allocates the Kalman filter structure.
359
360 \cvdefC{
361 CvKalman* cvCreateKalman( \par int dynam\_params,\par int measure\_params,\par int control\_params=0 );
362 }
363
364 \cvdefPy{
365 CreateKalman(dynam\_params, measure\_params, control\_params=0) -> CvKalman
366 }
367
368 \begin{description}
369 \cvarg{dynam\_params}{dimensionality of the state vector}
370 \cvarg{measure\_params}{dimensionality of the measurement vector}
371 \cvarg{control\_params}{dimensionality of the control vector}
372 \end{description}
373
374 The function allocates \cvCPyCross{CvKalman} and all its matrices and initializes them somehow.
375
376
377 \cvCPyFunc{KalmanCorrect}
378 Adjusts the model state.
379
380 \cvdefC{
381 const CvMat* cvKalmanCorrect( CvKalman* kalman, const CvMat* measurement );
382 }
383 \cvdefPy{
384 KalmanCorrect(kalman, measurement) -> cvmat
385 }
386
387 \begin{lstlisting}
388 #define cvKalmanUpdateByMeasurement cvKalmanCorrect
389 \end{lstlisting}
390
391 \begin{description}
392 \ifC
393 \cvarg{kalman}{Pointer to the structure to be updated}
394 \else
395 \cvarg{kalman}{Kalman filter object returned by \cvCPyCross{CreateKalman}}
396 \fi
397 \cvarg{measurement}{CvMat containing the measurement vector}
398 \end{description}
399
400 The function adjusts the stochastic model state on the basis of the given measurement of the model state:
401
402 \[
403 \begin{array}{l}
404 K_k=P'_k \cdot H^T \cdot (H \cdot P'_k \cdot H^T+R)^{-1}\\
405 x_k=x'_k+K_k \cdot (z_k-H \cdot x'_k)\\
406 P_k=(I-K_k \cdot H) \cdot P'_k
407 \end{array}
408 \]
409
410 where
411
412 \begin{tabular}{l p{4 in}}
413 $z_k$ & given measurement (\texttt{mesurement} parameter)\\
414 $K_k$ & Kalman "gain" matrix.
415 \end{tabular}
416
417 The function stores the adjusted state at \texttt{kalman->state\_post} and returns it on output.
418
419 \ifC
420 Example. Using Kalman filter to track a rotating point
421 \begin{lstlisting}
422 #include "cv.h"
423 #include "highgui.h"
424 #include <math.h>
425
426 int main(int argc, char** argv)
427 {
428     /* A matrix data */
429     const float A[] = { 1, 1, 0, 1 };
430
431     IplImage* img = cvCreateImage( cvSize(500,500), 8, 3 );
432     CvKalman* kalman = cvCreateKalman( 2, 1, 0 );
433     /* state is (phi, delta_phi) - angle and angle increment */
434     CvMat* state = cvCreateMat( 2, 1, CV_32FC1 );
435     CvMat* process_noise = cvCreateMat( 2, 1, CV_32FC1 );
436     /* only phi (angle) is measured */
437     CvMat* measurement = cvCreateMat( 1, 1, CV_32FC1 );
438     CvRandState rng;
439     int code = -1;
440
441     cvRandInit( &rng, 0, 1, -1, CV_RAND_UNI );
442
443     cvZero( measurement );
444     cvNamedWindow( "Kalman", 1 );
445
446     for(;;)
447     {
448         cvRandSetRange( &rng, 0, 0.1, 0 );
449         rng.disttype = CV_RAND_NORMAL;
450
451         cvRand( &rng, state );
452
453         memcpy( kalman->transition_matrix->data.fl, A, sizeof(A));
454         cvSetIdentity( kalman->measurement_matrix, cvRealScalar(1) );
455         cvSetIdentity( kalman->process_noise_cov, cvRealScalar(1e-5) );
456         cvSetIdentity( kalman->measurement_noise_cov, cvRealScalar(1e-1) );
457         cvSetIdentity( kalman->error_cov_post, cvRealScalar(1));
458         /* choose random initial state */
459         cvRand( &rng, kalman->state_post );
460
461         rng.disttype = CV_RAND_NORMAL;
462
463         for(;;)
464         {
465             #define calc_point(angle)                                      \
466                 cvPoint( cvRound(img->width/2 + img->width/3*cos(angle)),  \
467                          cvRound(img->height/2 - img->width/3*sin(angle)))
468
469             float state_angle = state->data.fl[0];
470             CvPoint state_pt = calc_point(state_angle);
471
472             /* predict point position */
473             const CvMat* prediction = cvKalmanPredict( kalman, 0 );
474             float predict_angle = prediction->data.fl[0];
475             CvPoint predict_pt = calc_point(predict_angle);
476             float measurement_angle;
477             CvPoint measurement_pt;
478
479             cvRandSetRange( &rng,
480                             0,
481                             sqrt(kalman->measurement_noise_cov->data.fl[0]),
482                             0 );
483             cvRand( &rng, measurement );
484
485             /* generate measurement */
486             cvMatMulAdd( kalman->measurement_matrix, state, measurement, measurement );
487
488             measurement_angle = measurement->data.fl[0];
489             measurement_pt = calc_point(measurement_angle);
490
491             /* plot points */
492             #define draw_cross( center, color, d )                        \
493                 cvLine( img, cvPoint( center.x - d, center.y - d ),       \
494                              cvPoint( center.x + d, center.y + d ),       \
495                              color, 1, 0 );                               \
496                 cvLine( img, cvPoint( center.x + d, center.y - d ),       \
497                              cvPoint( center.x - d, center.y + d ),       \
498                              color, 1, 0 )
499
500             cvZero( img );
501             draw_cross( state_pt, CV_RGB(255,255,255), 3 );
502             draw_cross( measurement_pt, CV_RGB(255,0,0), 3 );
503             draw_cross( predict_pt, CV_RGB(0,255,0), 3 );
504             cvLine( img, state_pt, predict_pt, CV_RGB(255,255,0), 3, 0 );
505
506             /* adjust Kalman filter state */
507             cvKalmanCorrect( kalman, measurement );
508
509             cvRandSetRange( &rng,
510                             0,
511                             sqrt(kalman->process_noise_cov->data.fl[0]),
512                             0 );
513             cvRand( &rng, process_noise );
514             cvMatMulAdd( kalman->transition_matrix,
515                          state,
516                          process_noise,
517                          state );
518
519             cvShowImage( "Kalman", img );
520             code = cvWaitKey( 100 );
521
522             if( code > 0 ) /* break current simulation by pressing a key */
523                 break;
524         }
525         if( code == 27 ) /* exit by ESCAPE */
526             break;
527     }
528
529     return 0;
530 }
531 \end{lstlisting}
532 \fi
533
534 \cvCPyFunc{KalmanPredict}
535 Estimates the subsequent model state.
536
537 \cvdefC{
538 const CvMat* cvKalmanPredict( \par CvKalman* kalman, \par const CvMat* control=NULL );
539 }
540 \cvdefPy{
541 cvKalmanPredict(kalman, control=None) -> cvmat
542 }
543 \begin{lstlisting}
544 #define cvKalmanUpdateByTime cvKalmanPredict
545 \end{lstlisting}
546
547 \begin{description}
548 \ifC
549 \cvarg{kalman}{Kalman filter state}
550 \else
551 \cvarg{kalman}{Kalman filter object returned by \cvCPyCross{CreateKalman}}
552 \fi
553 \cvarg{control}{Control vector $u_k$, should be NULL iff there is no external control (\texttt{control\_params} =0)}
554 \end{description}
555
556 The function estimates the subsequent stochastic model state by its current state and stores it at \texttt{kalman->state\_pre}:
557
558 \[
559 \begin{array}{l}
560 x'_k=A \cdot x_{k-1}+B \cdot u_k\\
561 P'_k=A \cdot P_{k-1}+A^T + Q
562 \end{array}
563 \]
564
565 where
566
567 \begin{tabular}{l p{5 in}}
568 $x'_k$ & is predicted state \texttt{kalman->state\_pre},\\
569 $x_{k-1}$ & is corrected state on the previous step \texttt{kalman->state\_post}
570                 (should be initialized somehow in the beginning, zero vector by default),\\
571 $u_k$ & is external control (\texttt{control} parameter),\\
572 $P'_k$ & is priori error covariance matrix \texttt{kalman->error\_cov\_pre}\\
573 $P_{k-1}$ & is posteriori error covariance matrix on the previous step \texttt{kalman->error\_cov\_post}
574                 (should be initialized somehow in the beginning, identity matrix by default),
575 \end{tabular}
576
577 The function returns the estimated state.
578
579 \cvCPyFunc{MeanShift}
580 Finds the object center on back projection.
581
582 \cvdefC{
583 int cvMeanShift( \par const CvArr* prob\_image,\par CvRect window,\par CvTermCriteria criteria,\par CvConnectedComp* comp );
584 }\cvdefPy{MeanShift(prob\_image,window,criteria)-> comp}
585
586 \begin{description}
587 \cvarg{prob\_image}{Back projection of the object histogram (see \cvCPyCross{CalcBackProject})}
588 \cvarg{window}{Initial search window}
589 \cvarg{criteria}{Criteria applied to determine when the window search should be finished}
590 \cvarg{comp}{Resultant structure that contains the converged search window coordinates (\texttt{comp->rect} field) and the sum of all of the pixels inside the window (\texttt{comp->area} field)}
591 \end{description}
592
593 The function iterates to find the object center
594 given its back projection and initial position of search window. The
595 iterations are made until the search window center moves by less than
596 the given value and/or until the function has done the maximum number
597 of iterations. The function returns the number of iterations made.
598
599 \cvCPyFunc{MultiplyAcc}
600 Adds the product of two input images to the accumulator.
601
602 \cvdefC{
603 void cvMultiplyAcc( \par const CvArr* image1,\par const CvArr* image2,\par CvArr* acc,\par const CvArr* mask=NULL );
604 }\cvdefPy{MulitplyAcc(image1,image2,acc,mask=NULL)-> None}
605
606 \begin{description}
607 \cvarg{image1}{First input image, 1- or 3-channel, 8-bit or 32-bit floating point (each channel of multi-channel image is processed independently)}
608 \cvarg{image2}{Second input image, the same format as the first one}
609 \cvarg{acc}{Accumulator with the same number of channels as input images, 32-bit or 64-bit floating-point}
610 \cvarg{mask}{Optional operation mask}
611 \end{description}
612
613 The function adds the product of 2 images or their selected regions to the accumulator \texttt{acc}:
614
615 \[ \texttt{acc}(x,y) \leftarrow \texttt{acc}(x,y) + \texttt{image1}(x,y) \cdot \texttt{image2}(x,y) \quad \text{if} \quad \texttt{mask}(x,y) \ne 0 \]
616
617 \ifC % {
618 \cvCPyFunc{ReleaseConDensation}
619 Deallocates the ConDensation filter structure.
620
621 \cvdefC{
622 void cvReleaseConDensation( CvConDensation** condens );
623
624 }
625 \begin{description}
626 \cvarg{condens}{Pointer to the pointer to the structure to be released}
627 \end{description}
628
629 The function releases the structure \cvCPyCross{CvConDensation}) and frees all memory previously allocated for the structure.
630
631 \fi % }
632
633 \cvCPyFunc{ReleaseKalman}
634 Deallocates the Kalman filter structure.
635
636 \ifC % {
637
638 \cvdefC{
639 void cvReleaseKalman( \par CvKalman** kalman );
640 }
641
642 \begin{description}
643 \cvarg{kalman}{double pointer to the Kalman filter structure}
644 \end{description}
645
646 The function releases the structure \cvCPyCross{CvKalman} and all of the underlying matrices.
647
648 \fi % }
649
650 \cvCPyFunc{RunningAvg}
651 Updates the running average.
652
653 \cvdefC{
654 void cvRunningAvg( \par const CvArr* image,\par CvArr* acc,\par double alpha,\par const CvArr* mask=NULL );
655 }\cvdefPy{RunningAvg(image,acc,alpha,mask=NULL)-> None}
656
657 \begin{description}
658 \cvarg{image}{Input image, 1- or 3-channel, 8-bit or 32-bit floating point (each channel of multi-channel image is processed independently)}
659 \cvarg{acc}{Accumulator with the same number of channels as input image, 32-bit or 64-bit floating-point}
660 \cvarg{alpha}{Weight of input image}
661 \cvarg{mask}{Optional operation mask}
662 \end{description}
663
664 The function calculates the weighted sum of the input image
665 \texttt{image} and the accumulator \texttt{acc} so that \texttt{acc}
666 becomes a running average of frame sequence:
667
668 \[ \texttt{acc}(x,y) \leftarrow (1-\alpha) \cdot \texttt{acc}(x,y) + \alpha \cdot \texttt{image}(x,y) \quad \text{if} \quad \texttt{mask}(x,y) \ne 0 \]
669
670 where $\alpha$ (\texttt{alpha}) regulates the update speed (how fast the accumulator forgets about previous frames).
671
672
673 \cvCPyFunc{SegmentMotion}
674 Segments a whole motion into separate moving parts.
675
676 \cvdefC{
677 CvSeq* cvSegmentMotion( \par const CvArr* mhi,\par CvArr* seg\_mask,\par CvMemStorage* storage,\par double timestamp,\par double seg\_thresh );
678 }\cvdefPy{SegmentMotion(mhi,seg\_mask,storage,timestamp,seg\_thresh)-> None}
679
680 \begin{description}
681 \cvarg{mhi}{Motion history image}
682 \cvarg{seg\_mask}{Image where the mask found should be stored, single-channel, 32-bit floating-point}
683 \cvarg{storage}{Memory storage that will contain a sequence of motion connected components}
684 \cvarg{timestamp}{Current time in milliseconds or other units}
685 \cvarg{seg\_thresh}{Segmentation threshold; recommended to be equal to the interval between motion history "steps" or greater}
686 \end{description}
687
688 The function finds all of the motion segments and
689 marks them in \texttt{seg\_mask} with individual values (1,2,...). It
690 also returns a sequence of \cvCPyCross{CvConnectedComp}
691 structures, one for each motion component. After that the
692 motion direction for every component can be calculated with
693 \cvCPyCross{CalcGlobalOrientation} using the extracted mask of the particular
694 component \cvCPyCross{Cmp}.
695
696 \cvCPyFunc{SnakeImage}
697 Changes the contour position to minimize its energy.
698
699 \cvdefC{
700 void cvSnakeImage( \par const IplImage* image,\par CvPoint* points,\par int length,\par float* alpha,\par float* beta,\par float* gamma,\par int coeff\_usage,\par CvSize win,\par CvTermCriteria criteria,\par int calc\_gradient=1 );
701 }\cvdefPy{SnakeImage(image,points,alpha,beta,gamma,coeff\_usage,win,criteria,calc\_gradient=1)-> None}
702
703 \begin{description}
704 \cvarg{image}{The source image or external energy field}
705 \cvarg{points}{Contour points (snake)}
706 \cvarg{length}{Number of points in the contour}
707 \cvarg{alpha}{Weight[s] of continuity energy, single float or array of \texttt{length} floats, one for each contour point}
708 \cvarg{beta}{Weight[s] of curvature energy, similar to \texttt{alpha}}
709 \cvarg{gamma}{Weight[s] of image energy, similar to \texttt{alpha}}
710 \cvarg{coeff\_usage}{Different uses of the previous three parameters:
711 \begin{description}
712   \cvarg{CV\_VALUE}{indicates that each of \texttt{alpha, beta, gamma} is a pointer to a single value to be used for all points;}
713   \cvarg{CV\_ARRAY}{indicates that each of \texttt{alpha, beta, gamma} is a pointer to an array of coefficients different for all the points of the snake. All the arrays must have the size equal to the contour size.}
714 \end{description}}
715 \cvarg{win}{Size of neighborhood of every point used to search the minimum, both \texttt{win.width} and \texttt{win.height} must be odd}
716 \cvarg{criteria}{Termination criteria}
717 \cvarg{calc\_gradient}{Gradient flag; if not 0, the function calculates the gradient magnitude for every image pixel and consideres it as the energy field, otherwise the input image itself is considered}
718 \end{description}
719
720 The function updates the snake in order to minimize its
721 total energy that is a sum of internal energy that depends on the contour
722 shape (the smoother contour is, the smaller internal energy is) and
723 external energy that depends on the energy field and reaches minimum at
724 the local energy extremums that correspond to the image edges in the case
725 of using an image gradient.
726
727 The parameter \texttt{criteria.epsilon} is used to define the minimal
728 number of points that must be moved during any iteration to keep the
729 iteration process running.
730
731 If at some iteration the number of moved points is less
732 than \texttt{criteria.epsilon} or the function performed
733 \texttt{criteria.max\_iter} iterations, the function terminates.
734
735 \cvCPyFunc{SquareAcc}
736 Adds the square of the source image to the accumulator.
737
738 \cvdefC{
739 void cvSquareAcc( \par const CvArr* image,\par CvArr* sqsum,\par const CvArr* mask=NULL );
740 }\cvdefPy{SquareAcc(image,sqsum,mask=NULL)-> None}
741
742 \begin{description}
743 \cvarg{image}{Input image, 1- or 3-channel, 8-bit or 32-bit floating point (each channel of multi-channel image is processed independently)}
744 \cvarg{sqsum}{Accumulator with the same number of channels as input image, 32-bit or 64-bit floating-point}
745 \cvarg{mask}{Optional operation mask}
746 \end{description}
747
748 The function adds the input image \texttt{image} or its selected region, raised to power 2, to the accumulator \texttt{sqsum}:
749
750 \[ \texttt{sqsum}(x,y) \leftarrow \texttt{sqsum}(x,y) + \texttt{image}(x,y)^2 \quad \text{if} \quad \texttt{mask}(x,y) \ne 0 \]
751
752 \cvCPyFunc{UpdateMotionHistory}
753 Updates the motion history image by a moving silhouette.
754
755 \cvdefC{
756 void cvUpdateMotionHistory( \par const CvArr* silhouette,\par CvArr* mhi,\par double timestamp,\par double duration );
757 }\cvdefPy{UpdateMotionHistory(silhouette,mhi,timestamp,duration)-> None}
758
759 \begin{description}
760 \cvarg{silhouette}{Silhouette mask that has non-zero pixels where the motion occurs}
761 \cvarg{mhi}{Motion history image, that is updated by the function (single-channel, 32-bit floating-point)}
762 \cvarg{timestamp}{Current time in milliseconds or other units}
763 \cvarg{duration}{Maximal duration of the motion track in the same units as \texttt{timestamp}}
764 \end{description}
765
766 The function updates the motion history image as following:
767
768 \[
769 \texttt{mhi}(x,y)=\forkthree
770 {\texttt{timestamp}}{if $\texttt{silhouette}(x,y) \ne 0$}
771 {0}{if $\texttt{silhouette}(x,y) = 0$ and $\texttt{mhi} < (\texttt{timestamp} - \texttt{duration})$}
772 {\texttt{mhi}(x,y)}{otherwise}
773 \]
774 That is, MHI pixels where motion occurs are set to the current timestamp, while the pixels where motion happened far ago are cleared.
775
776 \fi
777
778 \ifCpp
779
780 \cvCppFunc{accumulate}
781 Adds image to the accumulator.
782
783 \cvdefCpp{void accumulate( const Mat\& src, Mat\& dst, const Mat\& mask=Mat() );}
784 \begin{description}
785 \cvarg{src}{The input image, 1- or 3-channel, 8-bit or 32-bit floating point}
786 \cvarg{dst}{The accumulator image with the same number of channels as input image, 32-bit or 64-bit floating-point}
787 \cvarg{mask}{Optional operation mask}
788 \end{description}
789
790 The function adds \texttt{src}, or some of its elements, to \texttt{dst}:
791
792 \[ \texttt{dst}(x,y) \leftarrow \texttt{dst}(x,y) + \texttt{src}(x,y) \quad \text{if} \quad \texttt{mask}(x,y) \ne 0 \]
793
794 The function supports multi-channel images; each channel is processed independently.
795
796 The functions \texttt{accumulate*} can be used, for example, to collect statistic of background of a scene, viewed by a still camera, for the further foreground-background segmentation.
797
798 See also: \cvCppCross{accumulateSquare}, \cvCppCross{accumulateProduct}, \cvCppCross{accumulateWeighted}
799
800 \cvCppFunc{accumulateSquare}
801 Adds the square of the source image to the accumulator.
802
803 \cvdefCpp{void accumulateSquare( const Mat\& src, Mat\& dst, \par const Mat\& mask=Mat() );}
804 \begin{description}
805 \cvarg{src}{The input image, 1- or 3-channel, 8-bit or 32-bit floating point}
806 \cvarg{dst}{The accumulator image with the same number of channels as input image, 32-bit or 64-bit floating-point}
807 \cvarg{mask}{Optional operation mask}
808 \end{description}
809
810 The function adds the input image \texttt{src} or its selected region, raised to power 2, to the accumulator \texttt{dst}:
811
812 \[ \texttt{dst}(x,y) \leftarrow \texttt{dst}(x,y) + \texttt{src}(x,y)^2 \quad \text{if} \quad \texttt{mask}(x,y) \ne 0 \]
813
814 The function supports multi-channel images; each channel is processed independently.
815
816 See also: \cvCppCross{accumulateSquare}, \cvCppCross{accumulateProduct}, \cvCppCross{accumulateWeighted}
817
818 \cvCppFunc{accumulateProduct}
819 Adds the per-element product of two input images to the accumulator.
820
821 \cvdefCpp{void accumulateProduct( const Mat\& src1, const Mat\& src2,\par
822                         Mat\& dst, const Mat\& mask=Mat() );}
823 \begin{description}
824 \cvarg{src1}{The first input image, 1- or 3-channel, 8-bit or 32-bit floating point}
825 \cvarg{src2}{The second input image of the same type and the same size as \texttt{src1}}
826 \cvarg{dst}{Accumulator with the same number of channels as input images, 32-bit or 64-bit floating-point}
827 \cvarg{mask}{Optional operation mask}
828 \end{description}
829
830 The function adds the product of 2 images or their selected regions to the accumulator \texttt{dst}:
831
832 \[ \texttt{dst}(x,y) \leftarrow \texttt{dst}(x,y) + \texttt{src1}(x,y) \cdot \texttt{src2}(x,y) \quad \text{if} \quad \texttt{mask}(x,y) \ne 0 \]
833
834 The function supports multi-channel images; each channel is processed independently.
835
836 See also: \cvCppCross{accumulate}, \cvCppCross{accumulateSquare}, \cvCppCross{accumulateWeighted}
837
838 \cvCppFunc{accumulateWeighted}
839 Updates the running average.
840
841 \cvdefCpp{void accumulateWeighted( const Mat\& src, Mat\& dst,\par
842                          double alpha, const Mat\& mask=Mat() );}
843 \begin{description}
844 \cvarg{src}{The input image, 1- or 3-channel, 8-bit or 32-bit floating point}
845 \cvarg{dst}{The accumulator image with the same number of channels as input image, 32-bit or 64-bit floating-point}
846 \cvarg{alpha}{Weight of the input image}
847 \cvarg{mask}{Optional operation mask}
848 \end{description}
849
850 The function calculates the weighted sum of the input image
851 \texttt{src} and the accumulator \texttt{dst} so that \texttt{dst}
852 becomes a running average of frame sequence:
853
854 \[ \texttt{dst}(x,y) \leftarrow (1-\texttt{alpha}) \cdot \texttt{dst}(x,y) + \texttt{alpha} \cdot \texttt{src}(x,y) \quad \text{if} \quad \texttt{mask}(x,y) \ne 0 \]
855
856 that is, \texttt{alpha} regulates the update speed (how fast the accumulator "forgets" about earlier images).
857 The function supports multi-channel images; each channel is processed independently.
858
859 See also: \cvCppCross{accumulate}, \cvCppCross{accumulateSquare}, \cvCppCross{accumulateProduct}
860
861 \cvCppFunc{calcOpticalFlowPyrLK}
862 Calculates the optical flow for a sparse feature set using the iterative Lucas-Kanade method with pyramids
863
864 \cvdefCpp{void calcOpticalFlowPyrLK( const Mat\& prevImg, const Mat\& nextImg,\par
865         const vector<Point2f>\& prevPts, vector<Point2f>\& nextPts,\par
866         vector<uchar>\& status, vector<float>\& err, \par
867         Size winSize=Size(15,15), int maxLevel=3,\par
868         TermCriteria criteria=TermCriteria(\par
869             TermCriteria::COUNT+TermCriteria::EPS, 30, 0.01),\par
870         double derivLambda=0.5, int flags=0 );}
871 \begin{description}
872 \cvarg{prevImg}{The first 8-bit single-channel or 3-channel input image}
873 \cvarg{nextImg}{The second input image of the same size and the same type as \texttt{prevImg}}
874 \cvarg{prevPts}{Vector of points for which the flow needs to be found}
875 \cvarg{nextPts}{The output vector of points containing the calculated new positions of the input features in the second image}
876 \cvarg{status}{The output status vector. Each element of the vector is set to 1 if the flow for the corresponding features has been found, 0 otherwise}
877 \cvarg{err}{The output vector that will contain the difference between patches around the original and moved points}
878 \cvarg{winSize}{Size of the search window at each pyramid level}
879 \cvarg{maxLevel}{0-based maximal pyramid level number. If 0, pyramids are not used (single level), if 1, two levels are used etc.}
880 \cvarg{criteria}{Specifies the termination criteria of the iterative search algorithm (after the specified maximum number of iterations \texttt{criteria.maxCount} or when the search window moves by less than \texttt{criteria.epsilon}}
881 \cvarg{derivLambda}{The relative weight of the spatial image derivatives impact to the optical flow estimation. If \texttt{derivLambda=0}, only the image intensity is used, if \texttt{derivLambda=1}, only derivatives are used. Any other values between 0 and 1 means that both derivatives and the image intensity are used (in the corresponding proportions).}
882 \cvarg{flags}{The operation flags:
883 \begin{description}
884   \cvarg{OPTFLOW\_USE\_INITIAL\_FLOW}{use initial estimations stored in \texttt{nextPts}. If the flag is not set, then initially $\texttt{nextPts}\leftarrow\texttt{prevPts}$}
885 \end{description}}
886 \end{description}
887
888 The function implements the sparse iterative version of the Lucas-Kanade optical flow in pyramids, see \cite{Bouguet00}.
889
890 \cvCppFunc{calcOpticalFlowFarneback}
891 Computes dense optical flow using Gunnar Farneback's algorithm
892
893 \cvdefCpp{void calcOpticalFlowFarneback( const Mat\& prevImg, const Mat\& nextImg,\par
894                                Mat\& flow, double pyrScale, int levels, int winsize,\par
895                                int iterations, int polyN, double polySigma, int flags );}
896 \begin{description}
897 \cvarg{prevImg}{The first 8-bit single-channel input image}
898 \cvarg{nextImg}{The second input image of the same size and the same type as \texttt{prevImg}}
899 \cvarg{flow}{The computed flow image; will have the same size as \texttt{prevImg} and type \texttt{CV\_32FC2}}
900 \cvarg{pyrScale}{Specifies the image scale (<1) to build the pyramids for each image. \texttt{pyrScale=0.5} means the classical pyramid, where each next layer is twice smaller than the previous}
901 \cvarg{levels}{The number of pyramid layers, including the initial image. \texttt{levels=1} means that no extra layers are created and only the original images are used}
902 \cvarg{winsize}{The averaging window size; The larger values increase the algorithm robustness to image noise and give more chances for fast motion detection, but yield more blurred motion field}
903 \cvarg{iterations}{The number of iterations the algorithm does at each pyramid level}
904 \cvarg{polyN}{Size of the pixel neighborhood used to find polynomial expansion in each pixel. The larger values mean that the image will be approximated with smoother surfaces, yielding more robust algorithm and more blurred  motion field. Typically, \texttt{polyN}=5 or 7}
905 \cvarg{polySigma}{Standard deviation of the Gaussian that is used to smooth derivatives that are used as a basis for the polynomial expansion. For \texttt{polyN=5} you can set \texttt{polySigma=1.1}, for \texttt{polyN=7} a good value would be \texttt{polySigma=1.5}}
906 \cvarg{flags}{The operation flags; can be a combination of the following:
907 \begin{description}
908     \cvarg{OPTFLOW\_USE\_INITIAL\_FLOW}{Use the input \texttt{flow} as the initial flow approximation}
909     \cvarg{OPTFLOW\_FARNEBACK\_GAUSSIAN}{Use a Gaussian $\texttt{winsize}\times\texttt{winsize}$ filter instead of box filter of the same size for optical flow estimation. Usually, this option gives more accurate flow than with a box filter, at the cost of lower speed (and normally \texttt{winsize} for a Gaussian window should be set to a larger value to achieve the same level of robustness)}
910 \end{description}}
911 \end{description}
912
913 The function finds optical flow for each \texttt{prevImg} pixel using the alorithm so that
914
915 \[\texttt{prevImg}(x,y) \sim \texttt{nextImg}(\texttt{flow}(x,y)[0], \texttt{flow}(x,y)[1])\]
916
917
918 \cvCppFunc{updateMotionHistory}
919 Updates the motion history image by a moving silhouette.
920
921 \cvdefCpp{void updateMotionHistory( const Mat\& silhouette, Mat\& mhi,\par
922                           double timestamp, double duration );}
923 \begin{description}
924 \cvarg{silhouette}{Silhouette mask that has non-zero pixels where the motion occurs}
925 \cvarg{mhi}{Motion history image, that is updated by the function (single-channel, 32-bit floating-point)}
926 \cvarg{timestamp}{Current time in milliseconds or other units}
927 \cvarg{duration}{Maximal duration of the motion track in the same units as \texttt{timestamp}}
928 \end{description}
929
930 The function updates the motion history image as following:
931
932 \[
933 \texttt{mhi}(x,y)=\forkthree
934 {\texttt{timestamp}}{if $\texttt{silhouette}(x,y) \ne 0$}
935 {0}{if $\texttt{silhouette}(x,y) = 0$ and $\texttt{mhi} < (\texttt{timestamp} - \texttt{duration})$}
936 {\texttt{mhi}(x,y)}{otherwise}
937 \]
938 That is, MHI pixels where motion occurs are set to the current \texttt{timestamp}, while the pixels where motion happened last time a long time ago are cleared.
939
940 The function, together with \cvCppCross{calcMotionGradient} and \cvCppCross{calcGlobalOrientation}, implements the motion templates technique, described in \cite{Davis97} and \cite{Bradski00}.
941 See also the OpenCV sample \texttt{motempl.c} that demonstrates the use of all the motion template functions.
942
943 \cvCppFunc{calcMotionGradient}
944 Calculates the gradient orientation of a motion history image.
945
946 \cvdefCpp{void calcMotionGradient( const Mat\& mhi, Mat\& mask,\par
947                          Mat\& orientation,\par
948                          double delta1, double delta2,\par
949                          int apertureSize=3 );}
950 \begin{description}
951 \cvarg{mhi}{Motion history single-channel floating-point image}
952 \cvarg{mask}{The output mask image; will have the type \texttt{CV\_8UC1} and the same size as \texttt{mhi}. Its non-zero elements will mark pixels where the motion gradient data is correct}
953 \cvarg{orientation}{The output motion gradient orientation image; will have the same type and the same size as \texttt{mhi}. Each pixel of it will the motion orientation in degrees, from 0 to 360.}
954 \cvarg{delta1, delta2}{The minimal and maximal allowed difference between \texttt{mhi} values within a pixel neighorhood. That is, the function finds the minimum ($m(x,y)$) and maximum ($M(x,y)$) \texttt{mhi} values over $3 \times 3$ neighborhood of each pixel and marks the motion orientation at $(x, y)$ as valid only if
955 \[
956 \min(\texttt{delta1} , \texttt{delta2} ) \le M(x,y)-m(x,y) \le \max(\texttt{delta1} ,\texttt{delta2}).
957 \]}
958 \cvarg{apertureSize}{The aperture size of \cvCppCross{Sobel} operator}
959 \end{description}
960
961 The function calculates the gradient orientation at each pixel $(x, y)$ as:
962
963 \[
964 \texttt{orientation}(x,y)=\arctan{\frac{d\texttt{mhi}/dy}{d\texttt{mhi}/dx}}
965 \]
966
967 (in fact, \cvCppCross{fastArctan} and \cvCppCross{phase} are used, so that the computed angle is measured in degrees and covers the full range 0..360). Also, the \texttt{mask} is filled to indicate pixels where the computed angle is valid.
968
969 \cvCppFunc{calcGlobalOrientation}
970 Calculates the global motion orientation in some selected region.
971
972 \cvdefCpp{double calcGlobalOrientation( const Mat\& orientation, const Mat\& mask,\par
973                               const Mat\& mhi, double timestamp,\par
974                               double duration );}
975 \begin{description}
976 \cvarg{orientation}{Motion gradient orientation image, calculated by the function \cvCppCross{calcMotionGradient}}
977 \cvarg{mask}{Mask image. It may be a conjunction of a valid gradient mask, also calculated by \cvCppCross{calcMotionGradient}, and the mask of the region, whose direction needs to be calculated}
978 \cvarg{mhi}{The motion history image, calculated by \cvCppCross{updateMotionHistory}}
979 \cvarg{timestamp}{The timestamp passed to \cvCppCross{updateMotionHistory}}
980 \cvarg{duration}{Maximal duration of motion track in milliseconds, passed to \cvCppCross{updateMotionHistory}}
981 \end{description}
982
983 The function calculates the average
984 motion direction in the selected region and returns the angle between
985 0 degrees  and 360 degrees. The average direction is computed from
986 the weighted orientation histogram, where a recent motion has larger
987 weight and the motion occurred in the past has smaller weight, as recorded in \texttt{mhi}.
988
989 \cvCppFunc{CamShift}
990 Finds the object center, size, and orientation
991
992 \cvdefCpp{RotatedRect CamShift( const Mat\& probImage, Rect\& window,\par
993                       TermCriteria criteria );}
994 \begin{description}
995 \cvarg{probImage}{Back projection of the object histogram; see \cvCppCross{calcBackProject}}
996 \cvarg{window}{Initial search window}
997 \cvarg{criteria}{Stop criteria for the underlying \cvCppCross{meanShift}}
998 \end{description}
999
1000 The function implements the CAMSHIFT object tracking algrorithm
1001 \cvCppCross{Bradski98}.
1002 First, it finds an object center using \cvCppCross{meanShift} and then adjust the window size and finds the optimal rotation. The function returns the rotated rectangle structure that includes the object position, size and the orientation. The next position of the search window can be obtained with \texttt{RotatedRect::boundingRect()}.
1003
1004 See the OpenCV sample \texttt{camshiftdemo.c} that tracks colored objects.
1005
1006 \cvCppFunc{meanShift}
1007 Finds the object on a back projection image.
1008
1009 \cvdefCpp{int meanShift( const Mat\& probImage, Rect\& window,\par
1010                TermCriteria criteria );}
1011 \begin{description}
1012 \cvarg{probImage}{Back projection of the object histogram; see \cvCppCross{calcBackProject}}
1013 \cvarg{window}{Initial search window}
1014 \cvarg{criteria}{The stop criteria for the iterative search algorithm}
1015 \end{description}
1016
1017 The function implements iterative object search algorithm. It takes the object back projection on input and the initial position. The mass center in \texttt{window} of the back projection image is computed and the search window center shifts to the mass center. The procedure is repeated until the specified number of iterations \texttt{criteria.maxCount} is done or until the window center shifts by less than \texttt{criteria.epsilon}. The algorithm is used inside \cvCppCross{CamShift} and, unlike \cvCppCross{CamShift}, the search window size or orientation do not change during the search. You can simply pass the output of \cvCppCross{calcBackProject} to this function, but better results can be obtained if you pre-filter the back projection and remove the noise (e.g. by retrieving connected components with \cvCppCross{findContours}, throwing away contours with small area (\cvCppCross{contourArea}) and rendering the  remaining contours with \cvCppCross{drawContours})
1018
1019
1020 \cvCppFunc{KalmanFilter}
1021 Kalman filter class
1022
1023 \begin{lstlisting}
1024 class KalmanFilter
1025 {
1026 public:
1027     KalmanFilter();newline
1028     KalmanFilter(int dynamParams, int measureParams, int controlParams=0);newline
1029     void init(int dynamParams, int measureParams, int controlParams=0);newline
1030     // predicts statePre from statePost
1031     const Mat& predict(const Mat& control=Mat());newline
1032     // corrects statePre based on the input measurement vector
1033     // and stores the result to statePost. 
1034     const Mat& correct(const Mat& measurement);newline
1035
1036     Mat statePre;           // predicted state (x'(k)):
1037                             //    x(k)=A*x(k-1)+B*u(k)
1038     Mat statePost;          // corrected state (x(k)):
1039                             //    x(k)=x'(k)+K(k)*(z(k)-H*x'(k))
1040     Mat transitionMatrix;   // state transition matrix (A)
1041     Mat controlMatrix;      // control matrix (B)
1042                             //   (it is not used if there is no control)
1043     Mat measurementMatrix;  // measurement matrix (H)
1044     Mat processNoiseCov;    // process noise covariance matrix (Q)
1045     Mat measurementNoiseCov;// measurement noise covariance matrix (R)
1046     Mat errorCovPre;        // priori error estimate covariance matrix (P'(k)):
1047                             //    P'(k)=A*P(k-1)*At + Q)*/
1048     Mat gain;               // Kalman gain matrix (K(k)):
1049                             //    K(k)=P'(k)*Ht*inv(H*P'(k)*Ht+R)
1050     Mat errorCovPost;       // posteriori error estimate covariance matrix (P(k)):
1051                             //    P(k)=(I-K(k)*H)*P'(k)
1052     ...
1053 };
1054 \end{lstlisting}
1055
1056 The class implements standard Kalman filter \url{http://en.wikipedia.org/wiki/Kalman_filter}. However, you can modify \texttt{transitionMatrix}, \texttt{controlMatrix} and \texttt{measurementMatrix} to get the extended Kalman filter functionality. See the OpenCV sample \texttt{kalman.c}
1057
1058 \fi