]> rtime.felk.cvut.cz Git - opencv.git/blob - opencv/src/cv/cvfundam.cpp
fixed output mask in cvFindHomography (thanks to robozyt, ticket #236)
[opencv.git] / opencv / src / cv / cvfundam.cpp
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 #include "_cv.h"
43 #include "_cvmodelest.h"
44
45 using namespace cv;
46
47 template<typename T> int icvCompressPoints( T* ptr, const uchar* mask, int mstep, int count )
48 {
49     int i, j;
50     for( i = j = 0; i < count; i++ )
51         if( mask[i*mstep] )
52         {
53             if( i > j )
54                 ptr[j] = ptr[i];
55             j++;
56         }
57     return j;
58 }
59
60 class CvHomographyEstimator : public CvModelEstimator2
61 {
62 public:
63     CvHomographyEstimator( int modelPoints );
64
65     virtual int runKernel( const CvMat* m1, const CvMat* m2, CvMat* model );
66     virtual bool refine( const CvMat* m1, const CvMat* m2,
67                          CvMat* model, int maxIters );
68 protected:
69     virtual void computeReprojError( const CvMat* m1, const CvMat* m2,
70                                      const CvMat* model, CvMat* error );
71 };
72
73
74 CvHomographyEstimator::CvHomographyEstimator(int _modelPoints)
75     : CvModelEstimator2(_modelPoints, cvSize(3,3), 1)
76 {
77     assert( _modelPoints == 4 || _modelPoints == 5 );
78     checkPartialSubsets = false;
79 }
80
81 int CvHomographyEstimator::runKernel( const CvMat* m1, const CvMat* m2, CvMat* H )
82 {
83     int i, count = m1->rows*m1->cols;
84     const CvPoint2D64f* M = (const CvPoint2D64f*)m1->data.ptr;
85     const CvPoint2D64f* m = (const CvPoint2D64f*)m2->data.ptr;
86
87     double LtL[9][9], W[9][9], V[9][9];
88     CvMat _LtL = cvMat( 9, 9, CV_64F, LtL );
89     CvMat matW = cvMat( 9, 9, CV_64F, W );
90     CvMat matV = cvMat( 9, 9, CV_64F, V );
91     CvMat _H0 = cvMat( 3, 3, CV_64F, V[8] );
92     CvMat _Htemp = cvMat( 3, 3, CV_64F, V[7] );
93     CvPoint2D64f cM={0,0}, cm={0,0}, sM={0,0}, sm={0,0};
94
95     for( i = 0; i < count; i++ )
96     {
97         cm.x += m[i].x; cm.y += m[i].y;
98         cM.x += M[i].x; cM.y += M[i].y;
99     }
100
101     cm.x /= count; cm.y /= count;
102     cM.x /= count; cM.y /= count;
103
104     for( i = 0; i < count; i++ )
105     {
106         sm.x += fabs(m[i].x - cm.x);
107         sm.y += fabs(m[i].y - cm.y);
108         sM.x += fabs(M[i].x - cM.x);
109         sM.y += fabs(M[i].y - cM.y);
110     }
111
112     if( fabs(sm.x) < DBL_EPSILON || fabs(sm.y) < DBL_EPSILON ||
113         fabs(sM.x) < DBL_EPSILON || fabs(sM.y) < DBL_EPSILON )
114         return 0;
115     sm.x = count/sm.x; sm.y = count/sm.y;
116     sM.x = count/sM.x; sM.y = count/sM.y;
117
118     double invHnorm[9] = { 1./sm.x, 0, cm.x, 0, 1./sm.y, cm.y, 0, 0, 1 };
119     double Hnorm2[9] = { sM.x, 0, -cM.x*sM.x, 0, sM.y, -cM.y*sM.y, 0, 0, 1 };
120     CvMat _invHnorm = cvMat( 3, 3, CV_64FC1, invHnorm );
121     CvMat _Hnorm2 = cvMat( 3, 3, CV_64FC1, Hnorm2 );
122
123     cvZero( &_LtL );
124     for( i = 0; i < count; i++ )
125     {
126         double x = (m[i].x - cm.x)*sm.x, y = (m[i].y - cm.y)*sm.y;
127         double X = (M[i].x - cM.x)*sM.x, Y = (M[i].y - cM.y)*sM.y;
128         double Lx[] = { X, Y, 1, 0, 0, 0, -x*X, -x*Y, -x };
129         double Ly[] = { 0, 0, 0, X, Y, 1, -y*X, -y*Y, -y };
130         int j, k;
131         for( j = 0; j < 9; j++ )
132             for( k = j; k < 9; k++ )
133                 LtL[j][k] += Lx[j]*Lx[k] + Ly[j]*Ly[k];
134     }
135     cvCompleteSymm( &_LtL );
136
137     //cvSVD( &_LtL, &matW, 0, &matV, CV_SVD_MODIFY_A + CV_SVD_V_T );
138     cvEigenVV( &_LtL, &matV, &matW );
139     cvMatMul( &_invHnorm, &_H0, &_Htemp );
140     cvMatMul( &_Htemp, &_Hnorm2, &_H0 );
141     cvConvertScale( &_H0, H, 1./_H0.data.db[8] );
142
143     return 1;
144 }
145
146
147 void CvHomographyEstimator::computeReprojError( const CvMat* m1, const CvMat* m2,
148                                                 const CvMat* model, CvMat* _err )
149 {
150     int i, count = m1->rows*m1->cols;
151     const CvPoint2D64f* M = (const CvPoint2D64f*)m1->data.ptr;
152     const CvPoint2D64f* m = (const CvPoint2D64f*)m2->data.ptr;
153     const double* H = model->data.db;
154     float* err = _err->data.fl;
155
156     for( i = 0; i < count; i++ )
157     {
158         double ww = 1./(H[6]*M[i].x + H[7]*M[i].y + 1.);
159         double dx = (H[0]*M[i].x + H[1]*M[i].y + H[2])*ww - m[i].x;
160         double dy = (H[3]*M[i].x + H[4]*M[i].y + H[5])*ww - m[i].y;
161         err[i] = (float)(dx*dx + dy*dy);
162     }
163 }
164
165 bool CvHomographyEstimator::refine( const CvMat* m1, const CvMat* m2, CvMat* model, int maxIters )
166 {
167     CvLevMarq solver(8, 0, cvTermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, maxIters, DBL_EPSILON));
168     int i, j, k, count = m1->rows*m1->cols;
169     const CvPoint2D64f* M = (const CvPoint2D64f*)m1->data.ptr;
170     const CvPoint2D64f* m = (const CvPoint2D64f*)m2->data.ptr;
171     CvMat modelPart = cvMat( solver.param->rows, solver.param->cols, model->type, model->data.ptr );
172     cvCopy( &modelPart, solver.param );
173
174     for(;;)
175     {
176         const CvMat* _param = 0;
177         CvMat *_JtJ = 0, *_JtErr = 0;
178         double* _errNorm = 0;
179
180         if( !solver.updateAlt( _param, _JtJ, _JtErr, _errNorm ))
181             break;
182
183         for( i = 0; i < count; i++ )
184         {
185             const double* h = _param->data.db;
186             double Mx = M[i].x, My = M[i].y;
187             double ww = 1./(h[6]*Mx + h[7]*My + 1.);
188             double _xi = (h[0]*Mx + h[1]*My + h[2])*ww;
189             double _yi = (h[3]*Mx + h[4]*My + h[5])*ww;
190             double err[] = { _xi - m[i].x, _yi - m[i].y };
191             if( _JtJ || _JtErr )
192             {
193                 double J[][8] =
194                 {
195                     { Mx*ww, My*ww, ww, 0, 0, 0, -Mx*ww*_xi, -My*ww*_xi },
196                     { 0, 0, 0, Mx*ww, My*ww, ww, -Mx*ww*_yi, -My*ww*_yi }
197                 };
198
199                 for( j = 0; j < 8; j++ )
200                 {
201                     for( k = j; k < 8; k++ )
202                         _JtJ->data.db[j*8+k] += J[0][j]*J[0][k] + J[1][j]*J[1][k];
203                     _JtErr->data.db[j] += J[0][j]*err[0] + J[1][j]*err[1];
204                 }
205             }
206             if( _errNorm )
207                 *_errNorm += err[0]*err[0] + err[1]*err[1];
208         }
209     }
210
211     cvCopy( solver.param, &modelPart );
212     return true;
213 }
214
215
216 CV_IMPL int
217 cvFindHomography( const CvMat* objectPoints, const CvMat* imagePoints,
218                   CvMat* __H, int method, double ransacReprojThreshold,
219                   CvMat* mask )
220 {
221     const double confidence = 0.995;
222     const int maxIters = 2000;
223     bool result = false;
224     Ptr<CvMat> m, M, tempMask;
225
226     double H[9];
227     CvMat matH = cvMat( 3, 3, CV_64FC1, H );
228     int count;
229
230     CV_Assert( CV_IS_MAT(imagePoints) && CV_IS_MAT(objectPoints) );
231
232     count = MAX(imagePoints->cols, imagePoints->rows);
233     CV_Assert( count >= 4 );
234
235     m = cvCreateMat( 1, count, CV_64FC2 );
236     cvConvertPointsHomogeneous( imagePoints, m );
237
238     M = cvCreateMat( 1, count, CV_64FC2 );
239     cvConvertPointsHomogeneous( objectPoints, M );
240
241     if( mask )
242     {
243         CV_Assert( CV_IS_MASK_ARR(mask) && CV_IS_MAT_CONT(mask->type) &&
244             (mask->rows == 1 || mask->cols == 1) &&
245             mask->rows*mask->cols == count );
246         tempMask = cvCloneMat(mask);
247     }
248     else if( count > 4 )
249         tempMask = cvCreateMat( 1, count, CV_8U );
250     if( !tempMask.empty() )
251         cvSet( tempMask, cvScalarAll(1.) );
252
253     CvHomographyEstimator estimator( MIN(count, 4) );
254     if( count == 4 )
255         method = 0;
256     if( method == CV_LMEDS )
257         result = estimator.runLMeDS( M, m, &matH, tempMask, confidence, maxIters );
258     else if( method == CV_RANSAC )
259         result = estimator.runRANSAC( M, m, &matH, tempMask, ransacReprojThreshold, confidence, maxIters);
260     else
261         result = estimator.runKernel( M, m, &matH ) > 0;
262
263     if( result && count > 4 )
264     {
265         icvCompressPoints( (CvPoint2D64f*)M->data.ptr, tempMask->data.ptr, 1, count );
266         count = icvCompressPoints( (CvPoint2D64f*)m->data.ptr, tempMask->data.ptr, 1, count );
267         M->cols = m->cols = count;
268         estimator.refine( M, m, &matH, 10 );
269     }
270
271     if( result )
272         cvConvert( &matH, __H );
273     
274     if( mask && tempMask )
275         cvCopy( tempMask, mask );
276
277     return (int)result;
278 }
279
280
281 /* Evaluation of Fundamental Matrix from point correspondences.
282    The original code has been written by Valery Mosyagin */
283
284 /* The algorithms (except for RANSAC) and the notation have been taken from
285    Zhengyou Zhang's research report
286    "Determining the Epipolar Geometry and its Uncertainty: A Review"
287    that can be found at http://www-sop.inria.fr/robotvis/personnel/zzhang/zzhang-eng.html */
288
289 /************************************** 7-point algorithm *******************************/
290 class CvFMEstimator : public CvModelEstimator2
291 {
292 public:
293     CvFMEstimator( int _modelPoints );
294
295     virtual int runKernel( const CvMat* m1, const CvMat* m2, CvMat* model );
296     virtual int run7Point( const CvMat* m1, const CvMat* m2, CvMat* model );
297     virtual int run8Point( const CvMat* m1, const CvMat* m2, CvMat* model );
298 protected:
299     virtual void computeReprojError( const CvMat* m1, const CvMat* m2,
300                                      const CvMat* model, CvMat* error );
301 };
302
303 CvFMEstimator::CvFMEstimator( int _modelPoints )
304 : CvModelEstimator2( _modelPoints, cvSize(3,3), _modelPoints == 7 ? 3 : 1 )
305 {
306     assert( _modelPoints == 7 || _modelPoints == 8 );
307 }
308
309
310 int CvFMEstimator::runKernel( const CvMat* m1, const CvMat* m2, CvMat* model )
311 {
312     return modelPoints == 7 ? run7Point( m1, m2, model ) : run8Point( m1, m2, model );
313 }
314
315 int CvFMEstimator::run7Point( const CvMat* _m1, const CvMat* _m2, CvMat* _fmatrix )
316 {
317     double a[7*9], w[7], v[9*9], c[4], r[3];
318     double* f1, *f2;
319     double t0, t1, t2;
320     CvMat A = cvMat( 7, 9, CV_64F, a );
321     CvMat V = cvMat( 9, 9, CV_64F, v );
322     CvMat W = cvMat( 7, 1, CV_64F, w );
323     CvMat coeffs = cvMat( 1, 4, CV_64F, c );
324     CvMat roots = cvMat( 1, 3, CV_64F, r );
325     const CvPoint2D64f* m1 = (const CvPoint2D64f*)_m1->data.ptr;
326     const CvPoint2D64f* m2 = (const CvPoint2D64f*)_m2->data.ptr;
327     double* fmatrix = _fmatrix->data.db;
328     int i, k, n;
329
330     // form a linear system: i-th row of A(=a) represents
331     // the equation: (m2[i], 1)'*F*(m1[i], 1) = 0
332     for( i = 0; i < 7; i++ )
333     {
334         double x0 = m1[i].x, y0 = m1[i].y;
335         double x1 = m2[i].x, y1 = m2[i].y;
336
337         a[i*9+0] = x1*x0;
338         a[i*9+1] = x1*y0;
339         a[i*9+2] = x1;
340         a[i*9+3] = y1*x0;
341         a[i*9+4] = y1*y0;
342         a[i*9+5] = y1;
343         a[i*9+6] = x0;
344         a[i*9+7] = y0;
345         a[i*9+8] = 1;
346     }
347
348     // A*(f11 f12 ... f33)' = 0 is singular (7 equations for 9 variables), so
349     // the solution is linear subspace of dimensionality 2.
350     // => use the last two singular vectors as a basis of the space
351     // (according to SVD properties)
352     cvSVD( &A, &W, 0, &V, CV_SVD_MODIFY_A + CV_SVD_V_T );
353     f1 = v + 7*9;
354     f2 = v + 8*9;
355
356     // f1, f2 is a basis => lambda*f1 + mu*f2 is an arbitrary f. matrix.
357     // as it is determined up to a scale, normalize lambda & mu (lambda + mu = 1),
358     // so f ~ lambda*f1 + (1 - lambda)*f2.
359     // use the additional constraint det(f) = det(lambda*f1 + (1-lambda)*f2) to find lambda.
360     // it will be a cubic equation.
361     // find c - polynomial coefficients.
362     for( i = 0; i < 9; i++ )
363         f1[i] -= f2[i];
364
365     t0 = f2[4]*f2[8] - f2[5]*f2[7];
366     t1 = f2[3]*f2[8] - f2[5]*f2[6];
367     t2 = f2[3]*f2[7] - f2[4]*f2[6];
368
369     c[3] = f2[0]*t0 - f2[1]*t1 + f2[2]*t2;
370
371     c[2] = f1[0]*t0 - f1[1]*t1 + f1[2]*t2 -
372            f1[3]*(f2[1]*f2[8] - f2[2]*f2[7]) +
373            f1[4]*(f2[0]*f2[8] - f2[2]*f2[6]) -
374            f1[5]*(f2[0]*f2[7] - f2[1]*f2[6]) +
375            f1[6]*(f2[1]*f2[5] - f2[2]*f2[4]) -
376            f1[7]*(f2[0]*f2[5] - f2[2]*f2[3]) +
377            f1[8]*(f2[0]*f2[4] - f2[1]*f2[3]);
378
379     t0 = f1[4]*f1[8] - f1[5]*f1[7];
380     t1 = f1[3]*f1[8] - f1[5]*f1[6];
381     t2 = f1[3]*f1[7] - f1[4]*f1[6];
382
383     c[1] = f2[0]*t0 - f2[1]*t1 + f2[2]*t2 -
384            f2[3]*(f1[1]*f1[8] - f1[2]*f1[7]) +
385            f2[4]*(f1[0]*f1[8] - f1[2]*f1[6]) -
386            f2[5]*(f1[0]*f1[7] - f1[1]*f1[6]) +
387            f2[6]*(f1[1]*f1[5] - f1[2]*f1[4]) -
388            f2[7]*(f1[0]*f1[5] - f1[2]*f1[3]) +
389            f2[8]*(f1[0]*f1[4] - f1[1]*f1[3]);
390
391     c[0] = f1[0]*t0 - f1[1]*t1 + f1[2]*t2;
392
393     // solve the cubic equation; there can be 1 to 3 roots ...
394     n = cvSolveCubic( &coeffs, &roots );
395
396     if( n < 1 || n > 3 )
397         return n;
398
399     for( k = 0; k < n; k++, fmatrix += 9 )
400     {
401         // for each root form the fundamental matrix
402         double lambda = r[k], mu = 1.;
403         double s = f1[8]*r[k] + f2[8];
404
405         // normalize each matrix, so that F(3,3) (~fmatrix[8]) == 1
406         if( fabs(s) > DBL_EPSILON )
407         {
408             mu = 1./s;
409             lambda *= mu;
410             fmatrix[8] = 1.;
411         }
412         else
413             fmatrix[8] = 0.;
414
415         for( i = 0; i < 8; i++ )
416             fmatrix[i] = f1[i]*lambda + f2[i]*mu;
417     }
418
419     return n;
420 }
421
422
423 int CvFMEstimator::run8Point( const CvMat* _m1, const CvMat* _m2, CvMat* _fmatrix )
424 {
425     double a[9*9], w[9], v[9*9];
426     CvMat W = cvMat( 1, 9, CV_64F, w );
427     CvMat V = cvMat( 9, 9, CV_64F, v );
428     CvMat A = cvMat( 9, 9, CV_64F, a );
429     CvMat U, F0, TF;
430
431     CvPoint2D64f m0c = {0,0}, m1c = {0,0};
432     double t, scale0 = 0, scale1 = 0;
433
434     const CvPoint2D64f* m1 = (const CvPoint2D64f*)_m1->data.ptr;
435     const CvPoint2D64f* m2 = (const CvPoint2D64f*)_m2->data.ptr;
436     double* fmatrix = _fmatrix->data.db;
437     int i, j, k, count = _m1->cols*_m1->rows;
438
439     // compute centers and average distances for each of the two point sets
440     for( i = 0; i < count; i++ )
441     {
442         double x = m1[i].x, y = m1[i].y;
443         m0c.x += x; m0c.y += y;
444
445         x = m2[i].x, y = m2[i].y;
446         m1c.x += x; m1c.y += y;
447     }
448
449     // calculate the normalizing transformations for each of the point sets:
450     // after the transformation each set will have the mass center at the coordinate origin
451     // and the average distance from the origin will be ~sqrt(2).
452     t = 1./count;
453     m0c.x *= t; m0c.y *= t;
454     m1c.x *= t; m1c.y *= t;
455
456     for( i = 0; i < count; i++ )
457     {
458         double x = m1[i].x - m0c.x, y = m1[i].y - m0c.y;
459         scale0 += sqrt(x*x + y*y);
460
461         x = fabs(m2[i].x - m1c.x), y = fabs(m2[i].y - m1c.y);
462         scale1 += sqrt(x*x + y*y);
463     }
464
465     scale0 *= t;
466     scale1 *= t;
467
468     if( scale0 < FLT_EPSILON || scale1 < FLT_EPSILON )
469         return 0;
470
471     scale0 = sqrt(2.)/scale0;
472     scale1 = sqrt(2.)/scale1;
473     
474     cvZero( &A );
475
476     // form a linear system Ax=0: for each selected pair of points m1 & m2,
477     // the row of A(=a) represents the coefficients of equation: (m2, 1)'*F*(m1, 1) = 0
478     // to save computation time, we compute (At*A) instead of A and then solve (At*A)x=0. 
479     for( i = 0; i < count; i++ )
480     {
481         double x0 = (m1[i].x - m0c.x)*scale0;
482         double y0 = (m1[i].y - m0c.y)*scale0;
483         double x1 = (m2[i].x - m1c.x)*scale1;
484         double y1 = (m2[i].y - m1c.y)*scale1;
485         double r[9] = { x1*x0, x1*y0, x1, y1*x0, y1*y0, y1, x0, y0, 1 };
486         for( j = 0; j < 9; j++ )
487             for( k = 0; k < 9; k++ )
488                 a[j*9+k] += r[j]*r[k];
489     }
490
491     cvSVD( &A, &W, 0, &V, CV_SVD_MODIFY_A + CV_SVD_V_T );
492
493     for( i = 0; i < 8; i++ )
494     {
495         if( fabs(w[i]) < DBL_EPSILON )
496             break;
497     }
498
499     if( i < 7 )
500         return 0;
501
502     F0 = cvMat( 3, 3, CV_64F, v + 9*8 ); // take the last column of v as a solution of Af = 0
503
504     // make F0 singular (of rank 2) by decomposing it with SVD,
505     // zeroing the last diagonal element of W and then composing the matrices back.
506
507     // use v as a temporary storage for different 3x3 matrices
508     W = U = V = TF = F0;
509     W.data.db = v;
510     U.data.db = v + 9;
511     V.data.db = v + 18;
512     TF.data.db = v + 27;
513
514     cvSVD( &F0, &W, &U, &V, CV_SVD_MODIFY_A + CV_SVD_U_T + CV_SVD_V_T );
515     W.data.db[8] = 0.;
516
517     // F0 <- U*diag([W(1), W(2), 0])*V'
518     cvGEMM( &U, &W, 1., 0, 0., &TF, CV_GEMM_A_T );
519     cvGEMM( &TF, &V, 1., 0, 0., &F0, 0/*CV_GEMM_B_T*/ );
520
521     // apply the transformation that is inverse
522     // to what we used to normalize the point coordinates
523     {
524         double tt0[] = { scale0, 0, -scale0*m0c.x, 0, scale0, -scale0*m0c.y, 0, 0, 1 };
525         double tt1[] = { scale1, 0, -scale1*m1c.x, 0, scale1, -scale1*m1c.y, 0, 0, 1 };
526         CvMat T0, T1;
527         T0 = T1 = F0;
528         T0.data.db = tt0;
529         T1.data.db = tt1;
530
531         // F0 <- T1'*F0*T0
532         cvGEMM( &T1, &F0, 1., 0, 0., &TF, CV_GEMM_A_T );
533         F0.data.db = fmatrix;
534         cvGEMM( &TF, &T0, 1., 0, 0., &F0, 0 );
535
536         // make F(3,3) = 1
537         if( fabs(F0.data.db[8]) > FLT_EPSILON )
538             cvScale( &F0, &F0, 1./F0.data.db[8] );
539     }
540
541     return 1;
542 }
543
544
545 void CvFMEstimator::computeReprojError( const CvMat* _m1, const CvMat* _m2,
546                                         const CvMat* model, CvMat* _err )
547 {
548     int i, count = _m1->rows*_m1->cols;
549     const CvPoint2D64f* m1 = (const CvPoint2D64f*)_m1->data.ptr;
550     const CvPoint2D64f* m2 = (const CvPoint2D64f*)_m2->data.ptr;
551     const double* F = model->data.db;
552     float* err = _err->data.fl;
553     
554     for( i = 0; i < count; i++ )
555     {
556         double a, b, c, d1, d2, s1, s2;
557
558         a = F[0]*m1[i].x + F[1]*m1[i].y + F[2];
559         b = F[3]*m1[i].x + F[4]*m1[i].y + F[5];
560         c = F[6]*m1[i].x + F[7]*m1[i].y + F[8];
561
562         s2 = 1./(a*a + b*b);
563         d2 = m2[i].x*a + m2[i].y*b + c;
564
565         a = F[0]*m2[i].x + F[3]*m2[i].y + F[6];
566         b = F[1]*m2[i].x + F[4]*m2[i].y + F[7];
567         c = F[2]*m2[i].x + F[5]*m2[i].y + F[8];
568
569         s1 = 1./(a*a + b*b);
570         d1 = m1[i].x*a + m1[i].y*b + c;
571
572         err[i] = (float)std::max(d1*d1*s1, d2*d2*s2);
573     }
574 }
575
576
577 CV_IMPL int cvFindFundamentalMat( const CvMat* points1, const CvMat* points2,
578                                   CvMat* fmatrix, int method,
579                                   double param1, double param2, CvMat* mask )
580 {
581     int result = 0;
582     Ptr<CvMat> m1, m2, tempMask;
583
584     double F[3*9];
585     CvMat _F3x3 = cvMat( 3, 3, CV_64FC1, F ), _F9x3 = cvMat( 9, 3, CV_64FC1, F );
586     int count;
587
588     CV_Assert( CV_IS_MAT(points1) && CV_IS_MAT(points2) && CV_ARE_SIZES_EQ(points1, points2) );
589     CV_Assert( CV_IS_MAT(fmatrix) && fmatrix->cols == 3 &&
590         (fmatrix->rows == 3 || (fmatrix->rows == 9 && method == CV_FM_7POINT)) );
591
592     count = MAX(points1->cols, points1->rows);
593     if( count < 7 )
594         return 0;
595
596     m1 = cvCreateMat( 1, count, CV_64FC2 );
597     cvConvertPointsHomogeneous( points1, m1 );
598
599     m2 = cvCreateMat( 1, count, CV_64FC2 );
600     cvConvertPointsHomogeneous( points2, m2 );
601
602     if( mask )
603     {
604         CV_Assert( CV_IS_MASK_ARR(mask) && CV_IS_MAT_CONT(mask->type) &&
605             (mask->rows == 1 || mask->cols == 1) &&
606             mask->rows*mask->cols == count );
607         tempMask = cvCloneMat(mask);
608     }
609     else if( count > 8 )
610         tempMask = cvCreateMat( 1, count, CV_8U );
611     if( !tempMask.empty() )
612         cvSet( tempMask, cvScalarAll(1.) );
613
614     CvFMEstimator estimator( MIN(count, (method & 3) == CV_FM_7POINT ? 7 : 8) );
615     if( count == 7 )
616         result = estimator.run7Point(m1, m2, &_F9x3);
617     else if( count == 8 || method == CV_FM_8POINT )
618         result = estimator.run8Point(m1, m2, &_F3x3);
619     else if( count > 8 )
620     {
621         if( param1 <= 0 )
622             param1 = 3;
623         if( param2 < DBL_EPSILON || param2 > 1 - DBL_EPSILON )
624             param2 = 0.99;
625         
626         if( (method & ~3) == CV_RANSAC )
627             result = estimator.runRANSAC(m1, m2, &_F3x3, tempMask, param1, param2 );
628         else
629             result = estimator.runLMeDS(m1, m2, &_F3x3, tempMask, param2 );
630         if( result <= 0 )
631             return 0;
632         /*icvCompressPoints( (CvPoint2D64f*)m1->data.ptr, tempMask->data.ptr, 1, count );
633         count = icvCompressPoints( (CvPoint2D64f*)m2->data.ptr, tempMask->data.ptr, 1, count );
634         assert( count >= 8 );
635         m1->cols = m2->cols = count;
636         estimator.run8Point(m1, m2, &_F3x3);*/
637     }
638
639     if( result )
640         cvConvert( fmatrix->rows == 3 ? &_F3x3 : &_F9x3, fmatrix );
641     
642     if( mask && tempMask )
643         cvCopy( tempMask, mask );
644
645     return result;
646 }
647
648
649 CV_IMPL void cvComputeCorrespondEpilines( const CvMat* points, int pointImageID,
650                                           const CvMat* fmatrix, CvMat* lines )
651 {
652     int abc_stride, abc_plane_stride, abc_elem_size;
653     int plane_stride, stride, elem_size;
654     int i, dims, count, depth, cn, abc_dims, abc_count, abc_depth, abc_cn;
655     uchar *ap, *bp, *cp;
656     const uchar *xp, *yp, *zp;
657     double f[9];
658     CvMat F = cvMat( 3, 3, CV_64F, f );
659
660     if( !CV_IS_MAT(points) )
661         CV_Error( !points ? CV_StsNullPtr : CV_StsBadArg, "points parameter is not a valid matrix" );
662
663     depth = CV_MAT_DEPTH(points->type);
664     cn = CV_MAT_CN(points->type);
665     if( (depth != CV_32F && depth != CV_64F) || (cn != 1 && cn != 2 && cn != 3) )
666         CV_Error( CV_StsUnsupportedFormat, "The format of point matrix is unsupported" );
667
668     if( points->rows > points->cols )
669     {
670         dims = cn*points->cols;
671         count = points->rows;
672     }
673     else
674     {
675         if( (points->rows > 1 && cn > 1) || (points->rows == 1 && cn == 1) )
676             CV_Error( CV_StsBadSize, "The point matrix does not have a proper layout (2xn, 3xn, nx2 or nx3)" );
677         dims = cn * points->rows;
678         count = points->cols;
679     }
680
681     if( dims != 2 && dims != 3 )
682         CV_Error( CV_StsOutOfRange, "The dimensionality of points must be 2 or 3" );
683
684     if( !CV_IS_MAT(fmatrix) )
685         CV_Error( !fmatrix ? CV_StsNullPtr : CV_StsBadArg, "fmatrix is not a valid matrix" );
686
687     if( CV_MAT_TYPE(fmatrix->type) != CV_32FC1 && CV_MAT_TYPE(fmatrix->type) != CV_64FC1 )
688         CV_Error( CV_StsUnsupportedFormat, "fundamental matrix must have 32fC1 or 64fC1 type" );
689
690     if( fmatrix->cols != 3 || fmatrix->rows != 3 )
691         CV_Error( CV_StsBadSize, "fundamental matrix must be 3x3" );
692
693     if( !CV_IS_MAT(lines) )
694         CV_Error( !lines ? CV_StsNullPtr : CV_StsBadArg, "lines parameter is not a valid matrix" );
695
696     abc_depth = CV_MAT_DEPTH(lines->type);
697     abc_cn = CV_MAT_CN(lines->type);
698     if( (abc_depth != CV_32F && abc_depth != CV_64F) || (abc_cn != 1 && abc_cn != 3) )
699         CV_Error( CV_StsUnsupportedFormat, "The format of the matrix of lines is unsupported" );
700
701     if( lines->rows > lines->cols )
702     {
703         abc_dims = abc_cn*lines->cols;
704         abc_count = lines->rows;
705     }
706     else
707     {
708         if( (lines->rows > 1 && abc_cn > 1) || (lines->rows == 1 && abc_cn == 1) )
709             CV_Error( CV_StsBadSize, "The lines matrix does not have a proper layout (3xn or nx3)" );
710         abc_dims = abc_cn * lines->rows;
711         abc_count = lines->cols;
712     }
713
714     if( abc_dims != 3 )
715         CV_Error( CV_StsOutOfRange, "The lines matrix does not have a proper layout (3xn or nx3)" );
716
717     if( abc_count != count )
718         CV_Error( CV_StsUnmatchedSizes, "The numbers of points and lines are different" );
719
720     elem_size = CV_ELEM_SIZE(depth);
721     abc_elem_size = CV_ELEM_SIZE(abc_depth);
722
723     if( points->rows == dims )
724     {
725         plane_stride = points->step;
726         stride = elem_size;
727     }
728     else
729     {
730         plane_stride = elem_size;
731         stride = points->rows == 1 ? dims*elem_size : points->step;
732     }
733
734     if( lines->rows == 3 )
735     {
736         abc_plane_stride = lines->step;
737         abc_stride = abc_elem_size;
738     }
739     else
740     {
741         abc_plane_stride = abc_elem_size;
742         abc_stride = lines->rows == 1 ? 3*abc_elem_size : lines->step;
743     }
744
745     cvConvert( fmatrix, &F );
746     if( pointImageID == 2 )
747         cvTranspose( &F, &F );
748
749     xp = points->data.ptr;
750     yp = xp + plane_stride;
751     zp = dims == 3 ? yp + plane_stride : 0;
752
753     ap = lines->data.ptr;
754     bp = ap + abc_plane_stride;
755     cp = bp + abc_plane_stride;
756
757     for( i = 0; i < count; i++ )
758     {
759         double x, y, z = 1.;
760         double a, b, c, nu;
761
762         if( depth == CV_32F )
763         {
764             x = *(float*)xp; y = *(float*)yp;
765             if( zp )
766                 z = *(float*)zp, zp += stride;
767         }
768         else
769         {
770             x = *(double*)xp; y = *(double*)yp;
771             if( zp )
772                 z = *(double*)zp, zp += stride;
773         }
774
775         xp += stride; yp += stride;
776
777         a = f[0]*x + f[1]*y + f[2]*z;
778         b = f[3]*x + f[4]*y + f[5]*z;
779         c = f[6]*x + f[7]*y + f[8]*z;
780         nu = a*a + b*b;
781         nu = nu ? 1./sqrt(nu) : 1.;
782         a *= nu; b *= nu; c *= nu;
783
784         if( abc_depth == CV_32F )
785         {
786             *(float*)ap = (float)a;
787             *(float*)bp = (float)b;
788             *(float*)cp = (float)c;
789         }
790         else
791         {
792             *(double*)ap = a;
793             *(double*)bp = b;
794             *(double*)cp = c;
795         }
796
797         ap += abc_stride;
798         bp += abc_stride;
799         cp += abc_stride;
800     }
801 }
802
803
804 CV_IMPL void cvConvertPointsHomogeneous( const CvMat* src, CvMat* dst )
805 {
806     Ptr<CvMat> temp, denom;
807
808     int i, s_count, s_dims, d_count, d_dims;
809     CvMat _src, _dst, _ones;
810     CvMat* ones = 0;
811
812     if( !CV_IS_MAT(src) )
813         CV_Error( !src ? CV_StsNullPtr : CV_StsBadArg,
814         "The input parameter is not a valid matrix" );
815
816     if( !CV_IS_MAT(dst) )
817         CV_Error( !dst ? CV_StsNullPtr : CV_StsBadArg,
818         "The output parameter is not a valid matrix" );
819
820     if( src == dst || src->data.ptr == dst->data.ptr )
821     {
822         if( src != dst && (!CV_ARE_TYPES_EQ(src, dst) || !CV_ARE_SIZES_EQ(src,dst)) )
823             CV_Error( CV_StsBadArg, "Invalid inplace operation" );
824         return;
825     }
826
827     if( src->rows > src->cols )
828     {
829         if( !((src->cols > 1) ^ (CV_MAT_CN(src->type) > 1)) )
830             CV_Error( CV_StsBadSize, "Either the number of channels or columns or rows must be =1" );
831
832         s_dims = CV_MAT_CN(src->type)*src->cols;
833         s_count = src->rows;
834     }
835     else
836     {
837         if( !((src->rows > 1) ^ (CV_MAT_CN(src->type) > 1)) )
838             CV_Error( CV_StsBadSize, "Either the number of channels or columns or rows must be =1" );
839
840         s_dims = CV_MAT_CN(src->type)*src->rows;
841         s_count = src->cols;
842     }
843
844     if( src->rows == 1 || src->cols == 1 )
845         src = cvReshape( src, &_src, 1, s_count );
846
847     if( dst->rows > dst->cols )
848     {
849         if( !((dst->cols > 1) ^ (CV_MAT_CN(dst->type) > 1)) )
850             CV_Error( CV_StsBadSize,
851             "Either the number of channels or columns or rows in the input matrix must be =1" );
852
853         d_dims = CV_MAT_CN(dst->type)*dst->cols;
854         d_count = dst->rows;
855     }
856     else
857     {
858         if( !((dst->rows > 1) ^ (CV_MAT_CN(dst->type) > 1)) )
859             CV_Error( CV_StsBadSize,
860             "Either the number of channels or columns or rows in the output matrix must be =1" );
861
862         d_dims = CV_MAT_CN(dst->type)*dst->rows;
863         d_count = dst->cols;
864     }
865
866     if( dst->rows == 1 || dst->cols == 1 )
867         dst = cvReshape( dst, &_dst, 1, d_count );
868
869     if( s_count != d_count )
870         CV_Error( CV_StsUnmatchedSizes, "Both matrices must have the same number of points" );
871
872     if( CV_MAT_DEPTH(src->type) < CV_32F || CV_MAT_DEPTH(dst->type) < CV_32F )
873         CV_Error( CV_StsUnsupportedFormat,
874         "Both matrices must be floating-point (single or double precision)" );
875
876     if( s_dims < 2 || s_dims > 4 || d_dims < 2 || d_dims > 4 )
877         CV_Error( CV_StsOutOfRange,
878         "Both input and output point dimensionality must be 2, 3 or 4" );
879
880     if( s_dims < d_dims - 1 || s_dims > d_dims + 1 )
881         CV_Error( CV_StsUnmatchedSizes,
882         "The dimensionalities of input and output point sets differ too much" );
883
884     if( s_dims == d_dims - 1 )
885     {
886         if( d_count == dst->rows )
887         {
888             ones = cvGetSubRect( dst, &_ones, cvRect( s_dims, 0, 1, d_count ));
889             dst = cvGetSubRect( dst, &_dst, cvRect( 0, 0, s_dims, d_count ));
890         }
891         else
892         {
893             ones = cvGetSubRect( dst, &_ones, cvRect( 0, s_dims, d_count, 1 ));
894             dst = cvGetSubRect( dst, &_dst, cvRect( 0, 0, d_count, s_dims ));
895         }
896     }
897
898     if( s_dims <= d_dims )
899     {
900         if( src->rows == dst->rows && src->cols == dst->cols )
901         {
902             if( CV_ARE_TYPES_EQ( src, dst ) )
903                 cvCopy( src, dst );
904             else
905                 cvConvert( src, dst );
906         }
907         else
908         {
909             if( !CV_ARE_TYPES_EQ( src, dst ))
910             {
911                 temp = cvCreateMat( src->rows, src->cols, dst->type );
912                 cvConvert( src, temp );
913                 src = temp;
914             }
915             cvTranspose( src, dst );
916         }
917
918         if( ones )
919             cvSet( ones, cvRealScalar(1.) );
920     }
921     else
922     {
923         int s_plane_stride, s_stride, d_plane_stride, d_stride, elem_size;
924
925         if( !CV_ARE_TYPES_EQ( src, dst ))
926         {
927             temp = cvCreateMat( src->rows, src->cols, dst->type );
928             cvConvert( src, temp );
929             src = temp;
930         }
931
932         elem_size = CV_ELEM_SIZE(src->type);
933
934         if( s_count == src->cols )
935             s_plane_stride = src->step / elem_size, s_stride = 1;
936         else
937             s_stride = src->step / elem_size, s_plane_stride = 1;
938
939         if( d_count == dst->cols )
940             d_plane_stride = dst->step / elem_size, d_stride = 1;
941         else
942             d_stride = dst->step / elem_size, d_plane_stride = 1;
943
944         denom = cvCreateMat( 1, d_count, dst->type );
945
946         if( CV_MAT_DEPTH(dst->type) == CV_32F )
947         {
948             const float* xs = src->data.fl;
949             const float* ys = xs + s_plane_stride;
950             const float* zs = 0;
951             const float* ws = xs + (s_dims - 1)*s_plane_stride;
952
953             float* iw = denom->data.fl;
954
955             float* xd = dst->data.fl;
956             float* yd = xd + d_plane_stride;
957             float* zd = 0;
958
959             if( d_dims == 3 )
960             {
961                 zs = ys + s_plane_stride;
962                 zd = yd + d_plane_stride;
963             }
964
965             for( i = 0; i < d_count; i++, ws += s_stride )
966             {
967                 float t = *ws;
968                 iw[i] = fabs((double)t) > FLT_EPSILON ? t : 1.f;
969             }
970
971             cvDiv( 0, denom, denom );
972
973             if( d_dims == 3 )
974                 for( i = 0; i < d_count; i++ )
975                 {
976                     float w = iw[i];
977                     float x = *xs * w, y = *ys * w, z = *zs * w;
978                     xs += s_stride; ys += s_stride; zs += s_stride;
979                     *xd = x; *yd = y; *zd = z;
980                     xd += d_stride; yd += d_stride; zd += d_stride;
981                 }
982             else
983                 for( i = 0; i < d_count; i++ )
984                 {
985                     float w = iw[i];
986                     float x = *xs * w, y = *ys * w;
987                     xs += s_stride; ys += s_stride;
988                     *xd = x; *yd = y;
989                     xd += d_stride; yd += d_stride;
990                 }
991         }
992         else
993         {
994             const double* xs = src->data.db;
995             const double* ys = xs + s_plane_stride;
996             const double* zs = 0;
997             const double* ws = xs + (s_dims - 1)*s_plane_stride;
998
999             double* iw = denom->data.db;
1000
1001             double* xd = dst->data.db;
1002             double* yd = xd + d_plane_stride;
1003             double* zd = 0;
1004
1005             if( d_dims == 3 )
1006             {
1007                 zs = ys + s_plane_stride;
1008                 zd = yd + d_plane_stride;
1009             }
1010
1011             for( i = 0; i < d_count; i++, ws += s_stride )
1012             {
1013                 double t = *ws;
1014                 iw[i] = fabs(t) > DBL_EPSILON ? t : 1.;
1015             }
1016
1017             cvDiv( 0, denom, denom );
1018
1019             if( d_dims == 3 )
1020                 for( i = 0; i < d_count; i++ )
1021                 {
1022                     double w = iw[i];
1023                     double x = *xs * w, y = *ys * w, z = *zs * w;
1024                     xs += s_stride; ys += s_stride; zs += s_stride;
1025                     *xd = x; *yd = y; *zd = z;
1026                     xd += d_stride; yd += d_stride; zd += d_stride;
1027                 }
1028             else
1029                 for( i = 0; i < d_count; i++ )
1030                 {
1031                     double w = iw[i];
1032                     double x = *xs * w, y = *ys * w;
1033                     xs += s_stride; ys += s_stride;
1034                     *xd = x; *yd = y;
1035                     xd += d_stride; yd += d_stride;
1036                 }
1037         }
1038     }
1039 }
1040
1041 namespace cv
1042 {
1043
1044 static Mat _findHomography( const Mat& points1, const Mat& points2,
1045                             int method, double ransacReprojThreshold,
1046                             vector<uchar>* mask )
1047 {
1048     CV_Assert(points1.isContinuous() && points2.isContinuous() &&
1049               points1.type() == points2.type() &&
1050               ((points1.rows == 1 && points1.channels() == 2) ||
1051                points1.cols*points1.channels() == 2) &&
1052               ((points2.rows == 1 && points2.channels() == 2) ||
1053                points2.cols*points2.channels() == 2));
1054     
1055     Mat H(3, 3, CV_64F);
1056     CvMat _pt1 = Mat(points1), _pt2 = Mat(points2);
1057     CvMat matH = H, _mask, *pmask = 0;
1058     if( mask )
1059     {
1060         mask->resize(points1.cols*points1.rows*points1.channels()/2);
1061         pmask = &(_mask = cvMat(1, (int)mask->size(), CV_8U, (void*)&(*mask)[0]));
1062     }
1063     bool ok = cvFindHomography( &_pt1, &_pt2, &matH, method, ransacReprojThreshold, pmask ) > 0;
1064     if( !ok )
1065         H = Scalar(0);
1066     return H;
1067 }
1068     
1069 static Mat _findFundamentalMat( const Mat& points1, const Mat& points2,
1070                                int method, double param1, double param2,
1071                                vector<uchar>* mask )
1072 {
1073     CV_Assert(points1.isContinuous() && points2.isContinuous() &&
1074               points1.type() == points2.type() &&
1075               ((points1.rows == 1 && points1.channels() == 2) ||
1076                points1.cols*points1.channels() == 2) &&
1077               ((points2.rows == 1 && points2.channels() == 2) ||
1078                points2.cols*points2.channels() == 2));
1079     
1080     Mat F(3, 3, CV_64F);
1081     CvMat _pt1 = Mat(points1), _pt2 = Mat(points2);
1082     CvMat matF = F, _mask, *pmask = 0;
1083     if( mask )
1084     {
1085         mask->resize(points1.cols*points1.rows*points1.channels()/2);
1086         pmask = &(_mask = cvMat(1, (int)mask->size(), CV_8U, (void*)&(*mask)[0]));
1087     }
1088     int n = cvFindFundamentalMat( &_pt1, &_pt2, &matF, method, param1, param2, pmask );
1089     if( n <= 0 )
1090         F = Scalar(0);
1091     return F;
1092 }
1093     
1094 }    
1095
1096
1097 cv::Mat cv::findHomography( const Mat& srcPoints, const Mat& dstPoints,
1098                             vector<uchar>& mask, int method,
1099                             double ransacReprojThreshold )
1100 {
1101     return _findHomography(srcPoints, dstPoints, method, ransacReprojThreshold, &mask);
1102 }
1103
1104 cv::Mat cv::findHomography( const Mat& srcPoints, const Mat& dstPoints,
1105                             int method, double ransacReprojThreshold )
1106 {
1107     return _findHomography(srcPoints, dstPoints, method, ransacReprojThreshold, 0);
1108 }
1109
1110     
1111 cv::Mat cv::findFundamentalMat( const Mat& points1, const Mat& points2,
1112                                 vector<uchar>& mask, int method, double param1, double param2 )
1113 {
1114     return _findFundamentalMat( points1, points2, method, param1, param2, &mask );
1115 }
1116
1117 cv::Mat cv::findFundamentalMat( const Mat& points1, const Mat& points2,
1118                                 int method, double param1, double param2 )
1119 {
1120     return _findFundamentalMat( points1, points2, method, param1, param2, 0 );
1121 }
1122
1123 void cv::computeCorrespondEpilines( const Mat& points, int whichImage,
1124                                     const Mat& F, vector<Vec3f>& lines )
1125 {
1126     CV_Assert(points.isContinuous() &&
1127               (points.depth() == CV_32S || points.depth() == CV_32F) &&
1128               ((points.rows == 1 && points.channels() == 2) ||
1129                points.cols*points.channels() == 2));
1130     
1131     lines.resize(points.cols*points.rows*points.channels()/2);
1132     CvMat _points = points, _lines = Mat(lines), matF = F;
1133     cvComputeCorrespondEpilines(&_points, whichImage, &matF, &_lines);
1134 }
1135
1136 void cv::convertPointsHomogeneous( const Mat& src, vector<Point3f>& dst )
1137 {
1138     CV_Assert(src.isContinuous() &&
1139               (src.depth() == CV_32S || src.depth() == CV_32F) &&
1140               ((src.rows == 1 && src.channels() == 2) ||
1141                src.cols*src.channels() == 2));
1142     
1143     dst.resize(src.cols*src.rows*src.channels()/2);
1144     CvMat _src = src, _dst = Mat(dst);
1145     cvConvertPointsHomogeneous(&_src, &_dst);
1146 }
1147
1148 void cv::convertPointsHomogeneous( const Mat& src, vector<Point2f>& dst )
1149 {
1150     CV_Assert(src.isContinuous() &&
1151               (src.depth() == CV_32S || src.depth() == CV_32F) &&
1152               ((src.rows == 1 && src.channels() == 3) ||
1153                src.cols*src.channels() == 3));
1154     
1155     dst.resize(src.cols*src.rows*src.channels()/3);
1156     CvMat _src = Mat(src), _dst = Mat(dst);
1157     cvConvertPointsHomogeneous(&_src, &_dst);
1158 }
1159
1160 /* End of file. */