OpenCV 2.4.8 components for OpenCVgrabber.
[mmanager-3rdparty.git] / OpenCV2.4.8 / build / include / opencv2 / legacy / legacy.hpp
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 __OPENCV_LEGACY_HPP__
43 #define __OPENCV_LEGACY_HPP__
44
45 #include "opencv2/imgproc/imgproc.hpp"
46 #include "opencv2/imgproc/imgproc_c.h"
47 #include "opencv2/features2d/features2d.hpp"
48 #include "opencv2/calib3d/calib3d.hpp"
49 #include "opencv2/ml/ml.hpp"
50
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54
55 CVAPI(CvSeq*) cvSegmentImage( const CvArr* srcarr, CvArr* dstarr,
56                                     double canny_threshold,
57                                     double ffill_threshold,
58                                     CvMemStorage* storage );
59
60 /****************************************************************************************\
61 *                                  Eigen objects                                         *
62 \****************************************************************************************/
63
64 typedef int (CV_CDECL * CvCallback)(int index, void* buffer, void* user_data);
65 typedef union
66 {
67     CvCallback callback;
68     void* data;
69 }
70 CvInput;
71
72 #define CV_EIGOBJ_NO_CALLBACK     0
73 #define CV_EIGOBJ_INPUT_CALLBACK  1
74 #define CV_EIGOBJ_OUTPUT_CALLBACK 2
75 #define CV_EIGOBJ_BOTH_CALLBACK   3
76
77 /* Calculates covariation matrix of a set of arrays */
78 CVAPI(void)  cvCalcCovarMatrixEx( int nObjects, void* input, int ioFlags,
79                                   int ioBufSize, uchar* buffer, void* userData,
80                                   IplImage* avg, float* covarMatrix );
81
82 /* Calculates eigen values and vectors of covariation matrix of a set of
83    arrays */
84 CVAPI(void)  cvCalcEigenObjects( int nObjects, void* input, void* output,
85                                  int ioFlags, int ioBufSize, void* userData,
86                                  CvTermCriteria* calcLimit, IplImage* avg,
87                                  float* eigVals );
88
89 /* Calculates dot product (obj - avg) * eigObj (i.e. projects image to eigen vector) */
90 CVAPI(double)  cvCalcDecompCoeff( IplImage* obj, IplImage* eigObj, IplImage* avg );
91
92 /* Projects image to eigen space (finds all decomposion coefficients */
93 CVAPI(void)  cvEigenDecomposite( IplImage* obj, int nEigObjs, void* eigInput,
94                                  int ioFlags, void* userData, IplImage* avg,
95                                  float* coeffs );
96
97 /* Projects original objects used to calculate eigen space basis to that space */
98 CVAPI(void)  cvEigenProjection( void* eigInput, int nEigObjs, int ioFlags,
99                                 void* userData, float* coeffs, IplImage* avg,
100                                 IplImage* proj );
101
102 /****************************************************************************************\
103 *                                       1D/2D HMM                                        *
104 \****************************************************************************************/
105
106 typedef struct CvImgObsInfo
107 {
108     int obs_x;
109     int obs_y;
110     int obs_size;
111     float* obs;//consequtive observations
112
113     int* state;/* arr of pairs superstate/state to which observation belong */
114     int* mix;  /* number of mixture to which observation belong */
115
116 } CvImgObsInfo;/*struct for 1 image*/
117
118 typedef CvImgObsInfo Cv1DObsInfo;
119
120 typedef struct CvEHMMState
121 {
122     int num_mix;        /*number of mixtures in this state*/
123     float* mu;          /*mean vectors corresponding to each mixture*/
124     float* inv_var;     /* square root of inversed variances corresp. to each mixture*/
125     float* log_var_val; /* sum of 0.5 (LN2PI + ln(variance[i]) ) for i=1,n */
126     float* weight;      /*array of mixture weights. Summ of all weights in state is 1. */
127
128 } CvEHMMState;
129
130 typedef struct CvEHMM
131 {
132     int level; /* 0 - lowest(i.e its states are real states), ..... */
133     int num_states; /* number of HMM states */
134     float*  transP;/*transition probab. matrices for states */
135     float** obsProb; /* if level == 0 - array of brob matrices corresponding to hmm
136                         if level == 1 - martix of matrices */
137     union
138     {
139         CvEHMMState* state; /* if level == 0 points to real states array,
140                                if not - points to embedded hmms */
141         struct CvEHMM* ehmm; /* pointer to an embedded model or NULL, if it is a leaf */
142     } u;
143
144 } CvEHMM;
145
146 /*CVAPI(int)  icvCreate1DHMM( CvEHMM** this_hmm,
147                                    int state_number, int* num_mix, int obs_size );
148
149 CVAPI(int)  icvRelease1DHMM( CvEHMM** phmm );
150
151 CVAPI(int)  icvUniform1DSegm( Cv1DObsInfo* obs_info, CvEHMM* hmm );
152
153 CVAPI(int)  icvInit1DMixSegm( Cv1DObsInfo** obs_info_array, int num_img, CvEHMM* hmm);
154
155 CVAPI(int)  icvEstimate1DHMMStateParams( CvImgObsInfo** obs_info_array, int num_img, CvEHMM* hmm);
156
157 CVAPI(int)  icvEstimate1DObsProb( CvImgObsInfo* obs_info, CvEHMM* hmm );
158
159 CVAPI(int)  icvEstimate1DTransProb( Cv1DObsInfo** obs_info_array,
160                                            int num_seq,
161                                            CvEHMM* hmm );
162
163 CVAPI(float)  icvViterbi( Cv1DObsInfo* obs_info, CvEHMM* hmm);
164
165 CVAPI(int)  icv1DMixSegmL2( CvImgObsInfo** obs_info_array, int num_img, CvEHMM* hmm );*/
166
167 /*********************************** Embedded HMMs *************************************/
168
169 /* Creates 2D HMM */
170 CVAPI(CvEHMM*)  cvCreate2DHMM( int* stateNumber, int* numMix, int obsSize );
171
172 /* Releases HMM */
173 CVAPI(void)  cvRelease2DHMM( CvEHMM** hmm );
174
175 #define CV_COUNT_OBS(roi, win, delta, numObs )                                       \
176 {                                                                                    \
177    (numObs)->width  =((roi)->width  -(win)->width  +(delta)->width)/(delta)->width;  \
178    (numObs)->height =((roi)->height -(win)->height +(delta)->height)/(delta)->height;\
179 }
180
181 /* Creates storage for observation vectors */
182 CVAPI(CvImgObsInfo*)  cvCreateObsInfo( CvSize numObs, int obsSize );
183
184 /* Releases storage for observation vectors */
185 CVAPI(void)  cvReleaseObsInfo( CvImgObsInfo** obs_info );
186
187
188 /* The function takes an image on input and and returns the sequnce of observations
189    to be used with an embedded HMM; Each observation is top-left block of DCT
190    coefficient matrix */
191 CVAPI(void)  cvImgToObs_DCT( const CvArr* arr, float* obs, CvSize dctSize,
192                              CvSize obsSize, CvSize delta );
193
194
195 /* Uniformly segments all observation vectors extracted from image */
196 CVAPI(void)  cvUniformImgSegm( CvImgObsInfo* obs_info, CvEHMM* ehmm );
197
198 /* Does mixture segmentation of the states of embedded HMM */
199 CVAPI(void)  cvInitMixSegm( CvImgObsInfo** obs_info_array,
200                             int num_img, CvEHMM* hmm );
201
202 /* Function calculates means, variances, weights of every Gaussian mixture
203    of every low-level state of embedded HMM */
204 CVAPI(void)  cvEstimateHMMStateParams( CvImgObsInfo** obs_info_array,
205                                        int num_img, CvEHMM* hmm );
206
207 /* Function computes transition probability matrices of embedded HMM
208    given observations segmentation */
209 CVAPI(void)  cvEstimateTransProb( CvImgObsInfo** obs_info_array,
210                                   int num_img, CvEHMM* hmm );
211
212 /* Function computes probabilities of appearing observations at any state
213    (i.e. computes P(obs|state) for every pair(obs,state)) */
214 CVAPI(void)  cvEstimateObsProb( CvImgObsInfo* obs_info,
215                                 CvEHMM* hmm );
216
217 /* Runs Viterbi algorithm for embedded HMM */
218 CVAPI(float)  cvEViterbi( CvImgObsInfo* obs_info, CvEHMM* hmm );
219
220
221 /* Function clusters observation vectors from several images
222    given observations segmentation.
223    Euclidean distance used for clustering vectors.
224    Centers of clusters are given means of every mixture */
225 CVAPI(void)  cvMixSegmL2( CvImgObsInfo** obs_info_array,
226                           int num_img, CvEHMM* hmm );
227
228 /****************************************************************************************\
229 *               A few functions from old stereo gesture recognition demosions            *
230 \****************************************************************************************/
231
232 /* Creates hand mask image given several points on the hand */
233 CVAPI(void)  cvCreateHandMask( CvSeq* hand_points,
234                                    IplImage *img_mask, CvRect *roi);
235
236 /* Finds hand region in range image data */
237 CVAPI(void)  cvFindHandRegion (CvPoint3D32f* points, int count,
238                                 CvSeq* indexs,
239                                 float* line, CvSize2D32f size, int flag,
240                                 CvPoint3D32f* center,
241                                 CvMemStorage* storage, CvSeq **numbers);
242
243 /* Finds hand region in range image data (advanced version) */
244 CVAPI(void)  cvFindHandRegionA( CvPoint3D32f* points, int count,
245                                 CvSeq* indexs,
246                                 float* line, CvSize2D32f size, int jc,
247                                 CvPoint3D32f* center,
248                                 CvMemStorage* storage, CvSeq **numbers);
249
250 /* Calculates the cooficients of the homography matrix */
251 CVAPI(void)  cvCalcImageHomography( float* line, CvPoint3D32f* center,
252                                     float* intrinsic, float* homography );
253
254 /****************************************************************************************\
255 *                           More operations on sequences                                 *
256 \****************************************************************************************/
257
258 /*****************************************************************************************/
259
260 #define CV_CURRENT_INT( reader ) (*((int *)(reader).ptr))
261 #define CV_PREV_INT( reader ) (*((int *)(reader).prev_elem))
262
263 #define  CV_GRAPH_WEIGHTED_VERTEX_FIELDS() CV_GRAPH_VERTEX_FIELDS()\
264     float weight;
265
266 #define  CV_GRAPH_WEIGHTED_EDGE_FIELDS() CV_GRAPH_EDGE_FIELDS()
267
268 typedef struct CvGraphWeightedVtx
269 {
270     CV_GRAPH_WEIGHTED_VERTEX_FIELDS()
271 } CvGraphWeightedVtx;
272
273 typedef struct CvGraphWeightedEdge
274 {
275     CV_GRAPH_WEIGHTED_EDGE_FIELDS()
276 } CvGraphWeightedEdge;
277
278 typedef enum CvGraphWeightType
279 {
280     CV_NOT_WEIGHTED,
281     CV_WEIGHTED_VTX,
282     CV_WEIGHTED_EDGE,
283     CV_WEIGHTED_ALL
284 } CvGraphWeightType;
285
286
287 /* Calculates histogram of a contour */
288 CVAPI(void)  cvCalcPGH( const CvSeq* contour, CvHistogram* hist );
289
290 #define CV_DOMINANT_IPAN 1
291
292 /* Finds high-curvature points of the contour */
293 CVAPI(CvSeq*) cvFindDominantPoints( CvSeq* contour, CvMemStorage* storage,
294                                    int method CV_DEFAULT(CV_DOMINANT_IPAN),
295                                    double parameter1 CV_DEFAULT(0),
296                                    double parameter2 CV_DEFAULT(0),
297                                    double parameter3 CV_DEFAULT(0),
298                                    double parameter4 CV_DEFAULT(0));
299
300 /*****************************************************************************************/
301
302
303 /*******************************Stereo correspondence*************************************/
304
305 typedef struct CvCliqueFinder
306 {
307     CvGraph* graph;
308     int**    adj_matr;
309     int N; //graph size
310
311     // stacks, counters etc/
312     int k; //stack size
313     int* current_comp;
314     int** All;
315
316     int* ne;
317     int* ce;
318     int* fixp; //node with minimal disconnections
319     int* nod;
320     int* s; //for selected candidate
321     int status;
322     int best_score;
323     int weighted;
324     int weighted_edges;
325     float best_weight;
326     float* edge_weights;
327     float* vertex_weights;
328     float* cur_weight;
329     float* cand_weight;
330
331 } CvCliqueFinder;
332
333 #define CLIQUE_TIME_OFF 2
334 #define CLIQUE_FOUND 1
335 #define CLIQUE_END   0
336
337 /*CVAPI(void) cvStartFindCliques( CvGraph* graph, CvCliqueFinder* finder, int reverse,
338                                    int weighted CV_DEFAULT(0),  int weighted_edges CV_DEFAULT(0));
339 CVAPI(int) cvFindNextMaximalClique( CvCliqueFinder* finder, int* clock_rest CV_DEFAULT(0) );
340 CVAPI(void) cvEndFindCliques( CvCliqueFinder* finder );
341
342 CVAPI(void) cvBronKerbosch( CvGraph* graph );*/
343
344
345 /*F///////////////////////////////////////////////////////////////////////////////////////
346 //
347 //    Name:    cvSubgraphWeight
348 //    Purpose: finds weight of subgraph in a graph
349 //    Context:
350 //    Parameters:
351 //      graph - input graph.
352 //      subgraph - sequence of pairwise different ints.  These are indices of vertices of subgraph.
353 //      weight_type - describes the way we measure weight.
354 //            one of the following:
355 //            CV_NOT_WEIGHTED - weight of a clique is simply its size
356 //            CV_WEIGHTED_VTX - weight of a clique is the sum of weights of its vertices
357 //            CV_WEIGHTED_EDGE - the same but edges
358 //            CV_WEIGHTED_ALL - the same but both edges and vertices
359 //      weight_vtx - optional vector of floats, with size = graph->total.
360 //            If weight_type is either CV_WEIGHTED_VTX or CV_WEIGHTED_ALL
361 //            weights of vertices must be provided.  If weight_vtx not zero
362 //            these weights considered to be here, otherwise function assumes
363 //            that vertices of graph are inherited from CvGraphWeightedVtx.
364 //      weight_edge - optional matrix of floats, of width and height = graph->total.
365 //            If weight_type is either CV_WEIGHTED_EDGE or CV_WEIGHTED_ALL
366 //            weights of edges ought to be supplied.  If weight_edge is not zero
367 //            function finds them here, otherwise function expects
368 //            edges of graph to be inherited from CvGraphWeightedEdge.
369 //            If this parameter is not zero structure of the graph is determined from matrix
370 //            rather than from CvGraphEdge's.  In particular, elements corresponding to
371 //            absent edges should be zero.
372 //    Returns:
373 //      weight of subgraph.
374 //    Notes:
375 //F*/
376 /*CVAPI(float) cvSubgraphWeight( CvGraph *graph, CvSeq *subgraph,
377                                   CvGraphWeightType weight_type CV_DEFAULT(CV_NOT_WEIGHTED),
378                                   CvVect32f weight_vtx CV_DEFAULT(0),
379                                   CvMatr32f weight_edge CV_DEFAULT(0) );*/
380
381
382 /*F///////////////////////////////////////////////////////////////////////////////////////
383 //
384 //    Name:    cvFindCliqueEx
385 //    Purpose: tries to find clique with maximum possible weight in a graph
386 //    Context:
387 //    Parameters:
388 //      graph - input graph.
389 //      storage - memory storage to be used by the result.
390 //      is_complementary - optional flag showing whether function should seek for clique
391 //            in complementary graph.
392 //      weight_type - describes our notion about weight.
393 //            one of the following:
394 //            CV_NOT_WEIGHTED - weight of a clique is simply its size
395 //            CV_WEIGHTED_VTX - weight of a clique is the sum of weights of its vertices
396 //            CV_WEIGHTED_EDGE - the same but edges
397 //            CV_WEIGHTED_ALL - the same but both edges and vertices
398 //      weight_vtx - optional vector of floats, with size = graph->total.
399 //            If weight_type is either CV_WEIGHTED_VTX or CV_WEIGHTED_ALL
400 //            weights of vertices must be provided.  If weight_vtx not zero
401 //            these weights considered to be here, otherwise function assumes
402 //            that vertices of graph are inherited from CvGraphWeightedVtx.
403 //      weight_edge - optional matrix of floats, of width and height = graph->total.
404 //            If weight_type is either CV_WEIGHTED_EDGE or CV_WEIGHTED_ALL
405 //            weights of edges ought to be supplied.  If weight_edge is not zero
406 //            function finds them here, otherwise function expects
407 //            edges of graph to be inherited from CvGraphWeightedEdge.
408 //            Note that in case of CV_WEIGHTED_EDGE or CV_WEIGHTED_ALL
409 //            nonzero is_complementary implies nonzero weight_edge.
410 //      start_clique - optional sequence of pairwise different ints.  They are indices of
411 //            vertices that shall be present in the output clique.
412 //      subgraph_of_ban - optional sequence of (maybe equal) ints.  They are indices of
413 //            vertices that shall not be present in the output clique.
414 //      clique_weight_ptr - optional output parameter.  Weight of found clique stored here.
415 //      num_generations - optional number of generations in evolutionary part of algorithm,
416 //            zero forces to return first found clique.
417 //      quality - optional parameter determining degree of required quality/speed tradeoff.
418 //            Must be in the range from 0 to 9.
419 //            0 is fast and dirty, 9 is slow but hopefully yields good clique.
420 //    Returns:
421 //      sequence of pairwise different ints.
422 //      These are indices of vertices that form found clique.
423 //    Notes:
424 //      in cases of CV_WEIGHTED_EDGE and CV_WEIGHTED_ALL weights should be nonnegative.
425 //      start_clique has a priority over subgraph_of_ban.
426 //F*/
427 /*CVAPI(CvSeq*) cvFindCliqueEx( CvGraph *graph, CvMemStorage *storage,
428                                  int is_complementary CV_DEFAULT(0),
429                                  CvGraphWeightType weight_type CV_DEFAULT(CV_NOT_WEIGHTED),
430                                  CvVect32f weight_vtx CV_DEFAULT(0),
431                                  CvMatr32f weight_edge CV_DEFAULT(0),
432                                  CvSeq *start_clique CV_DEFAULT(0),
433                                  CvSeq *subgraph_of_ban CV_DEFAULT(0),
434                                  float *clique_weight_ptr CV_DEFAULT(0),
435                                  int num_generations CV_DEFAULT(3),
436                                  int quality CV_DEFAULT(2) );*/
437
438
439 #define CV_UNDEF_SC_PARAM         12345 //default value of parameters
440
441 #define CV_IDP_BIRCHFIELD_PARAM1  25
442 #define CV_IDP_BIRCHFIELD_PARAM2  5
443 #define CV_IDP_BIRCHFIELD_PARAM3  12
444 #define CV_IDP_BIRCHFIELD_PARAM4  15
445 #define CV_IDP_BIRCHFIELD_PARAM5  25
446
447
448 #define  CV_DISPARITY_BIRCHFIELD  0
449
450
451 /*F///////////////////////////////////////////////////////////////////////////
452 //
453 //    Name:    cvFindStereoCorrespondence
454 //    Purpose: find stereo correspondence on stereo-pair
455 //    Context:
456 //    Parameters:
457 //      leftImage - left image of stereo-pair (format 8uC1).
458 //      rightImage - right image of stereo-pair (format 8uC1).
459 //   mode - mode of correspondence retrieval (now CV_DISPARITY_BIRCHFIELD only)
460 //      dispImage - destination disparity image
461 //      maxDisparity - maximal disparity
462 //      param1, param2, param3, param4, param5 - parameters of algorithm
463 //    Returns:
464 //    Notes:
465 //      Images must be rectified.
466 //      All images must have format 8uC1.
467 //F*/
468 CVAPI(void)
469 cvFindStereoCorrespondence(
470                    const  CvArr* leftImage, const  CvArr* rightImage,
471                    int     mode,
472                    CvArr*  dispImage,
473                    int     maxDisparity,
474                    double  param1 CV_DEFAULT(CV_UNDEF_SC_PARAM),
475                    double  param2 CV_DEFAULT(CV_UNDEF_SC_PARAM),
476                    double  param3 CV_DEFAULT(CV_UNDEF_SC_PARAM),
477                    double  param4 CV_DEFAULT(CV_UNDEF_SC_PARAM),
478                    double  param5 CV_DEFAULT(CV_UNDEF_SC_PARAM) );
479
480 /*****************************************************************************************/
481 /************ Epiline functions *******************/
482
483
484
485 typedef struct CvStereoLineCoeff
486 {
487     double Xcoef;
488     double XcoefA;
489     double XcoefB;
490     double XcoefAB;
491
492     double Ycoef;
493     double YcoefA;
494     double YcoefB;
495     double YcoefAB;
496
497     double Zcoef;
498     double ZcoefA;
499     double ZcoefB;
500     double ZcoefAB;
501 }CvStereoLineCoeff;
502
503
504 typedef struct CvCamera
505 {
506     float   imgSize[2]; /* size of the camera view, used during calibration */
507     float   matrix[9]; /* intinsic camera parameters:  [ fx 0 cx; 0 fy cy; 0 0 1 ] */
508     float   distortion[4]; /* distortion coefficients - two coefficients for radial distortion
509                               and another two for tangential: [ k1 k2 p1 p2 ] */
510     float   rotMatr[9];
511     float   transVect[3]; /* rotation matrix and transition vector relatively
512                              to some reference point in the space. */
513 } CvCamera;
514
515 typedef struct CvStereoCamera
516 {
517     CvCamera* camera[2]; /* two individual camera parameters */
518     float fundMatr[9]; /* fundamental matrix */
519
520     /* New part for stereo */
521     CvPoint3D32f epipole[2];
522     CvPoint2D32f quad[2][4]; /* coordinates of destination quadrangle after
523                                 epipolar geometry rectification */
524     double coeffs[2][3][3];/* coefficients for transformation */
525     CvPoint2D32f border[2][4];
526     CvSize warpSize;
527     CvStereoLineCoeff* lineCoeffs;
528     int needSwapCameras;/* flag set to 1 if need to swap cameras for good reconstruction */
529     float rotMatrix[9];
530     float transVector[3];
531 } CvStereoCamera;
532
533
534 typedef struct CvContourOrientation
535 {
536     float egvals[2];
537     float egvects[4];
538
539     float max, min; // minimum and maximum projections
540     int imax, imin;
541 } CvContourOrientation;
542
543 #define CV_CAMERA_TO_WARP 1
544 #define CV_WARP_TO_CAMERA 2
545
546 CVAPI(int) icvConvertWarpCoordinates(double coeffs[3][3],
547                                 CvPoint2D32f* cameraPoint,
548                                 CvPoint2D32f* warpPoint,
549                                 int direction);
550
551 CVAPI(int) icvGetSymPoint3D(  CvPoint3D64f pointCorner,
552                             CvPoint3D64f point1,
553                             CvPoint3D64f point2,
554                             CvPoint3D64f *pointSym2);
555
556 CVAPI(void) icvGetPieceLength3D(CvPoint3D64f point1,CvPoint3D64f point2,double* dist);
557
558 CVAPI(int) icvCompute3DPoint(    double alpha,double betta,
559                             CvStereoLineCoeff* coeffs,
560                             CvPoint3D64f* point);
561
562 CVAPI(int) icvCreateConvertMatrVect( double*     rotMatr1,
563                                 double*     transVect1,
564                                 double*     rotMatr2,
565                                 double*     transVect2,
566                                 double*     convRotMatr,
567                                 double*     convTransVect);
568
569 CVAPI(int) icvConvertPointSystem(CvPoint3D64f  M2,
570                             CvPoint3D64f* M1,
571                             double*     rotMatr,
572                             double*     transVect
573                             );
574
575 CVAPI(int) icvComputeCoeffForStereo(  CvStereoCamera* stereoCamera);
576
577 CVAPI(int) icvGetCrossPieceVector(CvPoint2D32f p1_start,CvPoint2D32f p1_end,CvPoint2D32f v2_start,CvPoint2D32f v2_end,CvPoint2D32f *cross);
578 CVAPI(int) icvGetCrossLineDirect(CvPoint2D32f p1,CvPoint2D32f p2,float a,float b,float c,CvPoint2D32f* cross);
579 CVAPI(float) icvDefinePointPosition(CvPoint2D32f point1,CvPoint2D32f point2,CvPoint2D32f point);
580 CVAPI(int) icvStereoCalibration( int numImages,
581                             int* nums,
582                             CvSize imageSize,
583                             CvPoint2D32f* imagePoints1,
584                             CvPoint2D32f* imagePoints2,
585                             CvPoint3D32f* objectPoints,
586                             CvStereoCamera* stereoparams
587                            );
588
589
590 CVAPI(int) icvComputeRestStereoParams(CvStereoCamera *stereoparams);
591
592 CVAPI(void) cvComputePerspectiveMap( const double coeffs[3][3], CvArr* rectMapX, CvArr* rectMapY );
593
594 CVAPI(int) icvComCoeffForLine(   CvPoint2D64f point1,
595                             CvPoint2D64f point2,
596                             CvPoint2D64f point3,
597                             CvPoint2D64f point4,
598                             double*    camMatr1,
599                             double*    rotMatr1,
600                             double*    transVect1,
601                             double*    camMatr2,
602                             double*    rotMatr2,
603                             double*    transVect2,
604                             CvStereoLineCoeff*    coeffs,
605                             int* needSwapCameras);
606
607 CVAPI(int) icvGetDirectionForPoint(  CvPoint2D64f point,
608                                 double* camMatr,
609                                 CvPoint3D64f* direct);
610
611 CVAPI(int) icvGetCrossLines(CvPoint3D64f point11,CvPoint3D64f point12,
612                        CvPoint3D64f point21,CvPoint3D64f point22,
613                        CvPoint3D64f* midPoint);
614
615 CVAPI(int) icvComputeStereoLineCoeffs(   CvPoint3D64f pointA,
616                                     CvPoint3D64f pointB,
617                                     CvPoint3D64f pointCam1,
618                                     double gamma,
619                                     CvStereoLineCoeff*    coeffs);
620
621 /*CVAPI(int) icvComputeFundMatrEpipoles ( double* camMatr1,
622                                     double*     rotMatr1,
623                                     double*     transVect1,
624                                     double*     camMatr2,
625                                     double*     rotMatr2,
626                                     double*     transVect2,
627                                     CvPoint2D64f* epipole1,
628                                     CvPoint2D64f* epipole2,
629                                     double*     fundMatr);*/
630
631 CVAPI(int) icvGetAngleLine( CvPoint2D64f startPoint, CvSize imageSize,CvPoint2D64f *point1,CvPoint2D64f *point2);
632
633 CVAPI(void) icvGetCoefForPiece(   CvPoint2D64f p_start,CvPoint2D64f p_end,
634                         double *a,double *b,double *c,
635                         int* result);
636
637 /*CVAPI(void) icvGetCommonArea( CvSize imageSize,
638                     CvPoint2D64f epipole1,CvPoint2D64f epipole2,
639                     double* fundMatr,
640                     double* coeff11,double* coeff12,
641                     double* coeff21,double* coeff22,
642                     int* result);*/
643
644 CVAPI(void) icvComputeeInfiniteProject1(double*    rotMatr,
645                                      double*    camMatr1,
646                                      double*    camMatr2,
647                                      CvPoint2D32f point1,
648                                      CvPoint2D32f *point2);
649
650 CVAPI(void) icvComputeeInfiniteProject2(double*    rotMatr,
651                                      double*    camMatr1,
652                                      double*    camMatr2,
653                                      CvPoint2D32f* point1,
654                                      CvPoint2D32f point2);
655
656 CVAPI(void) icvGetCrossDirectDirect(  double* direct1,double* direct2,
657                             CvPoint2D64f *cross,int* result);
658
659 CVAPI(void) icvGetCrossPieceDirect(   CvPoint2D64f p_start,CvPoint2D64f p_end,
660                             double a,double b,double c,
661                             CvPoint2D64f *cross,int* result);
662
663 CVAPI(void) icvGetCrossPiecePiece( CvPoint2D64f p1_start,CvPoint2D64f p1_end,
664                             CvPoint2D64f p2_start,CvPoint2D64f p2_end,
665                             CvPoint2D64f* cross,
666                             int* result);
667
668 CVAPI(void) icvGetPieceLength(CvPoint2D64f point1,CvPoint2D64f point2,double* dist);
669
670 CVAPI(void) icvGetCrossRectDirect(    CvSize imageSize,
671                             double a,double b,double c,
672                             CvPoint2D64f *start,CvPoint2D64f *end,
673                             int* result);
674
675 CVAPI(void) icvProjectPointToImage(   CvPoint3D64f point,
676                             double* camMatr,double* rotMatr,double* transVect,
677                             CvPoint2D64f* projPoint);
678
679 CVAPI(void) icvGetQuadsTransform( CvSize        imageSize,
680                         double*     camMatr1,
681                         double*     rotMatr1,
682                         double*     transVect1,
683                         double*     camMatr2,
684                         double*     rotMatr2,
685                         double*     transVect2,
686                         CvSize*       warpSize,
687                         double quad1[4][2],
688                         double quad2[4][2],
689                         double*     fundMatr,
690                         CvPoint3D64f* epipole1,
691                         CvPoint3D64f* epipole2
692                         );
693
694 CVAPI(void) icvGetQuadsTransformStruct(  CvStereoCamera* stereoCamera);
695
696 CVAPI(void) icvComputeStereoParamsForCameras(CvStereoCamera* stereoCamera);
697
698 CVAPI(void) icvGetCutPiece(   double* areaLineCoef1,double* areaLineCoef2,
699                     CvPoint2D64f epipole,
700                     CvSize imageSize,
701                     CvPoint2D64f* point11,CvPoint2D64f* point12,
702                     CvPoint2D64f* point21,CvPoint2D64f* point22,
703                     int* result);
704
705 CVAPI(void) icvGetMiddleAnglePoint(   CvPoint2D64f basePoint,
706                             CvPoint2D64f point1,CvPoint2D64f point2,
707                             CvPoint2D64f* midPoint);
708
709 CVAPI(void) icvGetNormalDirect(double* direct,CvPoint2D64f point,double* normDirect);
710
711 CVAPI(double) icvGetVect(CvPoint2D64f basePoint,CvPoint2D64f point1,CvPoint2D64f point2);
712
713 CVAPI(void) icvProjectPointToDirect(  CvPoint2D64f point,double* lineCoeff,
714                             CvPoint2D64f* projectPoint);
715
716 CVAPI(void) icvGetDistanceFromPointToDirect( CvPoint2D64f point,double* lineCoef,double*dist);
717
718 CVAPI(IplImage*) icvCreateIsometricImage( IplImage* src, IplImage* dst,
719                               int desired_depth, int desired_num_channels );
720
721 CVAPI(void) cvDeInterlace( const CvArr* frame, CvArr* fieldEven, CvArr* fieldOdd );
722
723 /*CVAPI(int) icvSelectBestRt(           int           numImages,
724                                     int*          numPoints,
725                                     CvSize        imageSize,
726                                     CvPoint2D32f* imagePoints1,
727                                     CvPoint2D32f* imagePoints2,
728                                     CvPoint3D32f* objectPoints,
729
730                                     CvMatr32f     cameraMatrix1,
731                                     CvVect32f     distortion1,
732                                     CvMatr32f     rotMatrs1,
733                                     CvVect32f     transVects1,
734
735                                     CvMatr32f     cameraMatrix2,
736                                     CvVect32f     distortion2,
737                                     CvMatr32f     rotMatrs2,
738                                     CvVect32f     transVects2,
739
740                                     CvMatr32f     bestRotMatr,
741                                     CvVect32f     bestTransVect
742                                     );*/
743
744
745 /****************************************************************************************\
746 *                                     Contour Tree                                       *
747 \****************************************************************************************/
748
749 /* Contour tree header */
750 typedef struct CvContourTree
751 {
752     CV_SEQUENCE_FIELDS()
753     CvPoint p1;            /* the first point of the binary tree root segment */
754     CvPoint p2;            /* the last point of the binary tree root segment */
755 } CvContourTree;
756
757 /* Builds hierarhical representation of a contour */
758 CVAPI(CvContourTree*)  cvCreateContourTree( const CvSeq* contour,
759                                             CvMemStorage* storage,
760                                             double threshold );
761
762 /* Reconstruct (completelly or partially) contour a from contour tree */
763 CVAPI(CvSeq*)  cvContourFromContourTree( const CvContourTree* tree,
764                                          CvMemStorage* storage,
765                                          CvTermCriteria criteria );
766
767 /* Compares two contour trees */
768 enum { CV_CONTOUR_TREES_MATCH_I1 = 1 };
769
770 CVAPI(double)  cvMatchContourTrees( const CvContourTree* tree1,
771                                     const CvContourTree* tree2,
772                                     int method, double threshold );
773
774 /****************************************************************************************\
775 *                                   Contour Morphing                                     *
776 \****************************************************************************************/
777
778 /* finds correspondence between two contours */
779 CvSeq* cvCalcContoursCorrespondence( const CvSeq* contour1,
780                                      const CvSeq* contour2,
781                                      CvMemStorage* storage);
782
783 /* morphs contours using the pre-calculated correspondence:
784    alpha=0 ~ contour1, alpha=1 ~ contour2 */
785 CvSeq* cvMorphContours( const CvSeq* contour1, const CvSeq* contour2,
786                         CvSeq* corr, double alpha,
787                         CvMemStorage* storage );
788
789
790 /****************************************************************************************\
791 *                                   Active Contours                                      *
792 \****************************************************************************************/
793
794 #define  CV_VALUE  1
795 #define  CV_ARRAY  2
796 /* Updates active contour in order to minimize its cummulative
797    (internal and external) energy. */
798 CVAPI(void)  cvSnakeImage( const IplImage* image, CvPoint* points,
799                            int  length, float* alpha,
800                            float* beta, float* gamma,
801                            int coeff_usage, CvSize  win,
802                            CvTermCriteria criteria, int calc_gradient CV_DEFAULT(1));
803
804 /****************************************************************************************\
805 *                                    Texture Descriptors                                 *
806 \****************************************************************************************/
807
808 #define CV_GLCM_OPTIMIZATION_NONE                   -2
809 #define CV_GLCM_OPTIMIZATION_LUT                    -1
810 #define CV_GLCM_OPTIMIZATION_HISTOGRAM              0
811
812 #define CV_GLCMDESC_OPTIMIZATION_ALLOWDOUBLENEST    10
813 #define CV_GLCMDESC_OPTIMIZATION_ALLOWTRIPLENEST    11
814 #define CV_GLCMDESC_OPTIMIZATION_HISTOGRAM          4
815
816 #define CV_GLCMDESC_ENTROPY                         0
817 #define CV_GLCMDESC_ENERGY                          1
818 #define CV_GLCMDESC_HOMOGENITY                      2
819 #define CV_GLCMDESC_CONTRAST                        3
820 #define CV_GLCMDESC_CLUSTERTENDENCY                 4
821 #define CV_GLCMDESC_CLUSTERSHADE                    5
822 #define CV_GLCMDESC_CORRELATION                     6
823 #define CV_GLCMDESC_CORRELATIONINFO1                7
824 #define CV_GLCMDESC_CORRELATIONINFO2                8
825 #define CV_GLCMDESC_MAXIMUMPROBABILITY              9
826
827 #define CV_GLCM_ALL                                 0
828 #define CV_GLCM_GLCM                                1
829 #define CV_GLCM_DESC                                2
830
831 typedef struct CvGLCM CvGLCM;
832
833 CVAPI(CvGLCM*) cvCreateGLCM( const IplImage* srcImage,
834                                 int stepMagnitude,
835                                 const int* stepDirections CV_DEFAULT(0),
836                                 int numStepDirections CV_DEFAULT(0),
837                                 int optimizationType CV_DEFAULT(CV_GLCM_OPTIMIZATION_NONE));
838
839 CVAPI(void) cvReleaseGLCM( CvGLCM** GLCM, int flag CV_DEFAULT(CV_GLCM_ALL));
840
841 CVAPI(void) cvCreateGLCMDescriptors( CvGLCM* destGLCM,
842                                         int descriptorOptimizationType
843                                         CV_DEFAULT(CV_GLCMDESC_OPTIMIZATION_ALLOWDOUBLENEST));
844
845 CVAPI(double) cvGetGLCMDescriptor( CvGLCM* GLCM, int step, int descriptor );
846
847 CVAPI(void) cvGetGLCMDescriptorStatistics( CvGLCM* GLCM, int descriptor,
848                                               double* average, double* standardDeviation );
849
850 CVAPI(IplImage*) cvCreateGLCMImage( CvGLCM* GLCM, int step );
851
852 /****************************************************************************************\
853 *                                  Face eyes&mouth tracking                              *
854 \****************************************************************************************/
855
856
857 typedef struct CvFaceTracker CvFaceTracker;
858
859 #define CV_NUM_FACE_ELEMENTS    3
860 enum CV_FACE_ELEMENTS
861 {
862     CV_FACE_MOUTH = 0,
863     CV_FACE_LEFT_EYE = 1,
864     CV_FACE_RIGHT_EYE = 2
865 };
866
867 CVAPI(CvFaceTracker*) cvInitFaceTracker(CvFaceTracker* pFaceTracking, const IplImage* imgGray,
868                                                 CvRect* pRects, int nRects);
869 CVAPI(int) cvTrackFace( CvFaceTracker* pFaceTracker, IplImage* imgGray,
870                               CvRect* pRects, int nRects,
871                               CvPoint* ptRotate, double* dbAngleRotate);
872 CVAPI(void) cvReleaseFaceTracker(CvFaceTracker** ppFaceTracker);
873
874
875 typedef struct CvFace
876 {
877     CvRect MouthRect;
878     CvRect LeftEyeRect;
879     CvRect RightEyeRect;
880 } CvFaceData;
881
882 CvSeq * cvFindFace(IplImage * Image,CvMemStorage* storage);
883 CvSeq * cvPostBoostingFindFace(IplImage * Image,CvMemStorage* storage);
884
885
886 /****************************************************************************************\
887 *                                         3D Tracker                                     *
888 \****************************************************************************************/
889
890 typedef unsigned char CvBool;
891
892 typedef struct Cv3dTracker2dTrackedObject
893 {
894     int id;
895     CvPoint2D32f p; // pgruebele: So we do not loose precision, this needs to be float
896 } Cv3dTracker2dTrackedObject;
897
898 CV_INLINE Cv3dTracker2dTrackedObject cv3dTracker2dTrackedObject(int id, CvPoint2D32f p)
899 {
900     Cv3dTracker2dTrackedObject r;
901     r.id = id;
902     r.p = p;
903     return r;
904 }
905
906 typedef struct Cv3dTrackerTrackedObject
907 {
908     int id;
909     CvPoint3D32f p;             // location of the tracked object
910 } Cv3dTrackerTrackedObject;
911
912 CV_INLINE Cv3dTrackerTrackedObject cv3dTrackerTrackedObject(int id, CvPoint3D32f p)
913 {
914     Cv3dTrackerTrackedObject r;
915     r.id = id;
916     r.p = p;
917     return r;
918 }
919
920 typedef struct Cv3dTrackerCameraInfo
921 {
922     CvBool valid;
923     float mat[4][4];              /* maps camera coordinates to world coordinates */
924     CvPoint2D32f principal_point; /* copied from intrinsics so this structure */
925                                   /* has all the info we need */
926 } Cv3dTrackerCameraInfo;
927
928 typedef struct Cv3dTrackerCameraIntrinsics
929 {
930     CvPoint2D32f principal_point;
931     float focal_length[2];
932     float distortion[4];
933 } Cv3dTrackerCameraIntrinsics;
934
935 CVAPI(CvBool) cv3dTrackerCalibrateCameras(int num_cameras,
936                      const Cv3dTrackerCameraIntrinsics camera_intrinsics[], /* size is num_cameras */
937                      CvSize etalon_size,
938                      float square_size,
939                      IplImage *samples[],                                   /* size is num_cameras */
940                      Cv3dTrackerCameraInfo camera_info[]);                  /* size is num_cameras */
941
942 CVAPI(int)  cv3dTrackerLocateObjects(int num_cameras, int num_objects,
943                    const Cv3dTrackerCameraInfo camera_info[],        /* size is num_cameras */
944                    const Cv3dTracker2dTrackedObject tracking_info[], /* size is num_objects*num_cameras */
945                    Cv3dTrackerTrackedObject tracked_objects[]);      /* size is num_objects */
946 /****************************************************************************************
947  tracking_info is a rectangular array; one row per camera, num_objects elements per row.
948  The id field of any unused slots must be -1. Ids need not be ordered or consecutive. On
949  completion, the return value is the number of objects located; i.e., the number of objects
950  visible by more than one camera. The id field of any unused slots in tracked objects is
951  set to -1.
952 ****************************************************************************************/
953
954
955 /****************************************************************************************\
956 *                           Skeletons and Linear-Contour Models                          *
957 \****************************************************************************************/
958
959 typedef enum CvLeeParameters
960 {
961     CV_LEE_INT = 0,
962     CV_LEE_FLOAT = 1,
963     CV_LEE_DOUBLE = 2,
964     CV_LEE_AUTO = -1,
965     CV_LEE_ERODE = 0,
966     CV_LEE_ZOOM = 1,
967     CV_LEE_NON = 2
968 } CvLeeParameters;
969
970 #define CV_NEXT_VORONOISITE2D( SITE ) ((SITE)->edge[0]->site[((SITE)->edge[0]->site[0] == (SITE))])
971 #define CV_PREV_VORONOISITE2D( SITE ) ((SITE)->edge[1]->site[((SITE)->edge[1]->site[0] == (SITE))])
972 #define CV_FIRST_VORONOIEDGE2D( SITE ) ((SITE)->edge[0])
973 #define CV_LAST_VORONOIEDGE2D( SITE ) ((SITE)->edge[1])
974 #define CV_NEXT_VORONOIEDGE2D( EDGE, SITE ) ((EDGE)->next[(EDGE)->site[0] != (SITE)])
975 #define CV_PREV_VORONOIEDGE2D( EDGE, SITE ) ((EDGE)->next[2 + ((EDGE)->site[0] != (SITE))])
976 #define CV_VORONOIEDGE2D_BEGINNODE( EDGE, SITE ) ((EDGE)->node[((EDGE)->site[0] != (SITE))])
977 #define CV_VORONOIEDGE2D_ENDNODE( EDGE, SITE ) ((EDGE)->node[((EDGE)->site[0] == (SITE))])
978 #define CV_TWIN_VORONOISITE2D( SITE, EDGE ) ( (EDGE)->site[((EDGE)->site[0] == (SITE))])
979
980 #define CV_VORONOISITE2D_FIELDS()    \
981     struct CvVoronoiNode2D *node[2]; \
982     struct CvVoronoiEdge2D *edge[2];
983
984 typedef struct CvVoronoiSite2D
985 {
986     CV_VORONOISITE2D_FIELDS()
987     struct CvVoronoiSite2D *next[2];
988 } CvVoronoiSite2D;
989
990 #define CV_VORONOIEDGE2D_FIELDS()    \
991     struct CvVoronoiNode2D *node[2]; \
992     struct CvVoronoiSite2D *site[2]; \
993     struct CvVoronoiEdge2D *next[4];
994
995 typedef struct CvVoronoiEdge2D
996 {
997     CV_VORONOIEDGE2D_FIELDS()
998 } CvVoronoiEdge2D;
999
1000 #define CV_VORONOINODE2D_FIELDS()       \
1001     CV_SET_ELEM_FIELDS(CvVoronoiNode2D) \
1002     CvPoint2D32f pt;                    \
1003     float radius;
1004
1005 typedef struct CvVoronoiNode2D
1006 {
1007     CV_VORONOINODE2D_FIELDS()
1008 } CvVoronoiNode2D;
1009
1010 #define CV_VORONOIDIAGRAM2D_FIELDS() \
1011     CV_GRAPH_FIELDS()                \
1012     CvSet *sites;
1013
1014 typedef struct CvVoronoiDiagram2D
1015 {
1016     CV_VORONOIDIAGRAM2D_FIELDS()
1017 } CvVoronoiDiagram2D;
1018
1019 /* Computes Voronoi Diagram for given polygons with holes */
1020 CVAPI(int)  cvVoronoiDiagramFromContour(CvSeq* ContourSeq,
1021                                            CvVoronoiDiagram2D** VoronoiDiagram,
1022                                            CvMemStorage* VoronoiStorage,
1023                                            CvLeeParameters contour_type CV_DEFAULT(CV_LEE_INT),
1024                                            int contour_orientation CV_DEFAULT(-1),
1025                                            int attempt_number CV_DEFAULT(10));
1026
1027 /* Computes Voronoi Diagram for domains in given image */
1028 CVAPI(int)  cvVoronoiDiagramFromImage(IplImage* pImage,
1029                                          CvSeq** ContourSeq,
1030                                          CvVoronoiDiagram2D** VoronoiDiagram,
1031                                          CvMemStorage* VoronoiStorage,
1032                                          CvLeeParameters regularization_method CV_DEFAULT(CV_LEE_NON),
1033                                          float approx_precision CV_DEFAULT(CV_LEE_AUTO));
1034
1035 /* Deallocates the storage */
1036 CVAPI(void) cvReleaseVoronoiStorage(CvVoronoiDiagram2D* VoronoiDiagram,
1037                                           CvMemStorage** pVoronoiStorage);
1038
1039 /*********************** Linear-Contour Model ****************************/
1040
1041 struct CvLCMEdge;
1042 struct CvLCMNode;
1043
1044 typedef struct CvLCMEdge
1045 {
1046     CV_GRAPH_EDGE_FIELDS()
1047     CvSeq* chain;
1048     float width;
1049     int index1;
1050     int index2;
1051 } CvLCMEdge;
1052
1053 typedef struct CvLCMNode
1054 {
1055     CV_GRAPH_VERTEX_FIELDS()
1056     CvContour* contour;
1057 } CvLCMNode;
1058
1059
1060 /* Computes hybrid model from Voronoi Diagram */
1061 CVAPI(CvGraph*) cvLinearContorModelFromVoronoiDiagram(CvVoronoiDiagram2D* VoronoiDiagram,
1062                                                          float maxWidth);
1063
1064 /* Releases hybrid model storage */
1065 CVAPI(int) cvReleaseLinearContorModelStorage(CvGraph** Graph);
1066
1067
1068 /* two stereo-related functions */
1069
1070 CVAPI(void) cvInitPerspectiveTransform( CvSize size, const CvPoint2D32f vertex[4], double matrix[3][3],
1071                                               CvArr* rectMap );
1072
1073 /*CVAPI(void) cvInitStereoRectification( CvStereoCamera* params,
1074                                              CvArr* rectMap1, CvArr* rectMap2,
1075                                              int do_undistortion );*/
1076
1077 /*************************** View Morphing Functions ************************/
1078
1079 typedef struct CvMatrix3
1080 {
1081     float m[3][3];
1082 } CvMatrix3;
1083
1084 /* The order of the function corresponds to the order they should appear in
1085    the view morphing pipeline */
1086
1087 /* Finds ending points of scanlines on left and right images of stereo-pair */
1088 CVAPI(void)  cvMakeScanlines( const CvMatrix3* matrix, CvSize  img_size,
1089                               int*  scanlines1, int*  scanlines2,
1090                               int*  lengths1, int*  lengths2,
1091                               int*  line_count );
1092
1093 /* Grab pixel values from scanlines and stores them sequentially
1094    (some sort of perspective image transform) */
1095 CVAPI(void)  cvPreWarpImage( int       line_count,
1096                              IplImage* img,
1097                              uchar*    dst,
1098                              int*      dst_nums,
1099                              int*      scanlines);
1100
1101 /* Approximate each grabbed scanline by a sequence of runs
1102    (lossy run-length compression) */
1103 CVAPI(void)  cvFindRuns( int    line_count,
1104                          uchar* prewarp1,
1105                          uchar* prewarp2,
1106                          int*   line_lengths1,
1107                          int*   line_lengths2,
1108                          int*   runs1,
1109                          int*   runs2,
1110                          int*   num_runs1,
1111                          int*   num_runs2);
1112
1113 /* Compares two sets of compressed scanlines */
1114 CVAPI(void)  cvDynamicCorrespondMulti( int  line_count,
1115                                        int* first,
1116                                        int* first_runs,
1117                                        int* second,
1118                                        int* second_runs,
1119                                        int* first_corr,
1120                                        int* second_corr);
1121
1122 /* Finds scanline ending coordinates for some intermediate "virtual" camera position */
1123 CVAPI(void)  cvMakeAlphaScanlines( int*  scanlines1,
1124                                    int*  scanlines2,
1125                                    int*  scanlinesA,
1126                                    int*  lengths,
1127                                    int   line_count,
1128                                    float alpha);
1129
1130 /* Blends data of the left and right image scanlines to get
1131    pixel values of "virtual" image scanlines */
1132 CVAPI(void)  cvMorphEpilinesMulti( int    line_count,
1133                                    uchar* first_pix,
1134                                    int*   first_num,
1135                                    uchar* second_pix,
1136                                    int*   second_num,
1137                                    uchar* dst_pix,
1138                                    int*   dst_num,
1139                                    float  alpha,
1140                                    int*   first,
1141                                    int*   first_runs,
1142                                    int*   second,
1143                                    int*   second_runs,
1144                                    int*   first_corr,
1145                                    int*   second_corr);
1146
1147 /* Does reverse warping of the morphing result to make
1148    it fill the destination image rectangle */
1149 CVAPI(void)  cvPostWarpImage( int       line_count,
1150                               uchar*    src,
1151                               int*      src_nums,
1152                               IplImage* img,
1153                               int*      scanlines);
1154
1155 /* Deletes Moire (missed pixels that appear due to discretization) */
1156 CVAPI(void)  cvDeleteMoire( IplImage*  img );
1157
1158
1159 typedef struct CvConDensation
1160 {
1161     int MP;
1162     int DP;
1163     float* DynamMatr;       /* Matrix of the linear Dynamics system  */
1164     float* State;           /* Vector of State                       */
1165     int SamplesNum;         /* Number of the Samples                 */
1166     float** flSamples;      /* arr of the Sample Vectors             */
1167     float** flNewSamples;   /* temporary array of the Sample Vectors */
1168     float* flConfidence;    /* Confidence for each Sample            */
1169     float* flCumulative;    /* Cumulative confidence                 */
1170     float* Temp;            /* Temporary vector                      */
1171     float* RandomSample;    /* RandomVector to update sample set     */
1172     struct CvRandState* RandS; /* Array of structures to generate random vectors */
1173 } CvConDensation;
1174
1175 /* Creates ConDensation filter state */
1176 CVAPI(CvConDensation*)  cvCreateConDensation( int dynam_params,
1177                                              int measure_params,
1178                                              int sample_count );
1179
1180 /* Releases ConDensation filter state */
1181 CVAPI(void)  cvReleaseConDensation( CvConDensation** condens );
1182
1183 /* Updates ConDensation filter by time (predict future state of the system) */
1184 CVAPI(void)  cvConDensUpdateByTime( CvConDensation* condens);
1185
1186 /* Initializes ConDensation filter samples  */
1187 CVAPI(void)  cvConDensInitSampleSet( CvConDensation* condens, CvMat* lower_bound, CvMat* upper_bound );
1188
1189 CV_INLINE int iplWidth( const IplImage* img )
1190 {
1191     return !img ? 0 : !img->roi ? img->width : img->roi->width;
1192 }
1193
1194 CV_INLINE int iplHeight( const IplImage* img )
1195 {
1196     return !img ? 0 : !img->roi ? img->height : img->roi->height;
1197 }
1198
1199 #ifdef __cplusplus
1200 }
1201 #endif
1202
1203 #ifdef __cplusplus
1204
1205 /****************************************************************************************\
1206 *                                   Calibration engine                                   *
1207 \****************************************************************************************/
1208
1209 typedef enum CvCalibEtalonType
1210 {
1211     CV_CALIB_ETALON_USER = -1,
1212     CV_CALIB_ETALON_CHESSBOARD = 0,
1213     CV_CALIB_ETALON_CHECKERBOARD = CV_CALIB_ETALON_CHESSBOARD
1214 }
1215 CvCalibEtalonType;
1216
1217 class CV_EXPORTS CvCalibFilter
1218 {
1219 public:
1220     /* Constructor & destructor */
1221     CvCalibFilter();
1222     virtual ~CvCalibFilter();
1223
1224     /* Sets etalon type - one for all cameras.
1225        etalonParams is used in case of pre-defined etalons (such as chessboard).
1226        Number of elements in etalonParams is determined by etalonType.
1227        E.g., if etalon type is CV_ETALON_TYPE_CHESSBOARD then:
1228          etalonParams[0] is number of squares per one side of etalon
1229          etalonParams[1] is number of squares per another side of etalon
1230          etalonParams[2] is linear size of squares in the board in arbitrary units.
1231        pointCount & points are used in case of
1232        CV_CALIB_ETALON_USER (user-defined) etalon. */
1233     virtual bool
1234         SetEtalon( CvCalibEtalonType etalonType, double* etalonParams,
1235                    int pointCount = 0, CvPoint2D32f* points = 0 );
1236
1237     /* Retrieves etalon parameters/or and points */
1238     virtual CvCalibEtalonType
1239         GetEtalon( int* paramCount = 0, const double** etalonParams = 0,
1240                    int* pointCount = 0, const CvPoint2D32f** etalonPoints = 0 ) const;
1241
1242     /* Sets number of cameras calibrated simultaneously. It is equal to 1 initially */
1243     virtual void SetCameraCount( int cameraCount );
1244
1245     /* Retrieves number of cameras */
1246     int GetCameraCount() const { return cameraCount; }
1247
1248     /* Starts cameras calibration */
1249     virtual bool SetFrames( int totalFrames );
1250
1251     /* Stops cameras calibration */
1252     virtual void Stop( bool calibrate = false );
1253
1254     /* Retrieves number of cameras */
1255     bool IsCalibrated() const { return isCalibrated; }
1256
1257     /* Feeds another serie of snapshots (one per each camera) to filter.
1258        Etalon points on these images are found automatically.
1259        If the function can't locate points, it returns false */
1260     virtual bool FindEtalon( IplImage** imgs );
1261
1262     /* The same but takes matrices */
1263     virtual bool FindEtalon( CvMat** imgs );
1264
1265     /* Lower-level function for feeding filter with already found etalon points.
1266        Array of point arrays for each camera is passed. */
1267     virtual bool Push( const CvPoint2D32f** points = 0 );
1268
1269     /* Returns total number of accepted frames and, optionally,
1270        total number of frames to collect */
1271     virtual int GetFrameCount( int* framesTotal = 0 ) const;
1272
1273     /* Retrieves camera parameters for specified camera.
1274        If camera is not calibrated the function returns 0 */
1275     virtual const CvCamera* GetCameraParams( int idx = 0 ) const;
1276
1277     virtual const CvStereoCamera* GetStereoParams() const;
1278
1279     /* Sets camera parameters for all cameras */
1280     virtual bool SetCameraParams( CvCamera* params );
1281
1282     /* Saves all camera parameters to file */
1283     virtual bool SaveCameraParams( const char* filename );
1284
1285     /* Loads all camera parameters from file */
1286     virtual bool LoadCameraParams( const char* filename );
1287
1288     /* Undistorts images using camera parameters. Some of src pointers can be NULL. */
1289     virtual bool Undistort( IplImage** src, IplImage** dst );
1290
1291     /* Undistorts images using camera parameters. Some of src pointers can be NULL. */
1292     virtual bool Undistort( CvMat** src, CvMat** dst );
1293
1294     /* Returns array of etalon points detected/partally detected
1295        on the latest frame for idx-th camera */
1296     virtual bool GetLatestPoints( int idx, CvPoint2D32f** pts,
1297                                                   int* count, bool* found );
1298
1299     /* Draw the latest detected/partially detected etalon */
1300     virtual void DrawPoints( IplImage** dst );
1301
1302     /* Draw the latest detected/partially detected etalon */
1303     virtual void DrawPoints( CvMat** dst );
1304
1305     virtual bool Rectify( IplImage** srcarr, IplImage** dstarr );
1306     virtual bool Rectify( CvMat** srcarr, CvMat** dstarr );
1307
1308 protected:
1309
1310     enum { MAX_CAMERAS = 3 };
1311
1312     /* etalon data */
1313     CvCalibEtalonType  etalonType;
1314     int     etalonParamCount;
1315     double* etalonParams;
1316     int     etalonPointCount;
1317     CvPoint2D32f* etalonPoints;
1318     CvSize  imgSize;
1319     CvMat*  grayImg;
1320     CvMat*  tempImg;
1321     CvMemStorage* storage;
1322
1323     /* camera data */
1324     int     cameraCount;
1325     CvCamera cameraParams[MAX_CAMERAS];
1326     CvStereoCamera stereo;
1327     CvPoint2D32f* points[MAX_CAMERAS];
1328     CvMat*  undistMap[MAX_CAMERAS][2];
1329     CvMat*  undistImg;
1330     int     latestCounts[MAX_CAMERAS];
1331     CvPoint2D32f* latestPoints[MAX_CAMERAS];
1332     CvMat*  rectMap[MAX_CAMERAS][2];
1333
1334     /* Added by Valery */
1335     //CvStereoCamera stereoParams;
1336
1337     int     maxPoints;
1338     int     framesTotal;
1339     int     framesAccepted;
1340     bool    isCalibrated;
1341 };
1342
1343 #include <iosfwd>
1344 #include <limits>
1345
1346 class CV_EXPORTS CvImage
1347 {
1348 public:
1349     CvImage() : image(0), refcount(0) {}
1350     CvImage( CvSize _size, int _depth, int _channels )
1351     {
1352         image = cvCreateImage( _size, _depth, _channels );
1353         refcount = image ? new int(1) : 0;
1354     }
1355
1356     CvImage( IplImage* img ) : image(img)
1357     {
1358         refcount = image ? new int(1) : 0;
1359     }
1360
1361     CvImage( const CvImage& img ) : image(img.image), refcount(img.refcount)
1362     {
1363         if( refcount ) ++(*refcount);
1364     }
1365
1366     CvImage( const char* filename, const char* imgname=0, int color=-1 ) : image(0), refcount(0)
1367     { load( filename, imgname, color ); }
1368
1369     CvImage( CvFileStorage* fs, const char* mapname, const char* imgname ) : image(0), refcount(0)
1370     { read( fs, mapname, imgname ); }
1371
1372     CvImage( CvFileStorage* fs, const char* seqname, int idx ) : image(0), refcount(0)
1373     { read( fs, seqname, idx ); }
1374
1375     ~CvImage()
1376     {
1377         if( refcount && !(--*refcount) )
1378         {
1379             cvReleaseImage( &image );
1380             delete refcount;
1381         }
1382     }
1383
1384     CvImage clone() { return CvImage(image ? cvCloneImage(image) : 0); }
1385
1386     void create( CvSize _size, int _depth, int _channels )
1387     {
1388         if( !image || !refcount ||
1389            image->width != _size.width || image->height != _size.height ||
1390            image->depth != _depth || image->nChannels != _channels )
1391             attach( cvCreateImage( _size, _depth, _channels ));
1392     }
1393
1394     void release() { detach(); }
1395     void clear() { detach(); }
1396
1397     void attach( IplImage* img, bool use_refcount=true )
1398     {
1399         if( refcount && --*refcount == 0 )
1400         {
1401             cvReleaseImage( &image );
1402             delete refcount;
1403         }
1404         image = img;
1405         refcount = use_refcount && image ? new int(1) : 0;
1406     }
1407
1408     void detach()
1409     {
1410         if( refcount && --*refcount == 0 )
1411         {
1412             cvReleaseImage( &image );
1413             delete refcount;
1414         }
1415         image = 0;
1416         refcount = 0;
1417     }
1418
1419     bool load( const char* filename, const char* imgname=0, int color=-1 );
1420     bool read( CvFileStorage* fs, const char* mapname, const char* imgname );
1421     bool read( CvFileStorage* fs, const char* seqname, int idx );
1422     void save( const char* filename, const char* imgname, const int* params=0 );
1423     void write( CvFileStorage* fs, const char* imgname );
1424
1425     void show( const char* window_name );
1426     bool is_valid() { return image != 0; }
1427
1428     int width() const { return image ? image->width : 0; }
1429     int height() const { return image ? image->height : 0; }
1430
1431     CvSize size() const { return image ? cvSize(image->width, image->height) : cvSize(0,0); }
1432
1433     CvSize roi_size() const
1434     {
1435         return !image ? cvSize(0,0) :
1436         !image->roi ? cvSize(image->width,image->height) :
1437         cvSize(image->roi->width, image->roi->height);
1438     }
1439
1440     CvRect roi() const
1441     {
1442         return !image ? cvRect(0,0,0,0) :
1443         !image->roi ? cvRect(0,0,image->width,image->height) :
1444         cvRect(image->roi->xOffset,image->roi->yOffset,
1445                image->roi->width,image->roi->height);
1446     }
1447
1448     int coi() const { return !image || !image->roi ? 0 : image->roi->coi; }
1449
1450     void set_roi(CvRect _roi) { cvSetImageROI(image,_roi); }
1451     void reset_roi() { cvResetImageROI(image); }
1452     void set_coi(int _coi) { cvSetImageCOI(image,_coi); }
1453     int depth() const { return image ? image->depth : 0; }
1454     int channels() const { return image ? image->nChannels : 0; }
1455     int pix_size() const { return image ? ((image->depth & 255)>>3)*image->nChannels : 0; }
1456
1457     uchar* data() { return image ? (uchar*)image->imageData : 0; }
1458     const uchar* data() const { return image ? (const uchar*)image->imageData : 0; }
1459     int step() const { return image ? image->widthStep : 0; }
1460     int origin() const { return image ? image->origin : 0; }
1461
1462     uchar* roi_row(int y)
1463     {
1464         assert(0<=y);
1465         assert(!image ?
1466                1 : image->roi ?
1467                y<image->roi->height : y<image->height);
1468
1469         return !image ? 0 :
1470         !image->roi ?
1471         (uchar*)(image->imageData + y*image->widthStep) :
1472         (uchar*)(image->imageData + (y+image->roi->yOffset)*image->widthStep +
1473                  image->roi->xOffset*((image->depth & 255)>>3)*image->nChannels);
1474     }
1475
1476     const uchar* roi_row(int y) const
1477     {
1478         assert(0<=y);
1479         assert(!image ?
1480                1 : image->roi ?
1481                y<image->roi->height : y<image->height);
1482
1483         return !image ? 0 :
1484         !image->roi ?
1485         (const uchar*)(image->imageData + y*image->widthStep) :
1486         (const uchar*)(image->imageData + (y+image->roi->yOffset)*image->widthStep +
1487                        image->roi->xOffset*((image->depth & 255)>>3)*image->nChannels);
1488     }
1489
1490     operator const IplImage* () const { return image; }
1491     operator IplImage* () { return image; }
1492
1493     CvImage& operator = (const CvImage& img)
1494     {
1495         if( img.refcount )
1496             ++*img.refcount;
1497         if( refcount && !(--*refcount) )
1498             cvReleaseImage( &image );
1499         image=img.image;
1500         refcount=img.refcount;
1501         return *this;
1502     }
1503
1504 protected:
1505     IplImage* image;
1506     int* refcount;
1507 };
1508
1509
1510 class CV_EXPORTS CvMatrix
1511 {
1512 public:
1513     CvMatrix() : matrix(0) {}
1514     CvMatrix( int _rows, int _cols, int _type )
1515     { matrix = cvCreateMat( _rows, _cols, _type ); }
1516
1517     CvMatrix( int _rows, int _cols, int _type, CvMat* hdr,
1518              void* _data=0, int _step=CV_AUTOSTEP )
1519     { matrix = cvInitMatHeader( hdr, _rows, _cols, _type, _data, _step ); }
1520
1521     CvMatrix( int rows, int cols, int type, CvMemStorage* storage, bool alloc_data=true );
1522
1523     CvMatrix( int _rows, int _cols, int _type, void* _data, int _step=CV_AUTOSTEP )
1524     { matrix = cvCreateMatHeader( _rows, _cols, _type );
1525         cvSetData( matrix, _data, _step ); }
1526
1527     CvMatrix( CvMat* m )
1528     { matrix = m; }
1529
1530     CvMatrix( const CvMatrix& m )
1531     {
1532         matrix = m.matrix;
1533         addref();
1534     }
1535
1536     CvMatrix( const char* filename, const char* matname=0, int color=-1 ) : matrix(0)
1537     {  load( filename, matname, color ); }
1538
1539     CvMatrix( CvFileStorage* fs, const char* mapname, const char* matname ) : matrix(0)
1540     {  read( fs, mapname, matname ); }
1541
1542     CvMatrix( CvFileStorage* fs, const char* seqname, int idx ) : matrix(0)
1543     {  read( fs, seqname, idx ); }
1544
1545     ~CvMatrix()
1546     {
1547         release();
1548     }
1549
1550     CvMatrix clone() { return CvMatrix(matrix ? cvCloneMat(matrix) : 0); }
1551
1552     void set( CvMat* m, bool add_ref )
1553     {
1554         release();
1555         matrix = m;
1556         if( add_ref )
1557             addref();
1558     }
1559
1560     void create( int _rows, int _cols, int _type )
1561     {
1562         if( !matrix || !matrix->refcount ||
1563            matrix->rows != _rows || matrix->cols != _cols ||
1564            CV_MAT_TYPE(matrix->type) != _type )
1565             set( cvCreateMat( _rows, _cols, _type ), false );
1566     }
1567
1568     void addref() const
1569     {
1570         if( matrix )
1571         {
1572             if( matrix->hdr_refcount )
1573                 ++matrix->hdr_refcount;
1574             else if( matrix->refcount )
1575                 ++*matrix->refcount;
1576         }
1577     }
1578
1579     void release()
1580     {
1581         if( matrix )
1582         {
1583             if( matrix->hdr_refcount )
1584             {
1585                 if( --matrix->hdr_refcount == 0 )
1586                     cvReleaseMat( &matrix );
1587             }
1588             else if( matrix->refcount )
1589             {
1590                 if( --*matrix->refcount == 0 )
1591                     cvFree( &matrix->refcount );
1592             }
1593             matrix = 0;
1594         }
1595     }
1596
1597     void clear()
1598     {
1599         release();
1600     }
1601
1602     bool load( const char* filename, const char* matname=0, int color=-1 );
1603     bool read( CvFileStorage* fs, const char* mapname, const char* matname );
1604     bool read( CvFileStorage* fs, const char* seqname, int idx );
1605     void save( const char* filename, const char* matname, const int* params=0 );
1606     void write( CvFileStorage* fs, const char* matname );
1607
1608     void show( const char* window_name );
1609
1610     bool is_valid() { return matrix != 0; }
1611
1612     int rows() const { return matrix ? matrix->rows : 0; }
1613     int cols() const { return matrix ? matrix->cols : 0; }
1614
1615     CvSize size() const
1616     {
1617         return !matrix ? cvSize(0,0) : cvSize(matrix->rows,matrix->cols);
1618     }
1619
1620     int type() const { return matrix ? CV_MAT_TYPE(matrix->type) : 0; }
1621     int depth() const { return matrix ? CV_MAT_DEPTH(matrix->type) : 0; }
1622     int channels() const { return matrix ? CV_MAT_CN(matrix->type) : 0; }
1623     int pix_size() const { return matrix ? CV_ELEM_SIZE(matrix->type) : 0; }
1624
1625     uchar* data() { return matrix ? matrix->data.ptr : 0; }
1626     const uchar* data() const { return matrix ? matrix->data.ptr : 0; }
1627     int step() const { return matrix ? matrix->step : 0; }
1628
1629     void set_data( void* _data, int _step=CV_AUTOSTEP )
1630     { cvSetData( matrix, _data, _step ); }
1631
1632     uchar* row(int i) { return !matrix ? 0 : matrix->data.ptr + i*matrix->step; }
1633     const uchar* row(int i) const
1634     { return !matrix ? 0 : matrix->data.ptr + i*matrix->step; }
1635
1636     operator const CvMat* () const { return matrix; }
1637     operator CvMat* () { return matrix; }
1638
1639     CvMatrix& operator = (const CvMatrix& _m)
1640     {
1641         _m.addref();
1642         release();
1643         matrix = _m.matrix;
1644         return *this;
1645     }
1646
1647 protected:
1648     CvMat* matrix;
1649 };
1650
1651 /****************************************************************************************\
1652  *                                       CamShiftTracker                                  *
1653  \****************************************************************************************/
1654
1655 class CV_EXPORTS CvCamShiftTracker
1656 {
1657 public:
1658
1659     CvCamShiftTracker();
1660     virtual ~CvCamShiftTracker();
1661
1662     /**** Characteristics of the object that are calculated by track_object method *****/
1663     float   get_orientation() const // orientation of the object in degrees
1664     { return m_box.angle; }
1665     float   get_length() const // the larger linear size of the object
1666     { return m_box.size.height; }
1667     float   get_width() const // the smaller linear size of the object
1668     { return m_box.size.width; }
1669     CvPoint2D32f get_center() const // center of the object
1670     { return m_box.center; }
1671     CvRect get_window() const // bounding rectangle for the object
1672     { return m_comp.rect; }
1673
1674     /*********************** Tracking parameters ************************/
1675     int     get_threshold() const // thresholding value that applied to back project
1676     { return m_threshold; }
1677
1678     int     get_hist_dims( int* dims = 0 ) const // returns number of histogram dimensions and sets
1679     { return m_hist ? cvGetDims( m_hist->bins, dims ) : 0; }
1680
1681     int     get_min_ch_val( int channel ) const // get the minimum allowed value of the specified channel
1682     { return m_min_ch_val[channel]; }
1683
1684     int     get_max_ch_val( int channel ) const // get the maximum allowed value of the specified channel
1685     { return m_max_ch_val[channel]; }
1686
1687     // set initial object rectangle (must be called before initial calculation of the histogram)
1688     bool    set_window( CvRect window)
1689     { m_comp.rect = window; return true; }
1690
1691     bool    set_threshold( int threshold ) // threshold applied to the histogram bins
1692     { m_threshold = threshold; return true; }
1693
1694     bool    set_hist_bin_range( int dim, int min_val, int max_val );
1695
1696     bool    set_hist_dims( int c_dims, int* dims );// set the histogram parameters
1697
1698     bool    set_min_ch_val( int channel, int val ) // set the minimum allowed value of the specified channel
1699     { m_min_ch_val[channel] = val; return true; }
1700     bool    set_max_ch_val( int channel, int val ) // set the maximum allowed value of the specified channel
1701     { m_max_ch_val[channel] = val; return true; }
1702
1703     /************************ The processing methods *********************************/
1704     // update object position
1705     virtual bool  track_object( const IplImage* cur_frame );
1706
1707     // update object histogram
1708     virtual bool  update_histogram( const IplImage* cur_frame );
1709
1710     // reset histogram
1711     virtual void  reset_histogram();
1712
1713     /************************ Retrieving internal data *******************************/
1714     // get back project image
1715     virtual IplImage* get_back_project()
1716     { return m_back_project; }
1717
1718     float query( int* bin ) const
1719     { return m_hist ? (float)cvGetRealND(m_hist->bins, bin) : 0.f; }
1720
1721 protected:
1722
1723     // internal method for color conversion: fills m_color_planes group
1724     virtual void color_transform( const IplImage* img );
1725
1726     CvHistogram* m_hist;
1727
1728     CvBox2D    m_box;
1729     CvConnectedComp m_comp;
1730
1731     float      m_hist_ranges_data[CV_MAX_DIM][2];
1732     float*     m_hist_ranges[CV_MAX_DIM];
1733
1734     int        m_min_ch_val[CV_MAX_DIM];
1735     int        m_max_ch_val[CV_MAX_DIM];
1736     int        m_threshold;
1737
1738     IplImage*  m_color_planes[CV_MAX_DIM];
1739     IplImage*  m_back_project;
1740     IplImage*  m_temp;
1741     IplImage*  m_mask;
1742 };
1743
1744 /****************************************************************************************\
1745 *                              Expectation - Maximization                                *
1746 \****************************************************************************************/
1747 struct CV_EXPORTS_W_MAP CvEMParams
1748 {
1749     CvEMParams();
1750     CvEMParams( int nclusters, int cov_mat_type=cv::EM::COV_MAT_DIAGONAL,
1751                 int start_step=cv::EM::START_AUTO_STEP,
1752                 CvTermCriteria term_crit=cvTermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, 100, FLT_EPSILON),
1753                 const CvMat* probs=0, const CvMat* weights=0, const CvMat* means=0, const CvMat** covs=0 );
1754
1755     CV_PROP_RW int nclusters;
1756     CV_PROP_RW int cov_mat_type;
1757     CV_PROP_RW int start_step;
1758     const CvMat* probs;
1759     const CvMat* weights;
1760     const CvMat* means;
1761     const CvMat** covs;
1762     CV_PROP_RW CvTermCriteria term_crit;
1763 };
1764
1765
1766 class CV_EXPORTS_W CvEM : public CvStatModel
1767 {
1768 public:
1769     // Type of covariation matrices
1770     enum { COV_MAT_SPHERICAL=cv::EM::COV_MAT_SPHERICAL,
1771            COV_MAT_DIAGONAL =cv::EM::COV_MAT_DIAGONAL,
1772            COV_MAT_GENERIC  =cv::EM::COV_MAT_GENERIC };
1773
1774     // The initial step
1775     enum { START_E_STEP=cv::EM::START_E_STEP,
1776            START_M_STEP=cv::EM::START_M_STEP,
1777            START_AUTO_STEP=cv::EM::START_AUTO_STEP };
1778
1779     CV_WRAP CvEM();
1780     CvEM( const CvMat* samples, const CvMat* sampleIdx=0,
1781           CvEMParams params=CvEMParams(), CvMat* labels=0 );
1782
1783     virtual ~CvEM();
1784
1785     virtual bool train( const CvMat* samples, const CvMat* sampleIdx=0,
1786                         CvEMParams params=CvEMParams(), CvMat* labels=0 );
1787
1788     virtual float predict( const CvMat* sample, CV_OUT CvMat* probs ) const;
1789
1790     CV_WRAP CvEM( const cv::Mat& samples, const cv::Mat& sampleIdx=cv::Mat(),
1791                   CvEMParams params=CvEMParams() );
1792
1793     CV_WRAP virtual bool train( const cv::Mat& samples,
1794                                 const cv::Mat& sampleIdx=cv::Mat(),
1795                                 CvEMParams params=CvEMParams(),
1796                                 CV_OUT cv::Mat* labels=0 );
1797
1798     CV_WRAP virtual float predict( const cv::Mat& sample, CV_OUT cv::Mat* probs=0 ) const;
1799     CV_WRAP virtual double calcLikelihood( const cv::Mat &sample ) const;
1800
1801     CV_WRAP int getNClusters() const;
1802     CV_WRAP cv::Mat getMeans() const;
1803     CV_WRAP void getCovs(CV_OUT std::vector<cv::Mat>& covs) const;
1804     CV_WRAP cv::Mat getWeights() const;
1805     CV_WRAP cv::Mat getProbs() const;
1806
1807     CV_WRAP inline double getLikelihood() const { return emObj.isTrained() ? logLikelihood : DBL_MAX; }
1808
1809     CV_WRAP virtual void clear();
1810
1811     int get_nclusters() const;
1812     const CvMat* get_means() const;
1813     const CvMat** get_covs() const;
1814     const CvMat* get_weights() const;
1815     const CvMat* get_probs() const;
1816
1817     inline double get_log_likelihood() const { return getLikelihood(); }
1818
1819     virtual void read( CvFileStorage* fs, CvFileNode* node );
1820     virtual void write( CvFileStorage* fs, const char* name ) const;
1821
1822 protected:
1823     void set_mat_hdrs();
1824
1825     cv::EM emObj;
1826     cv::Mat probs;
1827     double logLikelihood;
1828
1829     CvMat meansHdr;
1830     std::vector<CvMat> covsHdrs;
1831     std::vector<CvMat*> covsPtrs;
1832     CvMat weightsHdr;
1833     CvMat probsHdr;
1834 };
1835
1836 namespace cv
1837 {
1838
1839 typedef CvEMParams EMParams;
1840 typedef CvEM ExpectationMaximization;
1841
1842 /*!
1843  The Patch Generator class
1844  */
1845 class CV_EXPORTS PatchGenerator
1846 {
1847 public:
1848     PatchGenerator();
1849     PatchGenerator(double _backgroundMin, double _backgroundMax,
1850                    double _noiseRange, bool _randomBlur=true,
1851                    double _lambdaMin=0.6, double _lambdaMax=1.5,
1852                    double _thetaMin=-CV_PI, double _thetaMax=CV_PI,
1853                    double _phiMin=-CV_PI, double _phiMax=CV_PI );
1854     void operator()(const Mat& image, Point2f pt, Mat& patch, Size patchSize, RNG& rng) const;
1855     void operator()(const Mat& image, const Mat& transform, Mat& patch,
1856                     Size patchSize, RNG& rng) const;
1857     void warpWholeImage(const Mat& image, Mat& matT, Mat& buf,
1858                         CV_OUT Mat& warped, int border, RNG& rng) const;
1859     void generateRandomTransform(Point2f srcCenter, Point2f dstCenter,
1860                                  CV_OUT Mat& transform, RNG& rng,
1861                                  bool inverse=false) const;
1862     void setAffineParam(double lambda, double theta, double phi);
1863
1864     double backgroundMin, backgroundMax;
1865     double noiseRange;
1866     bool randomBlur;
1867     double lambdaMin, lambdaMax;
1868     double thetaMin, thetaMax;
1869     double phiMin, phiMax;
1870 };
1871
1872
1873 class CV_EXPORTS LDetector
1874 {
1875 public:
1876     LDetector();
1877     LDetector(int _radius, int _threshold, int _nOctaves,
1878               int _nViews, double _baseFeatureSize, double _clusteringDistance);
1879     void operator()(const Mat& image,
1880                     CV_OUT vector<KeyPoint>& keypoints,
1881                     int maxCount=0, bool scaleCoords=true) const;
1882     void operator()(const vector<Mat>& pyr,
1883                     CV_OUT vector<KeyPoint>& keypoints,
1884                     int maxCount=0, bool scaleCoords=true) const;
1885     void getMostStable2D(const Mat& image, CV_OUT vector<KeyPoint>& keypoints,
1886                          int maxCount, const PatchGenerator& patchGenerator) const;
1887     void setVerbose(bool verbose);
1888
1889     void read(const FileNode& node);
1890     void write(FileStorage& fs, const String& name=String()) const;
1891
1892     int radius;
1893     int threshold;
1894     int nOctaves;
1895     int nViews;
1896     bool verbose;
1897
1898     double baseFeatureSize;
1899     double clusteringDistance;
1900 };
1901
1902 typedef LDetector YAPE;
1903
1904 class CV_EXPORTS FernClassifier
1905 {
1906 public:
1907     FernClassifier();
1908     FernClassifier(const FileNode& node);
1909     FernClassifier(const vector<vector<Point2f> >& points,
1910                    const vector<Mat>& refimgs,
1911                    const vector<vector<int> >& labels=vector<vector<int> >(),
1912                    int _nclasses=0, int _patchSize=PATCH_SIZE,
1913                    int _signatureSize=DEFAULT_SIGNATURE_SIZE,
1914                    int _nstructs=DEFAULT_STRUCTS,
1915                    int _structSize=DEFAULT_STRUCT_SIZE,
1916                    int _nviews=DEFAULT_VIEWS,
1917                    int _compressionMethod=COMPRESSION_NONE,
1918                    const PatchGenerator& patchGenerator=PatchGenerator());
1919     virtual ~FernClassifier();
1920     virtual void read(const FileNode& n);
1921     virtual void write(FileStorage& fs, const String& name=String()) const;
1922     virtual void trainFromSingleView(const Mat& image,
1923                                      const vector<KeyPoint>& keypoints,
1924                                      int _patchSize=PATCH_SIZE,
1925                                      int _signatureSize=DEFAULT_SIGNATURE_SIZE,
1926                                      int _nstructs=DEFAULT_STRUCTS,
1927                                      int _structSize=DEFAULT_STRUCT_SIZE,
1928                                      int _nviews=DEFAULT_VIEWS,
1929                                      int _compressionMethod=COMPRESSION_NONE,
1930                                      const PatchGenerator& patchGenerator=PatchGenerator());
1931     virtual void train(const vector<vector<Point2f> >& points,
1932                        const vector<Mat>& refimgs,
1933                        const vector<vector<int> >& labels=vector<vector<int> >(),
1934                        int _nclasses=0, int _patchSize=PATCH_SIZE,
1935                        int _signatureSize=DEFAULT_SIGNATURE_SIZE,
1936                        int _nstructs=DEFAULT_STRUCTS,
1937                        int _structSize=DEFAULT_STRUCT_SIZE,
1938                        int _nviews=DEFAULT_VIEWS,
1939                        int _compressionMethod=COMPRESSION_NONE,
1940                        const PatchGenerator& patchGenerator=PatchGenerator());
1941     virtual int operator()(const Mat& img, Point2f kpt, vector<float>& signature) const;
1942     virtual int operator()(const Mat& patch, vector<float>& signature) const;
1943     virtual void clear();
1944     virtual bool empty() const;
1945     void setVerbose(bool verbose);
1946
1947     int getClassCount() const;
1948     int getStructCount() const;
1949     int getStructSize() const;
1950     int getSignatureSize() const;
1951     int getCompressionMethod() const;
1952     Size getPatchSize() const;
1953
1954     struct Feature
1955     {
1956         uchar x1, y1, x2, y2;
1957         Feature() : x1(0), y1(0), x2(0), y2(0) {}
1958         Feature(int _x1, int _y1, int _x2, int _y2)
1959         : x1((uchar)_x1), y1((uchar)_y1), x2((uchar)_x2), y2((uchar)_y2)
1960         {}
1961         template<typename _Tp> bool operator ()(const Mat_<_Tp>& patch) const
1962         { return patch(y1,x1) > patch(y2, x2); }
1963     };
1964
1965     enum
1966     {
1967         PATCH_SIZE = 31,
1968         DEFAULT_STRUCTS = 50,
1969         DEFAULT_STRUCT_SIZE = 9,
1970         DEFAULT_VIEWS = 5000,
1971         DEFAULT_SIGNATURE_SIZE = 176,
1972         COMPRESSION_NONE = 0,
1973         COMPRESSION_RANDOM_PROJ = 1,
1974         COMPRESSION_PCA = 2,
1975         DEFAULT_COMPRESSION_METHOD = COMPRESSION_NONE
1976     };
1977
1978 protected:
1979     virtual void prepare(int _nclasses, int _patchSize, int _signatureSize,
1980                          int _nstructs, int _structSize,
1981                          int _nviews, int _compressionMethod);
1982     virtual void finalize(RNG& rng);
1983     virtual int getLeaf(int fidx, const Mat& patch) const;
1984
1985     bool verbose;
1986     int nstructs;
1987     int structSize;
1988     int nclasses;
1989     int signatureSize;
1990     int compressionMethod;
1991     int leavesPerStruct;
1992     Size patchSize;
1993     vector<Feature> features;
1994     vector<int> classCounters;
1995     vector<float> posteriors;
1996 };
1997
1998
1999 /****************************************************************************************\
2000  *                                 Calonder Classifier                                    *
2001  \****************************************************************************************/
2002
2003 struct RTreeNode;
2004
2005 struct CV_EXPORTS BaseKeypoint
2006 {
2007     int x;
2008     int y;
2009     IplImage* image;
2010
2011     BaseKeypoint()
2012     : x(0), y(0), image(NULL)
2013     {}
2014
2015     BaseKeypoint(int _x, int _y, IplImage* _image)
2016     : x(_x), y(_y), image(_image)
2017     {}
2018 };
2019
2020 class CV_EXPORTS RandomizedTree
2021 {
2022 public:
2023     friend class RTreeClassifier;
2024
2025     static const uchar PATCH_SIZE = 32;
2026     static const int DEFAULT_DEPTH = 9;
2027     static const int DEFAULT_VIEWS = 5000;
2028     static const size_t DEFAULT_REDUCED_NUM_DIM = 176;
2029     static float GET_LOWER_QUANT_PERC() { return .03f; }
2030     static float GET_UPPER_QUANT_PERC() { return .92f; }
2031
2032     RandomizedTree();
2033     ~RandomizedTree();
2034
2035     void train(vector<BaseKeypoint> const& base_set, RNG &rng,
2036                int depth, int views, size_t reduced_num_dim, int num_quant_bits);
2037     void train(vector<BaseKeypoint> const& base_set, RNG &rng,
2038                PatchGenerator &make_patch, int depth, int views, size_t reduced_num_dim,
2039                int num_quant_bits);
2040
2041     // following two funcs are EXPERIMENTAL (do not use unless you know exactly what you do)
2042     static void quantizeVector(float *vec, int dim, int N, float bnds[2], int clamp_mode=0);
2043     static void quantizeVector(float *src, int dim, int N, float bnds[2], uchar *dst);
2044
2045     // patch_data must be a 32x32 array (no row padding)
2046     float* getPosterior(uchar* patch_data);
2047     const float* getPosterior(uchar* patch_data) const;
2048     uchar* getPosterior2(uchar* patch_data);
2049     const uchar* getPosterior2(uchar* patch_data) const;
2050
2051     void read(const char* file_name, int num_quant_bits);
2052     void read(std::istream &is, int num_quant_bits);
2053     void write(const char* file_name) const;
2054     void write(std::ostream &os) const;
2055
2056     int classes() { return classes_; }
2057     int depth() { return depth_; }
2058
2059     //void setKeepFloatPosteriors(bool b) { keep_float_posteriors_ = b; }
2060     void discardFloatPosteriors() { freePosteriors(1); }
2061
2062     inline void applyQuantization(int num_quant_bits) { makePosteriors2(num_quant_bits); }
2063
2064     // debug
2065     void savePosteriors(std::string url, bool append=false);
2066     void savePosteriors2(std::string url, bool append=false);
2067
2068 private:
2069     int classes_;
2070     int depth_;
2071     int num_leaves_;
2072     vector<RTreeNode> nodes_;
2073     float **posteriors_;        // 16-bytes aligned posteriors
2074     uchar **posteriors2_;     // 16-bytes aligned posteriors
2075     vector<int> leaf_counts_;
2076
2077     void createNodes(int num_nodes, RNG &rng);
2078     void allocPosteriorsAligned(int num_leaves, int num_classes);
2079     void freePosteriors(int which);    // which: 1=posteriors_, 2=posteriors2_, 3=both
2080     void init(int classes, int depth, RNG &rng);
2081     void addExample(int class_id, uchar* patch_data);
2082     void finalize(size_t reduced_num_dim, int num_quant_bits);
2083     int getIndex(uchar* patch_data) const;
2084     inline float* getPosteriorByIndex(int index);
2085     inline const float* getPosteriorByIndex(int index) const;
2086     inline uchar* getPosteriorByIndex2(int index);
2087     inline const uchar* getPosteriorByIndex2(int index) const;
2088     //void makeRandomMeasMatrix(float *cs_phi, PHI_DISTR_TYPE dt, size_t reduced_num_dim);
2089     void convertPosteriorsToChar();
2090     void makePosteriors2(int num_quant_bits);
2091     void compressLeaves(size_t reduced_num_dim);
2092     void estimateQuantPercForPosteriors(float perc[2]);
2093 };
2094
2095
2096 inline uchar* getData(IplImage* image)
2097 {
2098     return reinterpret_cast<uchar*>(image->imageData);
2099 }
2100
2101 inline float* RandomizedTree::getPosteriorByIndex(int index)
2102 {
2103     return const_cast<float*>(const_cast<const RandomizedTree*>(this)->getPosteriorByIndex(index));
2104 }
2105
2106 inline const float* RandomizedTree::getPosteriorByIndex(int index) const
2107 {
2108     return posteriors_[index];
2109 }
2110
2111 inline uchar* RandomizedTree::getPosteriorByIndex2(int index)
2112 {
2113     return const_cast<uchar*>(const_cast<const RandomizedTree*>(this)->getPosteriorByIndex2(index));
2114 }
2115
2116 inline const uchar* RandomizedTree::getPosteriorByIndex2(int index) const
2117 {
2118     return posteriors2_[index];
2119 }
2120
2121 struct CV_EXPORTS RTreeNode
2122 {
2123     short offset1, offset2;
2124
2125     RTreeNode() {}
2126     RTreeNode(uchar x1, uchar y1, uchar x2, uchar y2)
2127     : offset1(y1*RandomizedTree::PATCH_SIZE + x1),
2128     offset2(y2*RandomizedTree::PATCH_SIZE + x2)
2129     {}
2130
2131     //! Left child on 0, right child on 1
2132     inline bool operator() (uchar* patch_data) const
2133     {
2134         return patch_data[offset1] > patch_data[offset2];
2135     }
2136 };
2137
2138 class CV_EXPORTS RTreeClassifier
2139 {
2140 public:
2141     static const int DEFAULT_TREES = 48;
2142     static const size_t DEFAULT_NUM_QUANT_BITS = 4;
2143
2144     RTreeClassifier();
2145     void train(vector<BaseKeypoint> const& base_set,
2146                RNG &rng,
2147                int num_trees = RTreeClassifier::DEFAULT_TREES,
2148                int depth = RandomizedTree::DEFAULT_DEPTH,
2149                int views = RandomizedTree::DEFAULT_VIEWS,
2150                size_t reduced_num_dim = RandomizedTree::DEFAULT_REDUCED_NUM_DIM,
2151                int num_quant_bits = DEFAULT_NUM_QUANT_BITS);
2152     void train(vector<BaseKeypoint> const& base_set,
2153                RNG &rng,
2154                PatchGenerator &make_patch,
2155                int num_trees = RTreeClassifier::DEFAULT_TREES,
2156                int depth = RandomizedTree::DEFAULT_DEPTH,
2157                int views = RandomizedTree::DEFAULT_VIEWS,
2158                size_t reduced_num_dim = RandomizedTree::DEFAULT_REDUCED_NUM_DIM,
2159                int num_quant_bits = DEFAULT_NUM_QUANT_BITS);
2160
2161     // sig must point to a memory block of at least classes()*sizeof(float|uchar) bytes
2162     void getSignature(IplImage *patch, uchar *sig) const;
2163     void getSignature(IplImage *patch, float *sig) const;
2164     void getSparseSignature(IplImage *patch, float *sig, float thresh) const;
2165     // TODO: deprecated in favor of getSignature overload, remove
2166     void getFloatSignature(IplImage *patch, float *sig) const { getSignature(patch, sig); }
2167
2168     static int countNonZeroElements(float *vec, int n, double tol=1e-10);
2169     static inline void safeSignatureAlloc(uchar **sig, int num_sig=1, int sig_len=176);
2170     static inline uchar* safeSignatureAlloc(int num_sig=1, int sig_len=176);
2171
2172     inline int classes() const { return classes_; }
2173     inline int original_num_classes() const { return original_num_classes_; }
2174
2175     void setQuantization(int num_quant_bits);
2176     void discardFloatPosteriors();
2177
2178     void read(const char* file_name);
2179     void read(std::istream &is);
2180     void write(const char* file_name) const;
2181     void write(std::ostream &os) const;
2182
2183     // experimental and debug
2184     void saveAllFloatPosteriors(std::string file_url);
2185     void saveAllBytePosteriors(std::string file_url);
2186     void setFloatPosteriorsFromTextfile_176(std::string url);
2187     float countZeroElements();
2188
2189     vector<RandomizedTree> trees_;
2190
2191 private:
2192     int classes_;
2193     int num_quant_bits_;
2194     mutable uchar **posteriors_;
2195     mutable unsigned short *ptemp_;
2196     int original_num_classes_;
2197     bool keep_floats_;
2198 };
2199
2200 /****************************************************************************************\
2201 *                                     One-Way Descriptor                                 *
2202 \****************************************************************************************/
2203
2204 // CvAffinePose: defines a parameterized affine transformation of an image patch.
2205 // An image patch is rotated on angle phi (in degrees), then scaled lambda1 times
2206 // along horizontal and lambda2 times along vertical direction, and then rotated again
2207 // on angle (theta - phi).
2208 class CV_EXPORTS CvAffinePose
2209 {
2210 public:
2211     float phi;
2212     float theta;
2213     float lambda1;
2214     float lambda2;
2215 };
2216
2217 class CV_EXPORTS OneWayDescriptor
2218 {
2219 public:
2220     OneWayDescriptor();
2221     ~OneWayDescriptor();
2222
2223     // allocates memory for given descriptor parameters
2224     void Allocate(int pose_count, CvSize size, int nChannels);
2225
2226     // GenerateSamples: generates affine transformed patches with averaging them over small transformation variations.
2227     // If external poses and transforms were specified, uses them instead of generating random ones
2228     // - pose_count: the number of poses to be generated
2229     // - frontal: the input patch (can be a roi in a larger image)
2230     // - norm: if nonzero, normalizes the output patch so that the sum of pixel intensities is 1
2231     void GenerateSamples(int pose_count, IplImage* frontal, int norm = 0);
2232
2233     // GenerateSamplesFast: generates affine transformed patches with averaging them over small transformation variations.
2234     // Uses precalculated transformed pca components.
2235     // - frontal: the input patch (can be a roi in a larger image)
2236     // - pca_hr_avg: pca average vector
2237     // - pca_hr_eigenvectors: pca eigenvectors
2238     // - pca_descriptors: an array of precomputed descriptors of pca components containing their affine transformations
2239     //   pca_descriptors[0] corresponds to the average, pca_descriptors[1]-pca_descriptors[pca_dim] correspond to eigenvectors
2240     void GenerateSamplesFast(IplImage* frontal, CvMat* pca_hr_avg,
2241                              CvMat* pca_hr_eigenvectors, OneWayDescriptor* pca_descriptors);
2242
2243     // sets the poses and corresponding transforms
2244     void SetTransforms(CvAffinePose* poses, CvMat** transforms);
2245
2246     // Initialize: builds a descriptor.
2247     // - pose_count: the number of poses to build. If poses were set externally, uses them rather than generating random ones
2248     // - frontal: input patch. Can be a roi in a larger image
2249     // - feature_name: the feature name to be associated with the descriptor
2250     // - norm: if 1, the affine transformed patches are normalized so that their sum is 1
2251     void Initialize(int pose_count, IplImage* frontal, const char* feature_name = 0, int norm = 0);
2252
2253     // InitializeFast: builds a descriptor using precomputed descriptors of pca components
2254     // - pose_count: the number of poses to build
2255     // - frontal: input patch. Can be a roi in a larger image
2256     // - feature_name: the feature name to be associated with the descriptor
2257     // - pca_hr_avg: average vector for PCA
2258     // - pca_hr_eigenvectors: PCA eigenvectors (one vector per row)
2259     // - pca_descriptors: precomputed descriptors of PCA components, the first descriptor for the average vector
2260     // followed by the descriptors for eigenvectors
2261     void InitializeFast(int pose_count, IplImage* frontal, const char* feature_name,
2262                         CvMat* pca_hr_avg, CvMat* pca_hr_eigenvectors, OneWayDescriptor* pca_descriptors);
2263
2264     // ProjectPCASample: unwarps an image patch into a vector and projects it into PCA space
2265     // - patch: input image patch
2266     // - avg: PCA average vector
2267     // - eigenvectors: PCA eigenvectors, one per row
2268     // - pca_coeffs: output PCA coefficients
2269     void ProjectPCASample(IplImage* patch, CvMat* avg, CvMat* eigenvectors, CvMat* pca_coeffs) const;
2270
2271     // InitializePCACoeffs: projects all warped patches into PCA space
2272     // - avg: PCA average vector
2273     // - eigenvectors: PCA eigenvectors, one per row
2274     void InitializePCACoeffs(CvMat* avg, CvMat* eigenvectors);
2275
2276     // EstimatePose: finds the closest match between an input patch and a set of patches with different poses
2277     // - patch: input image patch
2278     // - pose_idx: the output index of the closest pose
2279     // - distance: the distance to the closest pose (L2 distance)
2280     void EstimatePose(IplImage* patch, int& pose_idx, float& distance) const;
2281
2282     // EstimatePosePCA: finds the closest match between an input patch and a set of patches with different poses.
2283     // The distance between patches is computed in PCA space
2284     // - patch: input image patch
2285     // - pose_idx: the output index of the closest pose
2286     // - distance: distance to the closest pose (L2 distance in PCA space)
2287     // - avg: PCA average vector. If 0, matching without PCA is used
2288     // - eigenvectors: PCA eigenvectors, one per row
2289     void EstimatePosePCA(CvArr* patch, int& pose_idx, float& distance, CvMat* avg, CvMat* eigenvalues) const;
2290
2291     // GetPatchSize: returns the size of each image patch after warping (2 times smaller than the input patch)
2292     CvSize GetPatchSize() const
2293     {
2294         return m_patch_size;
2295     }
2296
2297     // GetInputPatchSize: returns the required size of the patch that the descriptor is built from
2298     // (2 time larger than the patch after warping)
2299     CvSize GetInputPatchSize() const
2300     {
2301         return cvSize(m_patch_size.width*2, m_patch_size.height*2);
2302     }
2303
2304     // GetPatch: returns a patch corresponding to specified pose index
2305     // - index: pose index
2306     // - return value: the patch corresponding to specified pose index
2307     IplImage* GetPatch(int index);
2308
2309     // GetPose: returns a pose corresponding to specified pose index
2310     // - index: pose index
2311     // - return value: the pose corresponding to specified pose index
2312     CvAffinePose GetPose(int index) const;
2313
2314     // Save: saves all patches with different poses to a specified path
2315     void Save(const char* path);
2316
2317     // ReadByName: reads a descriptor from a file storage
2318     // - fs: file storage
2319     // - parent: parent node
2320     // - name: node name
2321     // - return value: 1 if succeeded, 0 otherwise
2322     int ReadByName(CvFileStorage* fs, CvFileNode* parent, const char* name);
2323
2324     // ReadByName: reads a descriptor from a file node
2325     // - parent: parent node
2326     // - name: node name
2327     // - return value: 1 if succeeded, 0 otherwise
2328     int ReadByName(const FileNode &parent, const char* name);
2329
2330     // Write: writes a descriptor into a file storage
2331     // - fs: file storage
2332     // - name: node name
2333     void Write(CvFileStorage* fs, const char* name);
2334
2335     // GetFeatureName: returns a name corresponding to a feature
2336     const char* GetFeatureName() const;
2337
2338     // GetCenter: returns the center of the feature
2339     CvPoint GetCenter() const;
2340
2341     void SetPCADimHigh(int pca_dim_high) {m_pca_dim_high = pca_dim_high;};
2342     void SetPCADimLow(int pca_dim_low) {m_pca_dim_low = pca_dim_low;};
2343
2344     int GetPCADimLow() const;
2345     int GetPCADimHigh() const;
2346
2347     CvMat** GetPCACoeffs() const {return m_pca_coeffs;}
2348
2349 protected:
2350     int m_pose_count; // the number of poses
2351     CvSize m_patch_size; // size of each image
2352     IplImage** m_samples; // an array of length m_pose_count containing the patch in different poses
2353     IplImage* m_input_patch;
2354     IplImage* m_train_patch;
2355     CvMat** m_pca_coeffs; // an array of length m_pose_count containing pca decomposition of the patch in different poses
2356     CvAffinePose* m_affine_poses; // an array of poses
2357     CvMat** m_transforms; // an array of affine transforms corresponding to poses
2358
2359     string m_feature_name; // the name of the feature associated with the descriptor
2360     CvPoint m_center; // the coordinates of the feature (the center of the input image ROI)
2361
2362     int m_pca_dim_high; // the number of descriptor pca components to use for generating affine poses
2363     int m_pca_dim_low; // the number of pca components to use for comparison
2364 };
2365
2366
2367 // OneWayDescriptorBase: encapsulates functionality for training/loading a set of one way descriptors
2368 // and finding the nearest closest descriptor to an input feature
2369 class CV_EXPORTS OneWayDescriptorBase
2370 {
2371 public:
2372
2373     // creates an instance of OneWayDescriptor from a set of training files
2374     // - patch_size: size of the input (large) patch
2375     // - pose_count: the number of poses to generate for each descriptor
2376     // - train_path: path to training files
2377     // - pca_config: the name of the file that contains PCA for small patches (2 times smaller
2378     // than patch_size each dimension
2379     // - pca_hr_config: the name of the file that contains PCA for large patches (of patch_size size)
2380     // - pca_desc_config: the name of the file that contains descriptors of PCA components
2381     OneWayDescriptorBase(CvSize patch_size, int pose_count, const char* train_path = 0, const char* pca_config = 0,
2382                          const char* pca_hr_config = 0, const char* pca_desc_config = 0, int pyr_levels = 1,
2383                          int pca_dim_high = 100, int pca_dim_low = 100);
2384
2385     OneWayDescriptorBase(CvSize patch_size, int pose_count, const string &pca_filename, const string &train_path = string(), const string &images_list = string(),
2386                          float _scale_min = 0.7f, float _scale_max=1.5f, float _scale_step=1.2f, int pyr_levels = 1,
2387                          int pca_dim_high = 100, int pca_dim_low = 100);
2388
2389
2390     virtual ~OneWayDescriptorBase();
2391     void clear ();
2392
2393
2394     // Allocate: allocates memory for a given number of descriptors
2395     void Allocate(int train_feature_count);
2396
2397     // AllocatePCADescriptors: allocates memory for pca descriptors
2398     void AllocatePCADescriptors();
2399
2400     // returns patch size
2401     CvSize GetPatchSize() const {return m_patch_size;};
2402     // returns the number of poses for each descriptor
2403     int GetPoseCount() const {return m_pose_count;};
2404
2405     // returns the number of pyramid levels
2406     int GetPyrLevels() const {return m_pyr_levels;};
2407
2408     // returns the number of descriptors
2409     int GetDescriptorCount() const {return m_train_feature_count;};
2410
2411     // CreateDescriptorsFromImage: creates descriptors for each of the input features
2412     // - src: input image
2413     // - features: input features
2414     // - pyr_levels: the number of pyramid levels
2415     void CreateDescriptorsFromImage(IplImage* src, const vector<KeyPoint>& features);
2416
2417     // CreatePCADescriptors: generates descriptors for PCA components, needed for fast generation of feature descriptors
2418     void CreatePCADescriptors();
2419
2420     // returns a feature descriptor by feature index
2421     const OneWayDescriptor* GetDescriptor(int desc_idx) const {return &m_descriptors[desc_idx];};
2422
2423     // FindDescriptor: finds the closest descriptor
2424     // - patch: input image patch
2425     // - desc_idx: output index of the closest descriptor to the input patch
2426     // - pose_idx: output index of the closest pose of the closest descriptor to the input patch
2427     // - distance: distance from the input patch to the closest feature pose
2428     // - _scales: scales of the input patch for each descriptor
2429     // - scale_ranges: input scales variation (float[2])
2430     void FindDescriptor(IplImage* patch, int& desc_idx, int& pose_idx, float& distance, float* _scale = 0, float* scale_ranges = 0) const;
2431
2432     // - patch: input image patch
2433     // - n: number of the closest indexes
2434     // - desc_idxs: output indexes of the closest descriptor to the input patch (n)
2435     // - pose_idx: output indexes of the closest pose of the closest descriptor to the input patch (n)
2436     // - distances: distance from the input patch to the closest feature pose (n)
2437     // - _scales: scales of the input patch
2438     // - scale_ranges: input scales variation (float[2])
2439     void FindDescriptor(IplImage* patch, int n, vector<int>& desc_idxs, vector<int>& pose_idxs,
2440                         vector<float>& distances, vector<float>& _scales, float* scale_ranges = 0) const;
2441
2442     // FindDescriptor: finds the closest descriptor
2443     // - src: input image
2444     // - pt: center of the feature
2445     // - desc_idx: output index of the closest descriptor to the input patch
2446     // - pose_idx: output index of the closest pose of the closest descriptor to the input patch
2447     // - distance: distance from the input patch to the closest feature pose
2448     void FindDescriptor(IplImage* src, cv::Point2f pt, int& desc_idx, int& pose_idx, float& distance) const;
2449
2450     // InitializePoses: generates random poses
2451     void InitializePoses();
2452
2453     // InitializeTransformsFromPoses: generates 2x3 affine matrices from poses (initializes m_transforms)
2454     void InitializeTransformsFromPoses();
2455
2456     // InitializePoseTransforms: subsequently calls InitializePoses and InitializeTransformsFromPoses
2457     void InitializePoseTransforms();
2458
2459     // InitializeDescriptor: initializes a descriptor
2460     // - desc_idx: descriptor index
2461     // - train_image: image patch (ROI is supported)
2462     // - feature_label: feature textual label
2463     void InitializeDescriptor(int desc_idx, IplImage* train_image, const char* feature_label);
2464
2465     void InitializeDescriptor(int desc_idx, IplImage* train_image, const KeyPoint& keypoint, const char* feature_label);
2466
2467     // InitializeDescriptors: load features from an image and create descriptors for each of them
2468     void InitializeDescriptors(IplImage* train_image, const vector<KeyPoint>& features,
2469                                const char* feature_label = "", int desc_start_idx = 0);
2470
2471     // Write: writes this object to a file storage
2472     // - fs: output filestorage
2473     void Write (FileStorage &fs) const;
2474
2475     // Read: reads OneWayDescriptorBase object from a file node
2476     // - fn: input file node
2477     void Read (const FileNode &fn);
2478
2479     // LoadPCADescriptors: loads PCA descriptors from a file
2480     // - filename: input filename
2481     int LoadPCADescriptors(const char* filename);
2482
2483     // LoadPCADescriptors: loads PCA descriptors from a file node
2484     // - fn: input file node
2485     int LoadPCADescriptors(const FileNode &fn);
2486
2487     // SavePCADescriptors: saves PCA descriptors to a file
2488     // - filename: output filename
2489     void SavePCADescriptors(const char* filename);
2490
2491     // SavePCADescriptors: saves PCA descriptors to a file storage
2492     // - fs: output file storage
2493     void SavePCADescriptors(CvFileStorage* fs) const;
2494
2495     // GeneratePCA: calculate and save PCA components and descriptors
2496     // - img_path: path to training PCA images directory
2497     // - images_list: filename with filenames of training PCA images
2498     void GeneratePCA(const char* img_path, const char* images_list, int pose_count=500);
2499
2500     // SetPCAHigh: sets the high resolution pca matrices (copied to internal structures)
2501     void SetPCAHigh(CvMat* avg, CvMat* eigenvectors);
2502
2503     // SetPCALow: sets the low resolution pca matrices (copied to internal structures)
2504     void SetPCALow(CvMat* avg, CvMat* eigenvectors);
2505
2506     int GetLowPCA(CvMat** avg, CvMat** eigenvectors)
2507     {
2508         *avg = m_pca_avg;
2509         *eigenvectors = m_pca_eigenvectors;
2510         return m_pca_dim_low;
2511     };
2512
2513     int GetPCADimLow() const {return m_pca_dim_low;};
2514     int GetPCADimHigh() const {return m_pca_dim_high;};
2515
2516     void ConvertDescriptorsArrayToTree(); // Converting pca_descriptors array to KD tree
2517
2518     // GetPCAFilename: get default PCA filename
2519     static string GetPCAFilename () { return "pca.yml"; }
2520
2521     virtual bool empty() const { return m_train_feature_count <= 0 ? true : false; }
2522
2523 protected:
2524     CvSize m_patch_size; // patch size
2525     int m_pose_count; // the number of poses for each descriptor
2526     int m_train_feature_count; // the number of the training features
2527     OneWayDescriptor* m_descriptors; // array of train feature descriptors
2528     CvMat* m_pca_avg; // PCA average Vector for small patches
2529     CvMat* m_pca_eigenvectors; // PCA eigenvectors for small patches
2530     CvMat* m_pca_hr_avg; // PCA average Vector for large patches
2531     CvMat* m_pca_hr_eigenvectors; // PCA eigenvectors for large patches
2532     OneWayDescriptor* m_pca_descriptors; // an array of PCA descriptors
2533
2534     cv::flann::Index* m_pca_descriptors_tree;
2535     CvMat* m_pca_descriptors_matrix;
2536
2537     CvAffinePose* m_poses; // array of poses
2538     CvMat** m_transforms; // array of affine transformations corresponding to poses
2539
2540     int m_pca_dim_high;
2541     int m_pca_dim_low;
2542
2543     int m_pyr_levels;
2544     float scale_min;
2545     float scale_max;
2546     float scale_step;
2547
2548     // SavePCAall: saves PCA components and descriptors to a file storage
2549     // - fs: output file storage
2550     void SavePCAall (FileStorage &fs) const;
2551
2552     // LoadPCAall: loads PCA components and descriptors from a file node
2553     // - fn: input file node
2554     void LoadPCAall (const FileNode &fn);
2555 };
2556
2557 class CV_EXPORTS OneWayDescriptorObject : public OneWayDescriptorBase
2558 {
2559 public:
2560     // creates an instance of OneWayDescriptorObject from a set of training files
2561     // - patch_size: size of the input (large) patch
2562     // - pose_count: the number of poses to generate for each descriptor
2563     // - train_path: path to training files
2564     // - pca_config: the name of the file that contains PCA for small patches (2 times smaller
2565     // than patch_size each dimension
2566     // - pca_hr_config: the name of the file that contains PCA for large patches (of patch_size size)
2567     // - pca_desc_config: the name of the file that contains descriptors of PCA components
2568     OneWayDescriptorObject(CvSize patch_size, int pose_count, const char* train_path, const char* pca_config,
2569                            const char* pca_hr_config = 0, const char* pca_desc_config = 0, int pyr_levels = 1);
2570
2571     OneWayDescriptorObject(CvSize patch_size, int pose_count, const string &pca_filename,
2572                            const string &train_path = string (), const string &images_list = string (),
2573                            float _scale_min = 0.7f, float _scale_max=1.5f, float _scale_step=1.2f, int pyr_levels = 1);
2574
2575
2576     virtual ~OneWayDescriptorObject();
2577
2578     // Allocate: allocates memory for a given number of features
2579     // - train_feature_count: the total number of features
2580     // - object_feature_count: the number of features extracted from the object
2581     void Allocate(int train_feature_count, int object_feature_count);
2582
2583
2584     void SetLabeledFeatures(const vector<KeyPoint>& features) {m_train_features = features;};
2585     vector<KeyPoint>& GetLabeledFeatures() {return m_train_features;};
2586     const vector<KeyPoint>& GetLabeledFeatures() const {return m_train_features;};
2587     vector<KeyPoint> _GetLabeledFeatures() const;
2588
2589     // IsDescriptorObject: returns 1 if descriptor with specified index is positive, otherwise 0
2590     int IsDescriptorObject(int desc_idx) const;
2591
2592     // MatchPointToPart: returns the part number of a feature if it matches one of the object parts, otherwise -1
2593     int MatchPointToPart(CvPoint pt) const;
2594
2595     // GetDescriptorPart: returns the part number of the feature corresponding to a specified descriptor
2596     // - desc_idx: descriptor index
2597     int GetDescriptorPart(int desc_idx) const;
2598
2599
2600     void InitializeObjectDescriptors(IplImage* train_image, const vector<KeyPoint>& features,
2601                                      const char* feature_label, int desc_start_idx = 0, float scale = 1.0f,
2602                                      int is_background = 0);
2603
2604     // GetObjectFeatureCount: returns the number of object features
2605     int GetObjectFeatureCount() const {return m_object_feature_count;};
2606
2607 protected:
2608     int* m_part_id; // contains part id for each of object descriptors
2609     vector<KeyPoint> m_train_features; // train features
2610     int m_object_feature_count; // the number of the positive features
2611
2612 };
2613
2614
2615 /*
2616  *  OneWayDescriptorMatcher
2617  */
2618 class OneWayDescriptorMatcher;
2619 typedef OneWayDescriptorMatcher OneWayDescriptorMatch;
2620
2621 class CV_EXPORTS OneWayDescriptorMatcher : public GenericDescriptorMatcher
2622 {
2623 public:
2624     class CV_EXPORTS Params
2625     {
2626     public:
2627         static const int POSE_COUNT = 500;
2628         static const int PATCH_WIDTH = 24;
2629         static const int PATCH_HEIGHT = 24;
2630         static float GET_MIN_SCALE() { return 0.7f; }
2631         static float GET_MAX_SCALE() { return 1.5f; }
2632         static float GET_STEP_SCALE() { return 1.2f; }
2633
2634         Params( int poseCount = POSE_COUNT,
2635                Size patchSize = Size(PATCH_WIDTH, PATCH_HEIGHT),
2636                string pcaFilename = string(),
2637                string trainPath = string(), string trainImagesList = string(),
2638                float minScale = GET_MIN_SCALE(), float maxScale = GET_MAX_SCALE(),
2639                float stepScale = GET_STEP_SCALE() );
2640
2641         int poseCount;
2642         Size patchSize;
2643         string pcaFilename;
2644         string trainPath;
2645         string trainImagesList;
2646
2647         float minScale, maxScale, stepScale;
2648     };
2649
2650     OneWayDescriptorMatcher( const Params& params=Params() );
2651     virtual ~OneWayDescriptorMatcher();
2652
2653     void initialize( const Params& params, const Ptr<OneWayDescriptorBase>& base=Ptr<OneWayDescriptorBase>() );
2654
2655     // Clears keypoints storing in collection and OneWayDescriptorBase
2656     virtual void clear();
2657
2658     virtual void train();
2659
2660     virtual bool isMaskSupported();
2661
2662     virtual void read( const FileNode &fn );
2663     virtual void write( FileStorage& fs ) const;
2664
2665     virtual bool empty() const;
2666
2667     virtual Ptr<GenericDescriptorMatcher> clone( bool emptyTrainData=false ) const;
2668
2669 protected:
2670     // Matches a set of keypoints from a single image of the training set. A rectangle with a center in a keypoint
2671     // and size (patch_width/2*scale, patch_height/2*scale) is cropped from the source image for each
2672     // keypoint. scale is iterated from DescriptorOneWayParams::min_scale to DescriptorOneWayParams::max_scale.
2673     // The minimum distance to each training patch with all its affine poses is found over all scales.
2674     // The class ID of a match is returned for each keypoint. The distance is calculated over PCA components
2675     // loaded with DescriptorOneWay::Initialize, kd tree is used for finding minimum distances.
2676     virtual void knnMatchImpl( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
2677                               vector<vector<DMatch> >& matches, int k,
2678                               const vector<Mat>& masks, bool compactResult );
2679     virtual void radiusMatchImpl( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
2680                                  vector<vector<DMatch> >& matches, float maxDistance,
2681                                  const vector<Mat>& masks, bool compactResult );
2682
2683     Ptr<OneWayDescriptorBase> base;
2684     Params params;
2685     int prevTrainCount;
2686 };
2687
2688 /*
2689  *  FernDescriptorMatcher
2690  */
2691 class FernDescriptorMatcher;
2692 typedef FernDescriptorMatcher FernDescriptorMatch;
2693
2694 class CV_EXPORTS FernDescriptorMatcher : public GenericDescriptorMatcher
2695 {
2696 public:
2697     class CV_EXPORTS Params
2698     {
2699     public:
2700         Params( int nclasses=0,
2701                int patchSize=FernClassifier::PATCH_SIZE,
2702                int signatureSize=FernClassifier::DEFAULT_SIGNATURE_SIZE,
2703                int nstructs=FernClassifier::DEFAULT_STRUCTS,
2704                int structSize=FernClassifier::DEFAULT_STRUCT_SIZE,
2705                int nviews=FernClassifier::DEFAULT_VIEWS,
2706                int compressionMethod=FernClassifier::COMPRESSION_NONE,
2707                const PatchGenerator& patchGenerator=PatchGenerator() );
2708
2709         Params( const string& filename );
2710
2711         int nclasses;
2712         int patchSize;
2713         int signatureSize;
2714         int nstructs;
2715         int structSize;
2716         int nviews;
2717         int compressionMethod;
2718         PatchGenerator patchGenerator;
2719
2720         string filename;
2721     };
2722
2723     FernDescriptorMatcher( const Params& params=Params() );
2724     virtual ~FernDescriptorMatcher();
2725
2726     virtual void clear();
2727
2728     virtual void train();
2729
2730     virtual bool isMaskSupported();
2731
2732     virtual void read( const FileNode &fn );
2733     virtual void write( FileStorage& fs ) const;
2734     virtual bool empty() const;
2735
2736     virtual Ptr<GenericDescriptorMatcher> clone( bool emptyTrainData=false ) const;
2737
2738 protected:
2739     virtual void knnMatchImpl( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
2740                               vector<vector<DMatch> >& matches, int k,
2741                               const vector<Mat>& masks, bool compactResult );
2742     virtual void radiusMatchImpl( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
2743                                  vector<vector<DMatch> >& matches, float maxDistance,
2744                                  const vector<Mat>& masks, bool compactResult );
2745
2746     void trainFernClassifier();
2747     void calcBestProbAndMatchIdx( const Mat& image, const Point2f& pt,
2748                                  float& bestProb, int& bestMatchIdx, vector<float>& signature );
2749     Ptr<FernClassifier> classifier;
2750     Params params;
2751     int prevTrainCount;
2752 };
2753
2754
2755 /*
2756  * CalonderDescriptorExtractor
2757  */
2758 template<typename T>
2759 class CV_EXPORTS CalonderDescriptorExtractor : public DescriptorExtractor
2760 {
2761 public:
2762     CalonderDescriptorExtractor( const string& classifierFile );
2763
2764     virtual void read( const FileNode &fn );
2765     virtual void write( FileStorage &fs ) const;
2766
2767     virtual int descriptorSize() const { return classifier_.classes(); }
2768     virtual int descriptorType() const { return DataType<T>::type; }
2769
2770     virtual bool empty() const;
2771
2772 protected:
2773     virtual void computeImpl( const Mat& image, vector<KeyPoint>& keypoints, Mat& descriptors ) const;
2774
2775     RTreeClassifier classifier_;
2776     static const int BORDER_SIZE = 16;
2777 };
2778
2779 template<typename T>
2780 CalonderDescriptorExtractor<T>::CalonderDescriptorExtractor(const std::string& classifier_file)
2781 {
2782     classifier_.read( classifier_file.c_str() );
2783 }
2784
2785 template<typename T>
2786 void CalonderDescriptorExtractor<T>::computeImpl( const Mat& image,
2787                                                  vector<KeyPoint>& keypoints,
2788                                                  Mat& descriptors) const
2789 {
2790     // Cannot compute descriptors for keypoints on the image border.
2791     KeyPointsFilter::runByImageBorder(keypoints, image.size(), BORDER_SIZE);
2792
2793     /// @todo Check 16-byte aligned
2794     descriptors.create((int)keypoints.size(), classifier_.classes(), cv::DataType<T>::type);
2795
2796     int patchSize = RandomizedTree::PATCH_SIZE;
2797     int offset = patchSize / 2;
2798     for (size_t i = 0; i < keypoints.size(); ++i)
2799     {
2800         cv::Point2f pt = keypoints[i].pt;
2801         IplImage ipl = image( Rect((int)(pt.x - offset), (int)(pt.y - offset), patchSize, patchSize) );
2802         classifier_.getSignature( &ipl, descriptors.ptr<T>((int)i));
2803     }
2804 }
2805
2806 template<typename T>
2807 void CalonderDescriptorExtractor<T>::read( const FileNode& )
2808 {}
2809
2810 template<typename T>
2811 void CalonderDescriptorExtractor<T>::write( FileStorage& ) const
2812 {}
2813
2814 template<typename T>
2815 bool CalonderDescriptorExtractor<T>::empty() const
2816 {
2817     return classifier_.trees_.empty();
2818 }
2819
2820
2821 ////////////////////// Brute Force Matcher //////////////////////////
2822
2823 template<class Distance>
2824 class CV_EXPORTS BruteForceMatcher : public BFMatcher
2825 {
2826 public:
2827     BruteForceMatcher( Distance d = Distance() ) : BFMatcher(Distance::normType, false) {(void)d;}
2828     virtual ~BruteForceMatcher() {}
2829 };
2830
2831
2832 /****************************************************************************************\
2833 *                                Planar Object Detection                                 *
2834 \****************************************************************************************/
2835
2836 class CV_EXPORTS PlanarObjectDetector
2837 {
2838 public:
2839     PlanarObjectDetector();
2840     PlanarObjectDetector(const FileNode& node);
2841     PlanarObjectDetector(const vector<Mat>& pyr, int _npoints=300,
2842                          int _patchSize=FernClassifier::PATCH_SIZE,
2843                          int _nstructs=FernClassifier::DEFAULT_STRUCTS,
2844                          int _structSize=FernClassifier::DEFAULT_STRUCT_SIZE,
2845                          int _nviews=FernClassifier::DEFAULT_VIEWS,
2846                          const LDetector& detector=LDetector(),
2847                          const PatchGenerator& patchGenerator=PatchGenerator());
2848     virtual ~PlanarObjectDetector();
2849     virtual void train(const vector<Mat>& pyr, int _npoints=300,
2850                        int _patchSize=FernClassifier::PATCH_SIZE,
2851                        int _nstructs=FernClassifier::DEFAULT_STRUCTS,
2852                        int _structSize=FernClassifier::DEFAULT_STRUCT_SIZE,
2853                        int _nviews=FernClassifier::DEFAULT_VIEWS,
2854                        const LDetector& detector=LDetector(),
2855                        const PatchGenerator& patchGenerator=PatchGenerator());
2856     virtual void train(const vector<Mat>& pyr, const vector<KeyPoint>& keypoints,
2857                        int _patchSize=FernClassifier::PATCH_SIZE,
2858                        int _nstructs=FernClassifier::DEFAULT_STRUCTS,
2859                        int _structSize=FernClassifier::DEFAULT_STRUCT_SIZE,
2860                        int _nviews=FernClassifier::DEFAULT_VIEWS,
2861                        const LDetector& detector=LDetector(),
2862                        const PatchGenerator& patchGenerator=PatchGenerator());
2863     Rect getModelROI() const;
2864     vector<KeyPoint> getModelPoints() const;
2865     const LDetector& getDetector() const;
2866     const FernClassifier& getClassifier() const;
2867     void setVerbose(bool verbose);
2868
2869     void read(const FileNode& node);
2870     void write(FileStorage& fs, const String& name=String()) const;
2871     bool operator()(const Mat& image, CV_OUT Mat& H, CV_OUT vector<Point2f>& corners) const;
2872     bool operator()(const vector<Mat>& pyr, const vector<KeyPoint>& keypoints,
2873                     CV_OUT Mat& H, CV_OUT vector<Point2f>& corners,
2874                     CV_OUT vector<int>* pairs=0) const;
2875
2876 protected:
2877     bool verbose;
2878     Rect modelROI;
2879     vector<KeyPoint> modelPoints;
2880     LDetector ldetector;
2881     FernClassifier fernClassifier;
2882 };
2883
2884 }
2885
2886 // 2009-01-12, Xavier Delacour <xavier.delacour@gmail.com>
2887
2888 struct lsh_hash {
2889     int h1, h2;
2890 };
2891
2892 struct CvLSHOperations
2893 {
2894     virtual ~CvLSHOperations() {}
2895
2896     virtual int vector_add(const void* data) = 0;
2897     virtual void vector_remove(int i) = 0;
2898     virtual const void* vector_lookup(int i) = 0;
2899     virtual void vector_reserve(int n) = 0;
2900     virtual unsigned int vector_count() = 0;
2901
2902     virtual void hash_insert(lsh_hash h, int l, int i) = 0;
2903     virtual void hash_remove(lsh_hash h, int l, int i) = 0;
2904     virtual int hash_lookup(lsh_hash h, int l, int* ret_i, int ret_i_max) = 0;
2905 };
2906
2907 #endif
2908
2909 #ifdef __cplusplus
2910 extern "C" {
2911 #endif
2912
2913 /* Splits color or grayscale image into multiple connected components
2914  of nearly the same color/brightness using modification of Burt algorithm.
2915  comp with contain a pointer to sequence (CvSeq)
2916  of connected components (CvConnectedComp) */
2917 CVAPI(void) cvPyrSegmentation( IplImage* src, IplImage* dst,
2918                               CvMemStorage* storage, CvSeq** comp,
2919                               int level, double threshold1,
2920                               double threshold2 );
2921
2922 /****************************************************************************************\
2923 *                              Planar subdivisions                                       *
2924 \****************************************************************************************/
2925
2926 /* Initializes Delaunay triangulation */
2927 CVAPI(void)  cvInitSubdivDelaunay2D( CvSubdiv2D* subdiv, CvRect rect );
2928
2929 /* Creates new subdivision */
2930 CVAPI(CvSubdiv2D*)  cvCreateSubdiv2D( int subdiv_type, int header_size,
2931                                      int vtx_size, int quadedge_size,
2932                                      CvMemStorage* storage );
2933
2934 /************************* high-level subdivision functions ***************************/
2935
2936 /* Simplified Delaunay diagram creation */
2937 CV_INLINE  CvSubdiv2D* cvCreateSubdivDelaunay2D( CvRect rect, CvMemStorage* storage )
2938 {
2939     CvSubdiv2D* subdiv = cvCreateSubdiv2D( CV_SEQ_KIND_SUBDIV2D, sizeof(*subdiv),
2940                                           sizeof(CvSubdiv2DPoint), sizeof(CvQuadEdge2D), storage );
2941
2942     cvInitSubdivDelaunay2D( subdiv, rect );
2943     return subdiv;
2944 }
2945
2946
2947 /* Inserts new point to the Delaunay triangulation */
2948 CVAPI(CvSubdiv2DPoint*)  cvSubdivDelaunay2DInsert( CvSubdiv2D* subdiv, CvPoint2D32f pt);
2949
2950 /* Locates a point within the Delaunay triangulation (finds the edge
2951  the point is left to or belongs to, or the triangulation point the given
2952  point coinsides with */
2953 CVAPI(CvSubdiv2DPointLocation)  cvSubdiv2DLocate(
2954                                                  CvSubdiv2D* subdiv, CvPoint2D32f pt,
2955                                                  CvSubdiv2DEdge* edge,
2956                                                  CvSubdiv2DPoint** vertex CV_DEFAULT(NULL) );
2957
2958 /* Calculates Voronoi tesselation (i.e. coordinates of Voronoi points) */
2959 CVAPI(void)  cvCalcSubdivVoronoi2D( CvSubdiv2D* subdiv );
2960
2961
2962 /* Removes all Voronoi points from the tesselation */
2963 CVAPI(void)  cvClearSubdivVoronoi2D( CvSubdiv2D* subdiv );
2964
2965
2966 /* Finds the nearest to the given point vertex in subdivision. */
2967 CVAPI(CvSubdiv2DPoint*) cvFindNearestPoint2D( CvSubdiv2D* subdiv, CvPoint2D32f pt );
2968
2969
2970 /************ Basic quad-edge navigation and operations ************/
2971
2972 CV_INLINE  CvSubdiv2DEdge  cvSubdiv2DNextEdge( CvSubdiv2DEdge edge )
2973 {
2974     return  CV_SUBDIV2D_NEXT_EDGE(edge);
2975 }
2976
2977
2978 CV_INLINE  CvSubdiv2DEdge  cvSubdiv2DRotateEdge( CvSubdiv2DEdge edge, int rotate )
2979 {
2980     return  (edge & ~3) + ((edge + rotate) & 3);
2981 }
2982
2983 CV_INLINE  CvSubdiv2DEdge  cvSubdiv2DSymEdge( CvSubdiv2DEdge edge )
2984 {
2985     return edge ^ 2;
2986 }
2987
2988 CV_INLINE  CvSubdiv2DEdge  cvSubdiv2DGetEdge( CvSubdiv2DEdge edge, CvNextEdgeType type )
2989 {
2990     CvQuadEdge2D* e = (CvQuadEdge2D*)(edge & ~3);
2991     edge = e->next[(edge + (int)type) & 3];
2992     return  (edge & ~3) + ((edge + ((int)type >> 4)) & 3);
2993 }
2994
2995
2996 CV_INLINE  CvSubdiv2DPoint*  cvSubdiv2DEdgeOrg( CvSubdiv2DEdge edge )
2997 {
2998     CvQuadEdge2D* e = (CvQuadEdge2D*)(edge & ~3);
2999     return (CvSubdiv2DPoint*)e->pt[edge & 3];
3000 }
3001
3002
3003 CV_INLINE  CvSubdiv2DPoint*  cvSubdiv2DEdgeDst( CvSubdiv2DEdge edge )
3004 {
3005     CvQuadEdge2D* e = (CvQuadEdge2D*)(edge & ~3);
3006     return (CvSubdiv2DPoint*)e->pt[(edge + 2) & 3];
3007 }
3008
3009 /****************************************************************************************\
3010 *                           Additional operations on Subdivisions                        *
3011 \****************************************************************************************/
3012
3013 // paints voronoi diagram: just demo function
3014 CVAPI(void)  icvDrawMosaic( CvSubdiv2D* subdiv, IplImage* src, IplImage* dst );
3015
3016 // checks planar subdivision for correctness. It is not an absolute check,
3017 // but it verifies some relations between quad-edges
3018 CVAPI(int)   icvSubdiv2DCheck( CvSubdiv2D* subdiv );
3019
3020 // returns squared distance between two 2D points with floating-point coordinates.
3021 CV_INLINE double icvSqDist2D32f( CvPoint2D32f pt1, CvPoint2D32f pt2 )
3022 {
3023     double dx = pt1.x - pt2.x;
3024     double dy = pt1.y - pt2.y;
3025
3026     return dx*dx + dy*dy;
3027 }
3028
3029
3030
3031
3032 CV_INLINE  double  cvTriangleArea( CvPoint2D32f a, CvPoint2D32f b, CvPoint2D32f c )
3033 {
3034     return ((double)b.x - a.x) * ((double)c.y - a.y) - ((double)b.y - a.y) * ((double)c.x - a.x);
3035 }
3036
3037
3038 /* Constructs kd-tree from set of feature descriptors */
3039 CVAPI(struct CvFeatureTree*) cvCreateKDTree(CvMat* desc);
3040
3041 /* Constructs spill-tree from set of feature descriptors */
3042 CVAPI(struct CvFeatureTree*) cvCreateSpillTree( const CvMat* raw_data,
3043                                                const int naive CV_DEFAULT(50),
3044                                                const double rho CV_DEFAULT(.7),
3045                                                const double tau CV_DEFAULT(.1) );
3046
3047 /* Release feature tree */
3048 CVAPI(void) cvReleaseFeatureTree(struct CvFeatureTree* tr);
3049
3050 /* Searches feature tree for k nearest neighbors of given reference points,
3051  searching (in case of kd-tree/bbf) at most emax leaves. */
3052 CVAPI(void) cvFindFeatures(struct CvFeatureTree* tr, const CvMat* query_points,
3053                            CvMat* indices, CvMat* dist, int k, int emax CV_DEFAULT(20));
3054
3055 /* Search feature tree for all points that are inlier to given rect region.
3056  Only implemented for kd trees */
3057 CVAPI(int) cvFindFeaturesBoxed(struct CvFeatureTree* tr,
3058                                CvMat* bounds_min, CvMat* bounds_max,
3059                                CvMat* out_indices);