Move the sources to trunk
[opencv] / cvaux / include / cvaux.h
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 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of Intel Corporation may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41
42 #ifndef __CVAUX__H__
43 #define __CVAUX__H__
44
45 #include "cv.h"
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51 CVAPI(CvSeq*) cvSegmentImage( const CvArr* srcarr, CvArr* dstarr,
52                                     double canny_threshold,
53                                     double ffill_threshold,
54                                     CvMemStorage* storage );
55
56 /****************************************************************************************\
57 *                                  Eigen objects                                         *
58 \****************************************************************************************/
59
60 typedef int (CV_CDECL * CvCallback)(int index, void* buffer, void* user_data);
61 typedef union
62 {
63     CvCallback callback;
64     void* data;
65 }
66 CvInput;
67
68 #define CV_EIGOBJ_NO_CALLBACK     0
69 #define CV_EIGOBJ_INPUT_CALLBACK  1
70 #define CV_EIGOBJ_OUTPUT_CALLBACK 2
71 #define CV_EIGOBJ_BOTH_CALLBACK   3
72
73 /* Calculates covariation matrix of a set of arrays */
74 CVAPI(void)  cvCalcCovarMatrixEx( int nObjects, void* input, int ioFlags,
75                                   int ioBufSize, uchar* buffer, void* userData,
76                                   IplImage* avg, float* covarMatrix );
77
78 /* Calculates eigen values and vectors of covariation matrix of a set of
79    arrays */
80 CVAPI(void)  cvCalcEigenObjects( int nObjects, void* input, void* output,
81                                  int ioFlags, int ioBufSize, void* userData,
82                                  CvTermCriteria* calcLimit, IplImage* avg,
83                                  float* eigVals );
84
85 /* Calculates dot product (obj - avg) * eigObj (i.e. projects image to eigen vector) */
86 CVAPI(double)  cvCalcDecompCoeff( IplImage* obj, IplImage* eigObj, IplImage* avg );
87
88 /* Projects image to eigen space (finds all decomposion coefficients */
89 CVAPI(void)  cvEigenDecomposite( IplImage* obj, int nEigObjs, void* eigInput,
90                                  int ioFlags, void* userData, IplImage* avg,
91                                  float* coeffs );
92
93 /* Projects original objects used to calculate eigen space basis to that space */
94 CVAPI(void)  cvEigenProjection( void* eigInput, int nEigObjs, int ioFlags,
95                                 void* userData, float* coeffs, IplImage* avg,
96                                 IplImage* proj );
97
98 /****************************************************************************************\
99 *                                       1D/2D HMM                                        *
100 \****************************************************************************************/
101
102 typedef struct CvImgObsInfo
103 {
104     int obs_x;
105     int obs_y;
106     int obs_size;
107     float* obs;//consequtive observations
108
109     int* state;/* arr of pairs superstate/state to which observation belong */
110     int* mix;  /* number of mixture to which observation belong */
111
112 }
113 CvImgObsInfo;/*struct for 1 image*/
114
115 typedef CvImgObsInfo Cv1DObsInfo;
116
117 typedef struct CvEHMMState
118 {
119     int num_mix;        /*number of mixtures in this state*/
120     float* mu;          /*mean vectors corresponding to each mixture*/
121     float* inv_var;     /* square root of inversed variances corresp. to each mixture*/
122     float* log_var_val; /* sum of 0.5 (LN2PI + ln(variance[i]) ) for i=1,n */
123     float* weight;      /*array of mixture weights. Summ of all weights in state is 1. */
124
125 }
126 CvEHMMState;
127
128 typedef struct CvEHMM
129 {
130     int level; /* 0 - lowest(i.e its states are real states), ..... */
131     int num_states; /* number of HMM states */
132     float*  transP;/*transition probab. matrices for states */
133     float** obsProb; /* if level == 0 - array of brob matrices corresponding to hmm
134                         if level == 1 - martix of matrices */
135     union
136     {
137         CvEHMMState* state; /* if level == 0 points to real states array,
138                                if not - points to embedded hmms */
139         struct CvEHMM* ehmm; /* pointer to an embedded model or NULL, if it is a leaf */
140     } u;
141
142 }
143 CvEHMM;
144
145 /*CVAPI(int)  icvCreate1DHMM( CvEHMM** this_hmm,
146                                    int state_number, int* num_mix, int obs_size );
147
148 CVAPI(int)  icvRelease1DHMM( CvEHMM** phmm );
149
150 CVAPI(int)  icvUniform1DSegm( Cv1DObsInfo* obs_info, CvEHMM* hmm );
151
152 CVAPI(int)  icvInit1DMixSegm( Cv1DObsInfo** obs_info_array, int num_img, CvEHMM* hmm);
153
154 CVAPI(int)  icvEstimate1DHMMStateParams( CvImgObsInfo** obs_info_array, int num_img, CvEHMM* hmm);
155
156 CVAPI(int)  icvEstimate1DObsProb( CvImgObsInfo* obs_info, CvEHMM* hmm );
157
158 CVAPI(int)  icvEstimate1DTransProb( Cv1DObsInfo** obs_info_array,
159                                            int num_seq,
160                                            CvEHMM* hmm );
161
162 CVAPI(float)  icvViterbi( Cv1DObsInfo* obs_info, CvEHMM* hmm);
163
164 CVAPI(int)  icv1DMixSegmL2( CvImgObsInfo** obs_info_array, int num_img, CvEHMM* hmm );*/
165
166 /*********************************** Embedded HMMs *************************************/
167
168 /* Creates 2D HMM */
169 CVAPI(CvEHMM*)  cvCreate2DHMM( int* stateNumber, int* numMix, int obsSize );
170
171 /* Releases HMM */
172 CVAPI(void)  cvRelease2DHMM( CvEHMM** hmm );
173
174 #define CV_COUNT_OBS(roi, win, delta, numObs )                                       \
175 {                                                                                    \
176    (numObs)->width  =((roi)->width  -(win)->width  +(delta)->width)/(delta)->width;  \
177    (numObs)->height =((roi)->height -(win)->height +(delta)->height)/(delta)->height;\
178 }
179
180 /* Creates storage for observation vectors */
181 CVAPI(CvImgObsInfo*)  cvCreateObsInfo( CvSize numObs, int obsSize );
182
183 /* Releases storage for observation vectors */
184 CVAPI(void)  cvReleaseObsInfo( CvImgObsInfo** obs_info );
185
186
187 /* The function takes an image on input and and returns the sequnce of observations
188    to be used with an embedded HMM; Each observation is top-left block of DCT
189    coefficient matrix */
190 CVAPI(void)  cvImgToObs_DCT( const CvArr* arr, float* obs, CvSize dctSize,
191                              CvSize obsSize, CvSize delta );
192
193
194 /* Uniformly segments all observation vectors extracted from image */
195 CVAPI(void)  cvUniformImgSegm( CvImgObsInfo* obs_info, CvEHMM* ehmm );
196
197 /* Does mixture segmentation of the states of embedded HMM */
198 CVAPI(void)  cvInitMixSegm( CvImgObsInfo** obs_info_array,
199                             int num_img, CvEHMM* hmm );
200
201 /* Function calculates means, variances, weights of every Gaussian mixture
202    of every low-level state of embedded HMM */
203 CVAPI(void)  cvEstimateHMMStateParams( CvImgObsInfo** obs_info_array,
204                                        int num_img, CvEHMM* hmm );
205
206 /* Function computes transition probability matrices of embedded HMM
207    given observations segmentation */
208 CVAPI(void)  cvEstimateTransProb( CvImgObsInfo** obs_info_array,
209                                   int num_img, CvEHMM* hmm );
210
211 /* Function computes probabilities of appearing observations at any state
212    (i.e. computes P(obs|state) for every pair(obs,state)) */
213 CVAPI(void)  cvEstimateObsProb( CvImgObsInfo* obs_info,
214                                 CvEHMM* hmm );
215
216 /* Runs Viterbi algorithm for embedded HMM */
217 CVAPI(float)  cvEViterbi( CvImgObsInfo* obs_info, CvEHMM* hmm );
218
219
220 /* Function clusters observation vectors from several images
221    given observations segmentation.
222    Euclidean distance used for clustering vectors.
223    Centers of clusters are given means of every mixture */
224 CVAPI(void)  cvMixSegmL2( CvImgObsInfo** obs_info_array,
225                           int num_img, CvEHMM* hmm );
226
227 /****************************************************************************************\
228 *               A few functions from old stereo gesture recognition demosions            *
229 \****************************************************************************************/
230
231 /* Creates hand mask image given several points on the hand */
232 CVAPI(void)  cvCreateHandMask( CvSeq* hand_points,
233                                    IplImage *img_mask, CvRect *roi);
234
235 /* Finds hand region in range image data */
236 CVAPI(void)  cvFindHandRegion (CvPoint3D32f* points, int count,
237                                 CvSeq* indexs,
238                                 float* line, CvSize2D32f size, int flag,
239                                 CvPoint3D32f* center,
240                                 CvMemStorage* storage, CvSeq **numbers);
241
242 /* Finds hand region in range image data (advanced version) */
243 CVAPI(void)  cvFindHandRegionA( CvPoint3D32f* points, int count,
244                                 CvSeq* indexs,
245                                 float* line, CvSize2D32f size, int jc,
246                                 CvPoint3D32f* center,
247                                 CvMemStorage* storage, CvSeq **numbers);
248
249 /****************************************************************************************\
250 *                           Additional operations on Subdivisions                        *
251 \****************************************************************************************/
252
253 // paints voronoi diagram: just demo function
254 CVAPI(void)  icvDrawMosaic( CvSubdiv2D* subdiv, IplImage* src, IplImage* dst );
255
256 // checks planar subdivision for correctness. It is not an absolute check,
257 // but it verifies some relations between quad-edges
258 CVAPI(int)   icvSubdiv2DCheck( CvSubdiv2D* subdiv );
259
260 // returns squared distance between two 2D points with floating-point coordinates.
261 CV_INLINE double icvSqDist2D32f( CvPoint2D32f pt1, CvPoint2D32f pt2 )
262 {
263     double dx = pt1.x - pt2.x;
264     double dy = pt1.y - pt2.y;
265
266     return dx*dx + dy*dy;
267 }
268
269
270 /****************************************************************************************\
271 *                           More operations on sequences                                 *
272 \****************************************************************************************/
273
274 /*****************************************************************************************/
275
276 #define CV_CURRENT_INT( reader ) (*((int *)(reader).ptr))
277 #define CV_PREV_INT( reader ) (*((int *)(reader).prev_elem))
278
279 #define  CV_GRAPH_WEIGHTED_VERTEX_FIELDS() CV_GRAPH_VERTEX_FIELDS()\
280     float weight;
281
282 #define  CV_GRAPH_WEIGHTED_EDGE_FIELDS() CV_GRAPH_EDGE_FIELDS()
283
284 typedef struct CvGraphWeightedVtx
285 {
286     CV_GRAPH_WEIGHTED_VERTEX_FIELDS()
287 }
288 CvGraphWeightedVtx;
289
290 typedef struct CvGraphWeightedEdge
291 {
292     CV_GRAPH_WEIGHTED_EDGE_FIELDS()
293 }
294 CvGraphWeightedEdge;
295
296 typedef enum CvGraphWeightType
297 {
298     CV_NOT_WEIGHTED,
299     CV_WEIGHTED_VTX,
300     CV_WEIGHTED_EDGE,
301     CV_WEIGHTED_ALL
302 } CvGraphWeightType;
303
304
305 /*****************************************************************************************/
306
307
308 /*******************************Stereo correspondence*************************************/
309
310 typedef struct CvCliqueFinder
311 {   
312     CvGraph* graph;
313     int**    adj_matr;
314     int N; //graph size
315
316     // stacks, counters etc/
317     int k; //stack size
318     int* current_comp;
319     int** All;
320     
321     int* ne;
322     int* ce;
323     int* fixp; //node with minimal disconnections
324     int* nod;
325     int* s; //for selected candidate
326     int status;
327     int best_score;
328     int weighted;
329     int weighted_edges;    
330     float best_weight;
331     float* edge_weights;
332     float* vertex_weights;
333     float* cur_weight;
334     float* cand_weight;
335
336 } CvCliqueFinder;
337
338 #define CLIQUE_TIME_OFF 2
339 #define CLIQUE_FOUND 1
340 #define CLIQUE_END   0
341
342 /*CVAPI(void) cvStartFindCliques( CvGraph* graph, CvCliqueFinder* finder, int reverse, 
343                                    int weighted CV_DEFAULT(0),  int weighted_edges CV_DEFAULT(0));
344 CVAPI(int) cvFindNextMaximalClique( CvCliqueFinder* finder, int* clock_rest CV_DEFAULT(0) ); 
345 CVAPI(void) cvEndFindCliques( CvCliqueFinder* finder );
346
347 CVAPI(void) cvBronKerbosch( CvGraph* graph );*/
348
349
350 /*F///////////////////////////////////////////////////////////////////////////////////////
351 //
352 //    Name:    cvSubgraphWeight
353 //    Purpose: finds weight of subgraph in a graph
354 //    Context:
355 //    Parameters:
356 //      graph - input graph.
357 //      subgraph - sequence of pairwise different ints.  These are indices of vertices of subgraph.
358 //      weight_type - describes the way we measure weight.
359 //            one of the following:
360 //            CV_NOT_WEIGHTED - weight of a clique is simply its size
361 //            CV_WEIGHTED_VTX - weight of a clique is the sum of weights of its vertices
362 //            CV_WEIGHTED_EDGE - the same but edges
363 //            CV_WEIGHTED_ALL - the same but both edges and vertices
364 //      weight_vtx - optional vector of floats, with size = graph->total.
365 //            If weight_type is either CV_WEIGHTED_VTX or CV_WEIGHTED_ALL
366 //            weights of vertices must be provided.  If weight_vtx not zero
367 //            these weights considered to be here, otherwise function assumes
368 //            that vertices of graph are inherited from CvGraphWeightedVtx.
369 //      weight_edge - optional matrix of floats, of width and height = graph->total.
370 //            If weight_type is either CV_WEIGHTED_EDGE or CV_WEIGHTED_ALL
371 //            weights of edges ought to be supplied.  If weight_edge is not zero
372 //            function finds them here, otherwise function expects
373 //            edges of graph to be inherited from CvGraphWeightedEdge.
374 //            If this parameter is not zero structure of the graph is determined from matrix
375 //            rather than from CvGraphEdge's.  In particular, elements corresponding to
376 //            absent edges should be zero.
377 //    Returns:
378 //      weight of subgraph.
379 //    Notes:
380 //F*/
381 /*CVAPI(float) cvSubgraphWeight( CvGraph *graph, CvSeq *subgraph,
382                                   CvGraphWeightType weight_type CV_DEFAULT(CV_NOT_WEIGHTED),
383                                   CvVect32f weight_vtx CV_DEFAULT(0),
384                                   CvMatr32f weight_edge CV_DEFAULT(0) );*/
385
386
387 /*F///////////////////////////////////////////////////////////////////////////////////////
388 //
389 //    Name:    cvFindCliqueEx
390 //    Purpose: tries to find clique with maximum possible weight in a graph
391 //    Context:
392 //    Parameters:
393 //      graph - input graph.
394 //      storage - memory storage to be used by the result.
395 //      is_complementary - optional flag showing whether function should seek for clique
396 //            in complementary graph.
397 //      weight_type - describes our notion about weight.
398 //            one of the following:
399 //            CV_NOT_WEIGHTED - weight of a clique is simply its size
400 //            CV_WEIGHTED_VTX - weight of a clique is the sum of weights of its vertices
401 //            CV_WEIGHTED_EDGE - the same but edges
402 //            CV_WEIGHTED_ALL - the same but both edges and vertices
403 //      weight_vtx - optional vector of floats, with size = graph->total.
404 //            If weight_type is either CV_WEIGHTED_VTX or CV_WEIGHTED_ALL
405 //            weights of vertices must be provided.  If weight_vtx not zero
406 //            these weights considered to be here, otherwise function assumes
407 //            that vertices of graph are inherited from CvGraphWeightedVtx.
408 //      weight_edge - optional matrix of floats, of width and height = graph->total.
409 //            If weight_type is either CV_WEIGHTED_EDGE or CV_WEIGHTED_ALL
410 //            weights of edges ought to be supplied.  If weight_edge is not zero
411 //            function finds them here, otherwise function expects
412 //            edges of graph to be inherited from CvGraphWeightedEdge.
413 //            Note that in case of CV_WEIGHTED_EDGE or CV_WEIGHTED_ALL
414 //            nonzero is_complementary implies nonzero weight_edge.
415 //      start_clique - optional sequence of pairwise different ints.  They are indices of
416 //            vertices that shall be present in the output clique.
417 //      subgraph_of_ban - optional sequence of (maybe equal) ints.  They are indices of
418 //            vertices that shall not be present in the output clique.
419 //      clique_weight_ptr - optional output parameter.  Weight of found clique stored here.
420 //      num_generations - optional number of generations in evolutionary part of algorithm,
421 //            zero forces to return first found clique.
422 //      quality - optional parameter determining degree of required quality/speed tradeoff.
423 //            Must be in the range from 0 to 9.
424 //            0 is fast and dirty, 9 is slow but hopefully yields good clique.
425 //    Returns:
426 //      sequence of pairwise different ints.
427 //      These are indices of vertices that form found clique.
428 //    Notes:
429 //      in cases of CV_WEIGHTED_EDGE and CV_WEIGHTED_ALL weights should be nonnegative.
430 //      start_clique has a priority over subgraph_of_ban.
431 //F*/
432 /*CVAPI(CvSeq*) cvFindCliqueEx( CvGraph *graph, CvMemStorage *storage,
433                                  int is_complementary CV_DEFAULT(0),
434                                  CvGraphWeightType weight_type CV_DEFAULT(CV_NOT_WEIGHTED),
435                                  CvVect32f weight_vtx CV_DEFAULT(0),
436                                  CvMatr32f weight_edge CV_DEFAULT(0),
437                                  CvSeq *start_clique CV_DEFAULT(0),
438                                  CvSeq *subgraph_of_ban CV_DEFAULT(0),
439                                  float *clique_weight_ptr CV_DEFAULT(0),
440                                  int num_generations CV_DEFAULT(3),
441                                  int quality CV_DEFAULT(2) );*/
442
443
444 #define CV_UNDEF_SC_PARAM         12345 //default value of parameters
445
446 #define CV_IDP_BIRCHFIELD_PARAM1  25    
447 #define CV_IDP_BIRCHFIELD_PARAM2  5
448 #define CV_IDP_BIRCHFIELD_PARAM3  12
449 #define CV_IDP_BIRCHFIELD_PARAM4  15
450 #define CV_IDP_BIRCHFIELD_PARAM5  25
451
452
453 #define  CV_DISPARITY_BIRCHFIELD  0    
454
455
456 /*F///////////////////////////////////////////////////////////////////////////
457 //
458 //    Name:    cvFindStereoCorrespondence
459 //    Purpose: find stereo correspondence on stereo-pair
460 //    Context:
461 //    Parameters:
462 //      leftImage - left image of stereo-pair (format 8uC1).
463 //      rightImage - right image of stereo-pair (format 8uC1).
464 //   mode - mode of correspondence retrieval (now CV_DISPARITY_BIRCHFIELD only)
465 //      dispImage - destination disparity image
466 //      maxDisparity - maximal disparity 
467 //      param1, param2, param3, param4, param5 - parameters of algorithm
468 //    Returns:
469 //    Notes:
470 //      Images must be rectified.
471 //      All images must have format 8uC1.
472 //F*/
473 CVAPI(void) 
474 cvFindStereoCorrespondence( 
475                    const  CvArr* leftImage, const  CvArr* rightImage,
476                    int     mode,
477                    CvArr*  dispImage,
478                    int     maxDisparity,                                
479                    double  param1 CV_DEFAULT(CV_UNDEF_SC_PARAM), 
480                    double  param2 CV_DEFAULT(CV_UNDEF_SC_PARAM), 
481                    double  param3 CV_DEFAULT(CV_UNDEF_SC_PARAM), 
482                    double  param4 CV_DEFAULT(CV_UNDEF_SC_PARAM), 
483                    double  param5 CV_DEFAULT(CV_UNDEF_SC_PARAM) );
484
485 /*****************************************************************************************/
486 /************ Epiline functions *******************/
487
488
489
490 typedef struct CvStereoLineCoeff
491 {
492     double Xcoef;
493     double XcoefA;
494     double XcoefB;
495     double XcoefAB;
496
497     double Ycoef;
498     double YcoefA;
499     double YcoefB;
500     double YcoefAB;
501
502     double Zcoef;
503     double ZcoefA;
504     double ZcoefB;
505     double ZcoefAB;
506 }CvStereoLineCoeff;
507
508
509 typedef struct CvCamera
510 {
511     float   imgSize[2]; /* size of the camera view, used during calibration */
512     float   matrix[9]; /* intinsic camera parameters:  [ fx 0 cx; 0 fy cy; 0 0 1 ] */
513     float   distortion[4]; /* distortion coefficients - two coefficients for radial distortion
514                               and another two for tangential: [ k1 k2 p1 p2 ] */
515     float   rotMatr[9];
516     float   transVect[3]; /* rotation matrix and transition vector relatively
517                              to some reference point in the space. */
518 }
519 CvCamera;
520
521 typedef struct CvStereoCamera
522 {
523     CvCamera* camera[2]; /* two individual camera parameters */
524     float fundMatr[9]; /* fundamental matrix */
525
526     /* New part for stereo */
527     CvPoint3D32f epipole[2];
528     CvPoint2D32f quad[2][4]; /* coordinates of destination quadrangle after
529                                 epipolar geometry rectification */
530     double coeffs[2][3][3];/* coefficients for transformation */
531     CvPoint2D32f border[2][4];
532     CvSize warpSize;
533     CvStereoLineCoeff* lineCoeffs;
534     int needSwapCameras;/* flag set to 1 if need to swap cameras for good reconstruction */
535     float rotMatrix[9];
536     float transVector[3];
537 }
538 CvStereoCamera;
539
540
541 typedef struct CvContourOrientation
542 {
543     float egvals[2];
544     float egvects[4];
545
546     float max, min; // minimum and maximum projections
547     int imax, imin;
548 } CvContourOrientation;
549
550 #define CV_CAMERA_TO_WARP 1
551 #define CV_WARP_TO_CAMERA 2
552
553 CVAPI(int) icvConvertWarpCoordinates(double coeffs[3][3],
554                                 CvPoint2D32f* cameraPoint,
555                                 CvPoint2D32f* warpPoint,
556                                 int direction);
557
558 CVAPI(int) icvGetSymPoint3D(  CvPoint3D64f pointCorner,
559                             CvPoint3D64f point1,
560                             CvPoint3D64f point2,
561                             CvPoint3D64f *pointSym2);
562
563 CVAPI(void) icvGetPieceLength3D(CvPoint3D64f point1,CvPoint3D64f point2,double* dist);
564
565 CVAPI(int) icvCompute3DPoint(    double alpha,double betta,
566                             CvStereoLineCoeff* coeffs,
567                             CvPoint3D64f* point);
568
569 CVAPI(int) icvCreateConvertMatrVect( CvMatr64d     rotMatr1,
570                                 CvMatr64d     transVect1,
571                                 CvMatr64d     rotMatr2,
572                                 CvMatr64d     transVect2,
573                                 CvMatr64d     convRotMatr,
574                                 CvMatr64d     convTransVect);
575
576 CVAPI(int) icvConvertPointSystem(CvPoint3D64f  M2,
577                             CvPoint3D64f* M1,
578                             CvMatr64d     rotMatr,
579                             CvMatr64d     transVect
580                             );
581
582 CVAPI(int) icvComputeCoeffForStereo(  CvStereoCamera* stereoCamera);
583
584 CVAPI(int) icvGetCrossPieceVector(CvPoint2D32f p1_start,CvPoint2D32f p1_end,CvPoint2D32f v2_start,CvPoint2D32f v2_end,CvPoint2D32f *cross);
585 CVAPI(int) icvGetCrossLineDirect(CvPoint2D32f p1,CvPoint2D32f p2,float a,float b,float c,CvPoint2D32f* cross);
586 CVAPI(float) icvDefinePointPosition(CvPoint2D32f point1,CvPoint2D32f point2,CvPoint2D32f point);
587 CVAPI(int) icvStereoCalibration( int numImages,
588                             int* nums,
589                             CvSize imageSize,
590                             CvPoint2D32f* imagePoints1,
591                             CvPoint2D32f* imagePoints2,
592                             CvPoint3D32f* objectPoints,
593                             CvStereoCamera* stereoparams
594                            );
595
596
597 CVAPI(int) icvComputeRestStereoParams(CvStereoCamera *stereoparams);
598
599 CVAPI(void) cvComputePerspectiveMap( const double coeffs[3][3], CvArr* rectMapX, CvArr* rectMapY );
600
601 CVAPI(int) icvComCoeffForLine(   CvPoint2D64f point1,
602                             CvPoint2D64f point2,
603                             CvPoint2D64f point3,
604                             CvPoint2D64f point4,
605                             CvMatr64d    camMatr1,
606                             CvMatr64d    rotMatr1,
607                             CvMatr64d    transVect1,
608                             CvMatr64d    camMatr2,
609                             CvMatr64d    rotMatr2,
610                             CvMatr64d    transVect2,
611                             CvStereoLineCoeff*    coeffs,
612                             int* needSwapCameras);
613
614 CVAPI(int) icvGetDirectionForPoint(  CvPoint2D64f point,
615                                 CvMatr64d camMatr,
616                                 CvPoint3D64f* direct);
617
618 CVAPI(int) icvGetCrossLines(CvPoint3D64f point11,CvPoint3D64f point12,
619                        CvPoint3D64f point21,CvPoint3D64f point22,
620                        CvPoint3D64f* midPoint);
621
622 CVAPI(int) icvComputeStereoLineCoeffs(   CvPoint3D64f pointA,
623                                     CvPoint3D64f pointB,
624                                     CvPoint3D64f pointCam1,
625                                     double gamma,
626                                     CvStereoLineCoeff*    coeffs);
627
628 /*CVAPI(int) icvComputeFundMatrEpipoles ( CvMatr64d camMatr1, 
629                                     CvMatr64d     rotMatr1, 
630                                     CvVect64d     transVect1,
631                                     CvMatr64d     camMatr2,
632                                     CvMatr64d     rotMatr2,
633                                     CvVect64d     transVect2,
634                                     CvPoint2D64f* epipole1,
635                                     CvPoint2D64f* epipole2,
636                                     CvMatr64d     fundMatr);*/
637
638 CVAPI(int) icvGetAngleLine( CvPoint2D64f startPoint, CvSize imageSize,CvPoint2D64f *point1,CvPoint2D64f *point2);
639
640 CVAPI(void) icvGetCoefForPiece(   CvPoint2D64f p_start,CvPoint2D64f p_end,
641                         double *a,double *b,double *c,
642                         int* result);
643
644 /*CVAPI(void) icvGetCommonArea( CvSize imageSize,
645                     CvPoint2D64f epipole1,CvPoint2D64f epipole2,
646                     CvMatr64d fundMatr,
647                     CvVect64d coeff11,CvVect64d coeff12,
648                     CvVect64d coeff21,CvVect64d coeff22,
649                     int* result);*/
650
651 CVAPI(void) icvComputeeInfiniteProject1(CvMatr64d    rotMatr,
652                                      CvMatr64d    camMatr1,
653                                      CvMatr64d    camMatr2,
654                                      CvPoint2D32f point1,
655                                      CvPoint2D32f *point2);
656
657 CVAPI(void) icvComputeeInfiniteProject2(CvMatr64d    rotMatr,
658                                      CvMatr64d    camMatr1,
659                                      CvMatr64d    camMatr2,
660                                      CvPoint2D32f* point1,
661                                      CvPoint2D32f point2);
662
663 CVAPI(void) icvGetCrossDirectDirect(  CvVect64d direct1,CvVect64d direct2,
664                             CvPoint2D64f *cross,int* result);
665
666 CVAPI(void) icvGetCrossPieceDirect(   CvPoint2D64f p_start,CvPoint2D64f p_end,
667                             double a,double b,double c,
668                             CvPoint2D64f *cross,int* result);
669
670 CVAPI(void) icvGetCrossPiecePiece( CvPoint2D64f p1_start,CvPoint2D64f p1_end,
671                             CvPoint2D64f p2_start,CvPoint2D64f p2_end,
672                             CvPoint2D64f* cross,
673                             int* result);
674                             
675 CVAPI(void) icvGetPieceLength(CvPoint2D64f point1,CvPoint2D64f point2,double* dist);
676
677 CVAPI(void) icvGetCrossRectDirect(    CvSize imageSize,
678                             double a,double b,double c,
679                             CvPoint2D64f *start,CvPoint2D64f *end,
680                             int* result);
681
682 CVAPI(void) icvProjectPointToImage(   CvPoint3D64f point,
683                             CvMatr64d camMatr,CvMatr64d rotMatr,CvVect64d transVect,
684                             CvPoint2D64f* projPoint);
685
686 CVAPI(void) icvGetQuadsTransform( CvSize        imageSize,
687                         CvMatr64d     camMatr1,
688                         CvMatr64d     rotMatr1,
689                         CvVect64d     transVect1,
690                         CvMatr64d     camMatr2,
691                         CvMatr64d     rotMatr2,
692                         CvVect64d     transVect2,
693                         CvSize*       warpSize,
694                         double quad1[4][2],
695                         double quad2[4][2],
696                         CvMatr64d     fundMatr,
697                         CvPoint3D64f* epipole1,
698                         CvPoint3D64f* epipole2
699                         );
700
701 CVAPI(void) icvGetQuadsTransformStruct(  CvStereoCamera* stereoCamera);
702
703 CVAPI(void) icvComputeStereoParamsForCameras(CvStereoCamera* stereoCamera);
704
705 CVAPI(void) icvGetCutPiece(   CvVect64d areaLineCoef1,CvVect64d areaLineCoef2,
706                     CvPoint2D64f epipole,
707                     CvSize imageSize,
708                     CvPoint2D64f* point11,CvPoint2D64f* point12,
709                     CvPoint2D64f* point21,CvPoint2D64f* point22,
710                     int* result);
711
712 CVAPI(void) icvGetMiddleAnglePoint(   CvPoint2D64f basePoint,
713                             CvPoint2D64f point1,CvPoint2D64f point2,
714                             CvPoint2D64f* midPoint);
715
716 CVAPI(void) icvGetNormalDirect(CvVect64d direct,CvPoint2D64f point,CvVect64d normDirect);
717
718 CVAPI(double) icvGetVect(CvPoint2D64f basePoint,CvPoint2D64f point1,CvPoint2D64f point2);
719
720 CVAPI(void) icvProjectPointToDirect(  CvPoint2D64f point,CvVect64d lineCoeff,
721                             CvPoint2D64f* projectPoint);
722
723 CVAPI(void) icvGetDistanceFromPointToDirect( CvPoint2D64f point,CvVect64d lineCoef,double*dist);
724
725 CVAPI(IplImage*) icvCreateIsometricImage( IplImage* src, IplImage* dst,
726                               int desired_depth, int desired_num_channels );
727
728 CVAPI(void) cvDeInterlace( const CvArr* frame, CvArr* fieldEven, CvArr* fieldOdd );
729
730 /*CVAPI(int) icvSelectBestRt(           int           numImages,
731                                     int*          numPoints,
732                                     CvSize        imageSize,
733                                     CvPoint2D32f* imagePoints1,
734                                     CvPoint2D32f* imagePoints2,
735                                     CvPoint3D32f* objectPoints,
736
737                                     CvMatr32f     cameraMatrix1,
738                                     CvVect32f     distortion1,
739                                     CvMatr32f     rotMatrs1,
740                                     CvVect32f     transVects1,
741
742                                     CvMatr32f     cameraMatrix2,
743                                     CvVect32f     distortion2,
744                                     CvMatr32f     rotMatrs2,
745                                     CvVect32f     transVects2,
746
747                                     CvMatr32f     bestRotMatr,
748                                     CvVect32f     bestTransVect
749                                     );*/
750
751 /****************************************************************************************\
752 *                                   Contour Morphing                                     *
753 \****************************************************************************************/
754
755 /* finds correspondence between two contours */
756 CvSeq* cvCalcContoursCorrespondence( const CvSeq* contour1,
757                                      const CvSeq* contour2, 
758                                      CvMemStorage* storage);
759
760 /* morphs contours using the pre-calculated correspondence:
761    alpha=0 ~ contour1, alpha=1 ~ contour2 */
762 CvSeq* cvMorphContours( const CvSeq* contour1, const CvSeq* contour2,
763                         CvSeq* corr, double alpha,
764                         CvMemStorage* storage );
765
766 /****************************************************************************************\
767 *                                    Texture Descriptors                                 *
768 \****************************************************************************************/
769
770 #define CV_GLCM_OPTIMIZATION_NONE                   -2
771 #define CV_GLCM_OPTIMIZATION_LUT                    -1
772 #define CV_GLCM_OPTIMIZATION_HISTOGRAM              0
773
774 #define CV_GLCMDESC_OPTIMIZATION_ALLOWDOUBLENEST    10
775 #define CV_GLCMDESC_OPTIMIZATION_ALLOWTRIPLENEST    11
776 #define CV_GLCMDESC_OPTIMIZATION_HISTOGRAM          4
777
778 #define CV_GLCMDESC_ENTROPY                         0
779 #define CV_GLCMDESC_ENERGY                          1
780 #define CV_GLCMDESC_HOMOGENITY                      2
781 #define CV_GLCMDESC_CONTRAST                        3
782 #define CV_GLCMDESC_CLUSTERTENDENCY                 4
783 #define CV_GLCMDESC_CLUSTERSHADE                    5
784 #define CV_GLCMDESC_CORRELATION                     6
785 #define CV_GLCMDESC_CORRELATIONINFO1                7
786 #define CV_GLCMDESC_CORRELATIONINFO2                8
787 #define CV_GLCMDESC_MAXIMUMPROBABILITY              9
788
789 #define CV_GLCM_ALL                                 0
790 #define CV_GLCM_GLCM                                1
791 #define CV_GLCM_DESC                                2
792
793 typedef struct CvGLCM CvGLCM;
794
795 CVAPI(CvGLCM*) cvCreateGLCM( const IplImage* srcImage,
796                                 int stepMagnitude,
797                                 const int* stepDirections CV_DEFAULT(0),
798                                 int numStepDirections CV_DEFAULT(0),
799                                 int optimizationType CV_DEFAULT(CV_GLCM_OPTIMIZATION_NONE));
800
801 CVAPI(void) cvReleaseGLCM( CvGLCM** GLCM, int flag CV_DEFAULT(CV_GLCM_ALL));
802
803 CVAPI(void) cvCreateGLCMDescriptors( CvGLCM* destGLCM,
804                                         int descriptorOptimizationType
805                                         CV_DEFAULT(CV_GLCMDESC_OPTIMIZATION_ALLOWDOUBLENEST));
806
807 CVAPI(double) cvGetGLCMDescriptor( CvGLCM* GLCM, int step, int descriptor );
808
809 CVAPI(void) cvGetGLCMDescriptorStatistics( CvGLCM* GLCM, int descriptor,
810                                               double* average, double* standardDeviation );
811
812 CVAPI(IplImage*) cvCreateGLCMImage( CvGLCM* GLCM, int step );
813
814 /****************************************************************************************\
815 *                                  Face eyes&mouth tracking                              *
816 \****************************************************************************************/
817
818
819 typedef struct CvFaceTracker CvFaceTracker;
820
821 #define CV_NUM_FACE_ELEMENTS    3 
822 enum CV_FACE_ELEMENTS
823 {
824     CV_FACE_MOUTH = 0,
825     CV_FACE_LEFT_EYE = 1,
826     CV_FACE_RIGHT_EYE = 2
827 };
828
829 CVAPI(CvFaceTracker*) cvInitFaceTracker(CvFaceTracker* pFaceTracking, const IplImage* imgGray,
830                                                 CvRect* pRects, int nRects);
831 CVAPI(int) cvTrackFace( CvFaceTracker* pFaceTracker, IplImage* imgGray,
832                               CvRect* pRects, int nRects,
833                               CvPoint* ptRotate, double* dbAngleRotate);
834 CVAPI(void) cvReleaseFaceTracker(CvFaceTracker** ppFaceTracker);
835
836
837 typedef struct CvFace
838 {
839     CvRect MouthRect;
840     CvRect LeftEyeRect;
841     CvRect RightEyeRect;
842 } CvFaceData;
843
844 CvSeq * cvFindFace(IplImage * Image,CvMemStorage* storage);
845 CvSeq * cvPostBoostingFindFace(IplImage * Image,CvMemStorage* storage);
846
847
848 /****************************************************************************************\
849 *                                         3D Tracker                                     *
850 \****************************************************************************************/
851
852 typedef unsigned char CvBool;
853
854 typedef struct
855 {
856     int id;
857     CvPoint2D32f p; // pgruebele: So we do not loose precision, this needs to be float
858 } Cv3dTracker2dTrackedObject;
859
860 CV_INLINE Cv3dTracker2dTrackedObject cv3dTracker2dTrackedObject(int id, CvPoint2D32f p)
861 {
862     Cv3dTracker2dTrackedObject r;
863     r.id = id;
864     r.p = p;
865     return r;
866 }
867
868 typedef struct
869 {
870     int id;
871     CvPoint3D32f p;             // location of the tracked object
872 } Cv3dTrackerTrackedObject;
873
874 CV_INLINE Cv3dTrackerTrackedObject cv3dTrackerTrackedObject(int id, CvPoint3D32f p)
875 {
876     Cv3dTrackerTrackedObject r;
877     r.id = id;
878     r.p = p;
879     return r;
880 }
881
882 typedef struct
883 {
884     CvBool valid;
885     float mat[4][4];              /* maps camera coordinates to world coordinates */
886     CvPoint2D32f principal_point; /* copied from intrinsics so this structure */
887                                   /* has all the info we need */
888 } Cv3dTrackerCameraInfo;
889
890 typedef struct
891 {
892     CvPoint2D32f principal_point;
893     float focal_length[2];
894     float distortion[4];
895 } Cv3dTrackerCameraIntrinsics;
896
897 CVAPI(CvBool) cv3dTrackerCalibrateCameras(int num_cameras,
898                      const Cv3dTrackerCameraIntrinsics camera_intrinsics[], /* size is num_cameras */
899                      CvSize etalon_size,
900                      float square_size,
901                      IplImage *samples[],                                   /* size is num_cameras */
902                      Cv3dTrackerCameraInfo camera_info[]);                  /* size is num_cameras */
903
904 CVAPI(int)  cv3dTrackerLocateObjects(int num_cameras, int num_objects,
905                    const Cv3dTrackerCameraInfo camera_info[],        /* size is num_cameras */
906                    const Cv3dTracker2dTrackedObject tracking_info[], /* size is num_objects*num_cameras */
907                    Cv3dTrackerTrackedObject tracked_objects[]);      /* size is num_objects */
908 /****************************************************************************************
909  tracking_info is a rectangular array; one row per camera, num_objects elements per row.
910  The id field of any unused slots must be -1. Ids need not be ordered or consecutive. On
911  completion, the return value is the number of objects located; i.e., the number of objects
912  visible by more than one camera. The id field of any unused slots in tracked objects is
913  set to -1.
914 ****************************************************************************************/
915
916
917 /****************************************************************************************\
918 *                           Skeletons and Linear-Contour Models                          *
919 \****************************************************************************************/
920
921 typedef enum CvLeeParameters
922 {
923     CV_LEE_INT = 0,
924     CV_LEE_FLOAT = 1,
925     CV_LEE_DOUBLE = 2,
926     CV_LEE_AUTO = -1,
927     CV_LEE_ERODE = 0,
928     CV_LEE_ZOOM = 1,
929     CV_LEE_NON = 2
930 } CvLeeParameters;
931
932 #define CV_NEXT_VORONOISITE2D( SITE ) ((SITE)->edge[0]->site[((SITE)->edge[0]->site[0] == (SITE))])
933 #define CV_PREV_VORONOISITE2D( SITE ) ((SITE)->edge[1]->site[((SITE)->edge[1]->site[0] == (SITE))])
934 #define CV_FIRST_VORONOIEDGE2D( SITE ) ((SITE)->edge[0])
935 #define CV_LAST_VORONOIEDGE2D( SITE ) ((SITE)->edge[1])
936 #define CV_NEXT_VORONOIEDGE2D( EDGE, SITE ) ((EDGE)->next[(EDGE)->site[0] != (SITE)])
937 #define CV_PREV_VORONOIEDGE2D( EDGE, SITE ) ((EDGE)->next[2 + ((EDGE)->site[0] != (SITE))])
938 #define CV_VORONOIEDGE2D_BEGINNODE( EDGE, SITE ) ((EDGE)->node[((EDGE)->site[0] != (SITE))])
939 #define CV_VORONOIEDGE2D_ENDNODE( EDGE, SITE ) ((EDGE)->node[((EDGE)->site[0] == (SITE))])
940 #define CV_TWIN_VORONOISITE2D( SITE, EDGE ) ( (EDGE)->site[((EDGE)->site[0] == (SITE))]) 
941
942 #define CV_VORONOISITE2D_FIELDS()    \
943     struct CvVoronoiNode2D *node[2]; \
944     struct CvVoronoiEdge2D *edge[2];
945
946 typedef struct CvVoronoiSite2D
947 {
948     CV_VORONOISITE2D_FIELDS()
949     struct CvVoronoiSite2D *next[2];
950 } CvVoronoiSite2D;
951
952 #define CV_VORONOIEDGE2D_FIELDS()    \
953     struct CvVoronoiNode2D *node[2]; \
954     struct CvVoronoiSite2D *site[2]; \
955     struct CvVoronoiEdge2D *next[4];
956
957 typedef struct CvVoronoiEdge2D
958 {
959     CV_VORONOIEDGE2D_FIELDS()
960 } CvVoronoiEdge2D;
961
962 #define CV_VORONOINODE2D_FIELDS()       \
963     CV_SET_ELEM_FIELDS(CvVoronoiNode2D) \
964     CvPoint2D32f pt;                    \
965     float radius;
966
967 typedef struct CvVoronoiNode2D
968 {
969     CV_VORONOINODE2D_FIELDS()
970 } CvVoronoiNode2D;
971
972 #define CV_VORONOIDIAGRAM2D_FIELDS() \
973     CV_GRAPH_FIELDS()                \
974     CvSet *sites;
975
976 typedef struct CvVoronoiDiagram2D
977 {
978     CV_VORONOIDIAGRAM2D_FIELDS()
979 } CvVoronoiDiagram2D;
980
981 /* Computes Voronoi Diagram for given polygons with holes */
982 CVAPI(int)  cvVoronoiDiagramFromContour(CvSeq* ContourSeq,
983                                            CvVoronoiDiagram2D** VoronoiDiagram,
984                                            CvMemStorage* VoronoiStorage,
985                                            CvLeeParameters contour_type CV_DEFAULT(CV_LEE_INT),
986                                            int contour_orientation CV_DEFAULT(-1),
987                                            int attempt_number CV_DEFAULT(10));
988
989 /* Computes Voronoi Diagram for domains in given image */
990 CVAPI(int)  cvVoronoiDiagramFromImage(IplImage* pImage,
991                                          CvSeq** ContourSeq,
992                                          CvVoronoiDiagram2D** VoronoiDiagram,
993                                          CvMemStorage* VoronoiStorage,
994                                          CvLeeParameters regularization_method CV_DEFAULT(CV_LEE_NON),
995                                          float approx_precision CV_DEFAULT(CV_LEE_AUTO));
996
997 /* Deallocates the storage */
998 CVAPI(void) cvReleaseVoronoiStorage(CvVoronoiDiagram2D* VoronoiDiagram,
999                                           CvMemStorage** pVoronoiStorage);
1000
1001 /*********************** Linear-Contour Model ****************************/
1002
1003 struct CvLCMEdge;
1004 struct CvLCMNode;
1005
1006 typedef struct CvLCMEdge
1007 {
1008     CV_GRAPH_EDGE_FIELDS() 
1009     CvSeq* chain;
1010     float width;
1011     int index1;
1012     int index2;
1013 } CvLCMEdge;
1014
1015 typedef struct CvLCMNode
1016 {
1017     CV_GRAPH_VERTEX_FIELDS()
1018     CvContour* contour; 
1019 } CvLCMNode;
1020
1021
1022 /* Computes hybrid model from Voronoi Diagram */
1023 CVAPI(CvGraph*) cvLinearContorModelFromVoronoiDiagram(CvVoronoiDiagram2D* VoronoiDiagram,
1024                                                          float maxWidth);
1025
1026 /* Releases hybrid model storage */
1027 CVAPI(int) cvReleaseLinearContorModelStorage(CvGraph** Graph);
1028
1029
1030 /* two stereo-related functions */
1031
1032 CVAPI(void) cvInitPerspectiveTransform( CvSize size, const CvPoint2D32f vertex[4], double matrix[3][3],
1033                                               CvArr* rectMap );
1034
1035 /*CVAPI(void) cvInitStereoRectification( CvStereoCamera* params,
1036                                              CvArr* rectMap1, CvArr* rectMap2,
1037                                              int do_undistortion );*/
1038
1039 /*************************** View Morphing Functions ************************/
1040
1041 /* The order of the function corresponds to the order they should appear in
1042    the view morphing pipeline */ 
1043
1044 /* Finds ending points of scanlines on left and right images of stereo-pair */
1045 CVAPI(void)  cvMakeScanlines( const CvMatrix3* matrix, CvSize  img_size,
1046                               int*  scanlines1, int*  scanlines2,
1047                               int*  lengths1, int*  lengths2,
1048                               int*  line_count );
1049
1050 /* Grab pixel values from scanlines and stores them sequentially
1051    (some sort of perspective image transform) */
1052 CVAPI(void)  cvPreWarpImage( int       line_count,
1053                              IplImage* img,
1054                              uchar*    dst,
1055                              int*      dst_nums,
1056                              int*      scanlines);
1057
1058 /* Approximate each grabbed scanline by a sequence of runs
1059    (lossy run-length compression) */
1060 CVAPI(void)  cvFindRuns( int    line_count,
1061                          uchar* prewarp1,
1062                          uchar* prewarp2,
1063                          int*   line_lengths1,
1064                          int*   line_lengths2,
1065                          int*   runs1,
1066                          int*   runs2,
1067                          int*   num_runs1,
1068                          int*   num_runs2);
1069
1070 /* Compares two sets of compressed scanlines */
1071 CVAPI(void)  cvDynamicCorrespondMulti( int  line_count,
1072                                        int* first,
1073                                        int* first_runs,
1074                                        int* second,
1075                                        int* second_runs,
1076                                        int* first_corr,
1077                                        int* second_corr);
1078
1079 /* Finds scanline ending coordinates for some intermediate "virtual" camera position */
1080 CVAPI(void)  cvMakeAlphaScanlines( int*  scanlines1,
1081                                    int*  scanlines2,
1082                                    int*  scanlinesA,
1083                                    int*  lengths,
1084                                    int   line_count,
1085                                    float alpha);
1086
1087 /* Blends data of the left and right image scanlines to get
1088    pixel values of "virtual" image scanlines */
1089 CVAPI(void)  cvMorphEpilinesMulti( int    line_count,
1090                                    uchar* first_pix,
1091                                    int*   first_num,
1092                                    uchar* second_pix,
1093                                    int*   second_num,
1094                                    uchar* dst_pix,
1095                                    int*   dst_num,
1096                                    float  alpha,
1097                                    int*   first,
1098                                    int*   first_runs,
1099                                    int*   second,
1100                                    int*   second_runs,
1101                                    int*   first_corr,
1102                                    int*   second_corr);
1103
1104 /* Does reverse warping of the morphing result to make
1105    it fill the destination image rectangle */
1106 CVAPI(void)  cvPostWarpImage( int       line_count,
1107                               uchar*    src,
1108                               int*      src_nums,
1109                               IplImage* img,
1110                               int*      scanlines);
1111
1112 /* Deletes Moire (missed pixels that appear due to discretization) */
1113 CVAPI(void)  cvDeleteMoire( IplImage*  img );
1114
1115
1116 /****************************************************************************************\
1117 *                           Background/foreground segmentation                           *
1118 \****************************************************************************************/
1119
1120 #define CV_BG_MODEL_FGD     0
1121 #define CV_BG_MODEL_MOG     1
1122 #define CV_BG_MODEL_FGD_SIMPLE   2
1123
1124 struct CvBGStatModel;
1125
1126 typedef void (CV_CDECL * CvReleaseBGStatModel)( struct CvBGStatModel** bg_model );
1127 typedef int (CV_CDECL * CvUpdateBGStatModel)( IplImage* curr_frame, struct CvBGStatModel* bg_model );
1128
1129 #define CV_BG_STAT_MODEL_FIELDS()                                                   \
1130     int             type; /*type of BG model*/                                      \
1131     CvReleaseBGStatModel release;                                                   \
1132     CvUpdateBGStatModel update;                                                     \
1133     IplImage*       background;   /*8UC3 reference background image*/               \
1134     IplImage*       foreground;   /*8UC1 foreground image*/                         \
1135     IplImage**      layers;       /*8UC3 reference background image, can be null */ \
1136     int             layer_count;  /* can be zero */                                 \
1137     CvMemStorage*   storage;      /*storage for \93foreground_regions\94*/              \
1138     CvSeq*          foreground_regions /*foreground object contours*/
1139
1140 typedef struct CvBGStatModel
1141 {
1142     CV_BG_STAT_MODEL_FIELDS();
1143 }
1144 CvBGStatModel;
1145
1146 // 
1147
1148 // Releases memory used by BGStatModel
1149 CV_INLINE void cvReleaseBGStatModel( CvBGStatModel** bg_model )
1150 {
1151     if( bg_model && *bg_model && (*bg_model)->release )
1152         (*bg_model)->release( bg_model );
1153 }
1154
1155 // Updates statistical model and returns number of found foreground regions
1156 CV_INLINE int cvUpdateBGStatModel( IplImage* current_frame, CvBGStatModel*  bg_model )
1157 {
1158     return bg_model && bg_model->update ? bg_model->update( current_frame, bg_model ) : 0;
1159 }
1160
1161 // Performs FG post-processing using segmentation
1162 // (all pixels of a region will be classified as foreground if majority of pixels of the region are FG).
1163 // parameters:
1164 //      segments - pointer to result of segmentation (for example MeanShiftSegmentation)
1165 //      bg_model - pointer to CvBGStatModel structure
1166 CVAPI(void) cvRefineForegroundMaskBySegm( CvSeq* segments, CvBGStatModel*  bg_model );
1167
1168 /* Common use change detection function */
1169 CVAPI(int)  cvChangeDetection( IplImage*  prev_frame,
1170                                IplImage*  curr_frame,
1171                                IplImage*  change_mask );
1172
1173 /*
1174   Interface of ACM MM2003 algorithm
1175   (Liyuan Li, Weimin Huang, Irene Y.H. Gu, and Qi Tian. 
1176   "Foreground Object Detection from Videos Containing Complex Background. ACM MM2003")
1177 */
1178
1179 /* default paremeters of foreground detection algorithm */
1180 #define  CV_BGFG_FGD_LC              128
1181 #define  CV_BGFG_FGD_N1C             15
1182 #define  CV_BGFG_FGD_N2C             25
1183
1184 #define  CV_BGFG_FGD_LCC             64
1185 #define  CV_BGFG_FGD_N1CC            25
1186 #define  CV_BGFG_FGD_N2CC            40
1187
1188 /* BG reference image update parameter */
1189 #define  CV_BGFG_FGD_ALPHA_1         0.1f
1190
1191 /* stat model update parameter
1192    0.002f ~ 1K frame(~45sec), 0.005 ~ 18sec (if 25fps and absolutely static BG) */
1193 #define  CV_BGFG_FGD_ALPHA_2         0.005f
1194
1195 /* start value for alpha parameter (to fast initiate statistic model) */
1196 #define  CV_BGFG_FGD_ALPHA_3         0.1f
1197
1198 #define  CV_BGFG_FGD_DELTA           2
1199
1200 #define  CV_BGFG_FGD_T               0.9f
1201
1202 #define  CV_BGFG_FGD_MINAREA         15.f
1203
1204 #define  CV_BGFG_FGD_BG_UPDATE_TRESH 0.5f
1205
1206 typedef struct CvFGDStatModelParams
1207 {
1208     int           Lc, N1c, N2c, Lcc, N1cc, N2cc, is_obj_without_holes, perform_morphing;
1209     float         alpha1, alpha2, alpha3, delta, T, minArea;
1210 }
1211 CvFGDStatModelParams;
1212
1213 typedef struct CvBGPixelCStatTable
1214 {
1215     float          Pv, Pvb;
1216     uchar          v[3];
1217 }
1218 CvBGPixelCStatTable;
1219
1220 typedef struct CvBGPixelCCStatTable
1221 {
1222     float          Pv, Pvb;
1223     uchar          v[6];
1224 }
1225 CvBGPixelCCStatTable;
1226
1227 typedef struct CvBGPixelStat
1228 {
1229     float                 Pbc;
1230     float                 Pbcc;
1231     CvBGPixelCStatTable*  ctable;
1232     CvBGPixelCCStatTable* cctable;
1233     uchar                 is_trained_st_model;
1234     uchar                 is_trained_dyn_model;
1235 }
1236 CvBGPixelStat;
1237
1238
1239 typedef struct CvFGDStatModel
1240 {
1241     CV_BG_STAT_MODEL_FIELDS();
1242     CvBGPixelStat*         pixel_stat;
1243     IplImage*              Ftd;
1244     IplImage*              Fbd;
1245     IplImage*              prev_frame;
1246     CvFGDStatModelParams   params;
1247 }
1248 CvFGDStatModel;
1249
1250 /* Creates FGD model */
1251 CVAPI(CvBGStatModel*) cvCreateFGDStatModel( IplImage* first_frame,
1252                     CvFGDStatModelParams* parameters CV_DEFAULT(NULL));
1253
1254 /* 
1255    Interface of Gaussian mixture algorithm
1256    (P. KadewTraKuPong and R. Bowden,
1257    "An improved adaptive background mixture model for real-time tracking with shadow detection"
1258    in Proc. 2nd European Workshp on Advanced Video-Based Surveillance Systems, 2001.")
1259 */
1260
1261 #define CV_BGFG_MOG_MAX_NGAUSSIANS 500
1262
1263 /* default parameters of gaussian background detection algorithm */
1264 #define CV_BGFG_MOG_BACKGROUND_THRESHOLD     0.7     /* threshold sum of weights for background test */
1265 #define CV_BGFG_MOG_STD_THRESHOLD            2.5     /* lambda=2.5 is 99% */
1266 #define CV_BGFG_MOG_WINDOW_SIZE              200     /* Learning rate; alpha = 1/CV_GBG_WINDOW_SIZE */
1267 #define CV_BGFG_MOG_NGAUSSIANS               5       /* = K = number of Gaussians in mixture */
1268 #define CV_BGFG_MOG_WEIGHT_INIT              0.05
1269 #define CV_BGFG_MOG_SIGMA_INIT               30
1270 #define CV_BGFG_MOG_MINAREA                  15.f
1271
1272
1273 #define CV_BGFG_MOG_NCOLORS                  3
1274
1275 typedef struct CvGaussBGStatModelParams
1276 {    
1277     int     win_size;               /* = 1/alpha */
1278     int     n_gauss;
1279     double  bg_threshold, std_threshold, minArea;
1280     double  weight_init, variance_init;
1281 }CvGaussBGStatModelParams;
1282
1283 typedef struct CvGaussBGValues
1284 {
1285     int         match_sum;
1286     double      weight;
1287     double      variance[CV_BGFG_MOG_NCOLORS];
1288     double      mean[CV_BGFG_MOG_NCOLORS];
1289 }
1290 CvGaussBGValues;
1291
1292 typedef struct CvGaussBGPoint
1293 {
1294     CvGaussBGValues* g_values;
1295 }
1296 CvGaussBGPoint;
1297
1298
1299 typedef struct CvGaussBGModel
1300 {
1301     CV_BG_STAT_MODEL_FIELDS();
1302     CvGaussBGStatModelParams   params;    
1303     CvGaussBGPoint*            g_point;    
1304     int                        countFrames;
1305 }
1306 CvGaussBGModel;
1307
1308
1309 /* Creates Gaussian mixture background model */
1310 CVAPI(CvBGStatModel*) cvCreateGaussianBGModel( IplImage* first_frame,
1311                 CvGaussBGStatModelParams* parameters CV_DEFAULT(NULL));
1312
1313 #ifdef __cplusplus
1314 }
1315 #endif
1316
1317 #ifdef __cplusplus
1318
1319 /****************************************************************************************\
1320 *                                   Calibration engine                                   *
1321 \****************************************************************************************/
1322
1323 typedef enum CvCalibEtalonType
1324 {
1325     CV_CALIB_ETALON_USER = -1,
1326     CV_CALIB_ETALON_CHESSBOARD = 0,
1327     CV_CALIB_ETALON_CHECKERBOARD = CV_CALIB_ETALON_CHESSBOARD
1328 }
1329 CvCalibEtalonType;
1330
1331 class CV_EXPORTS CvCalibFilter
1332 {
1333 public:
1334     /* Constructor & destructor */
1335     CvCalibFilter();
1336     virtual ~CvCalibFilter();
1337
1338     /* Sets etalon type - one for all cameras.
1339        etalonParams is used in case of pre-defined etalons (such as chessboard).
1340        Number of elements in etalonParams is determined by etalonType.
1341        E.g., if etalon type is CV_ETALON_TYPE_CHESSBOARD then:
1342          etalonParams[0] is number of squares per one side of etalon
1343          etalonParams[1] is number of squares per another side of etalon
1344          etalonParams[2] is linear size of squares in the board in arbitrary units.
1345        pointCount & points are used in case of
1346        CV_CALIB_ETALON_USER (user-defined) etalon. */
1347     virtual bool
1348         SetEtalon( CvCalibEtalonType etalonType, double* etalonParams,
1349                    int pointCount = 0, CvPoint2D32f* points = 0 );
1350
1351     /* Retrieves etalon parameters/or and points */
1352     virtual CvCalibEtalonType
1353         GetEtalon( int* paramCount = 0, const double** etalonParams = 0,
1354                    int* pointCount = 0, const CvPoint2D32f** etalonPoints = 0 ) const;
1355
1356     /* Sets number of cameras calibrated simultaneously. It is equal to 1 initially */
1357     virtual void SetCameraCount( int cameraCount );
1358
1359     /* Retrieves number of cameras */
1360     int GetCameraCount() const { return cameraCount; }
1361
1362     /* Starts cameras calibration */
1363     virtual bool SetFrames( int totalFrames );
1364     
1365     /* Stops cameras calibration */
1366     virtual void Stop( bool calibrate = false );
1367
1368     /* Retrieves number of cameras */
1369     bool IsCalibrated() const { return isCalibrated; }
1370
1371     /* Feeds another serie of snapshots (one per each camera) to filter.
1372        Etalon points on these images are found automatically.
1373        If the function can't locate points, it returns false */
1374     virtual bool FindEtalon( IplImage** imgs );
1375
1376     /* The same but takes matrices */
1377     virtual bool FindEtalon( CvMat** imgs );
1378
1379     /* Lower-level function for feeding filter with already found etalon points.
1380        Array of point arrays for each camera is passed. */
1381     virtual bool Push( const CvPoint2D32f** points = 0 );
1382
1383     /* Returns total number of accepted frames and, optionally,
1384        total number of frames to collect */
1385     virtual int GetFrameCount( int* framesTotal = 0 ) const;
1386
1387     /* Retrieves camera parameters for specified camera.
1388        If camera is not calibrated the function returns 0 */
1389     virtual const CvCamera* GetCameraParams( int idx = 0 ) const;
1390
1391     virtual const CvStereoCamera* GetStereoParams() const;
1392
1393     /* Sets camera parameters for all cameras */
1394     virtual bool SetCameraParams( CvCamera* params );
1395
1396     /* Saves all camera parameters to file */
1397     virtual bool SaveCameraParams( const char* filename );
1398     
1399     /* Loads all camera parameters from file */
1400     virtual bool LoadCameraParams( const char* filename );
1401
1402     /* Undistorts images using camera parameters. Some of src pointers can be NULL. */
1403     virtual bool Undistort( IplImage** src, IplImage** dst );
1404
1405     /* Undistorts images using camera parameters. Some of src pointers can be NULL. */
1406     virtual bool Undistort( CvMat** src, CvMat** dst );
1407
1408     /* Returns array of etalon points detected/partally detected
1409        on the latest frame for idx-th camera */
1410     virtual bool GetLatestPoints( int idx, CvPoint2D32f** pts,
1411                                                   int* count, bool* found );
1412
1413     /* Draw the latest detected/partially detected etalon */
1414     virtual void DrawPoints( IplImage** dst );
1415
1416     /* Draw the latest detected/partially detected etalon */
1417     virtual void DrawPoints( CvMat** dst );
1418
1419     virtual bool Rectify( IplImage** srcarr, IplImage** dstarr );
1420     virtual bool Rectify( CvMat** srcarr, CvMat** dstarr );
1421
1422 protected:
1423
1424     enum { MAX_CAMERAS = 3 };
1425
1426     /* etalon data */
1427     CvCalibEtalonType  etalonType;
1428     int     etalonParamCount;
1429     double* etalonParams;
1430     int     etalonPointCount;
1431     CvPoint2D32f* etalonPoints;
1432     CvSize  imgSize;
1433     CvMat*  grayImg;
1434     CvMat*  tempImg;
1435     CvMemStorage* storage;
1436
1437     /* camera data */
1438     int     cameraCount;
1439     CvCamera cameraParams[MAX_CAMERAS];
1440     CvStereoCamera stereo;
1441     CvPoint2D32f* points[MAX_CAMERAS];
1442     CvMat*  undistMap[MAX_CAMERAS][2];
1443     CvMat*  undistImg;
1444     int     latestCounts[MAX_CAMERAS];
1445     CvPoint2D32f* latestPoints[MAX_CAMERAS];
1446     CvMat*  rectMap[MAX_CAMERAS][2];
1447
1448     /* Added by Valery */
1449     //CvStereoCamera stereoParams;
1450
1451     int     maxPoints;
1452     int     framesTotal;
1453     int     framesAccepted;
1454     bool    isCalibrated;
1455 };
1456
1457 #include "cvaux.hpp"
1458 #include "cvvidsurv.hpp"
1459 /*#include "cvmat.hpp"*/
1460 #endif
1461
1462 #endif
1463
1464 /* End of file. */