1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
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.
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.
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.
44 /* Evaluation of Fundamental Matrix from point correspondences.
45 The original code has been written by Valery Mosyagin */
47 /* The algorithms (except for RANSAC) and the notation have been taken from
48 Zhengyou Zhang's research report
49 "Determining the Epipolar Geometry and its Uncertainty: A Review"
50 that can be found at http://www-sop.inria.fr/robotvis/personnel/zzhang/zzhang-eng.html */
52 /************************************** 7-point algorithm *******************************/
54 icvFMatrix_7Point( const CvPoint2D64f* m0, const CvPoint2D64f* m1, double* fmatrix )
56 double a[7*9], w[7], v[9*9], c[4], r[3];
59 CvMat A = cvMat( 7, 9, CV_64F, a );
60 CvMat V = cvMat( 9, 9, CV_64F, v );
61 CvMat W = cvMat( 7, 1, CV_64F, w );
62 CvMat coeffs = cvMat( 1, 4, CV_64F, c );
63 CvMat roots = cvMat( 1, 3, CV_64F, r );
66 assert( m0 && m1 && fmatrix );
68 // form a linear system: i-th row of A(=a) represents
69 // the equation: (m1[i], 1)'*F*(m0[i], 1) = 0
70 for( i = 0; i < 7; i++ )
72 double x0 = m0[i].x, y0 = m0[i].y;
73 double x1 = m1[i].x, y1 = m1[i].y;
86 // A*(f11 f12 ... f33)' = 0 is singular (7 equations for 9 variables), so
87 // the solution is linear subspace of dimensionality 2.
88 // => use the last two singular vectors as a basis of the space
89 // (according to SVD properties)
90 cvSVD( &A, &W, 0, &V, CV_SVD_MODIFY_A + CV_SVD_V_T );
94 // f1, f2 is a basis => lambda*f1 + mu*f2 is an arbitrary f. matrix.
95 // as it is determined up to a scale, normalize lambda & mu (lambda + mu = 1),
96 // so f ~ lambda*f1 + (1 - lambda)*f2.
97 // use the additional constraint det(f) = det(lambda*f1 + (1-lambda)*f2) to find lambda.
98 // it will be a cubic equation.
99 // find c - polynomial coefficients.
100 for( i = 0; i < 9; i++ )
103 t0 = f2[4]*f2[8] - f2[5]*f2[7];
104 t1 = f2[3]*f2[8] - f2[5]*f2[6];
105 t2 = f2[3]*f2[7] - f2[4]*f2[6];
107 c[3] = f2[0]*t0 - f2[1]*t1 + f2[2]*t2;
109 c[2] = f1[0]*t0 - f1[1]*t1 + f1[2]*t2 -
110 f1[3]*(f2[1]*f2[8] - f2[2]*f2[7]) +
111 f1[4]*(f2[0]*f2[8] - f2[2]*f2[6]) -
112 f1[5]*(f2[0]*f2[7] - f2[1]*f2[6]) +
113 f1[6]*(f2[1]*f2[5] - f2[2]*f2[4]) -
114 f1[7]*(f2[0]*f2[5] - f2[2]*f2[3]) +
115 f1[8]*(f2[0]*f2[4] - f2[1]*f2[3]);
117 t0 = f1[4]*f1[8] - f1[5]*f1[7];
118 t1 = f1[3]*f1[8] - f1[5]*f1[6];
119 t2 = f1[3]*f1[7] - f1[4]*f1[6];
121 c[1] = f2[0]*t0 - f2[1]*t1 + f2[2]*t2 -
122 f2[3]*(f1[1]*f1[8] - f1[2]*f1[7]) +
123 f2[4]*(f1[0]*f1[8] - f1[2]*f1[6]) -
124 f2[5]*(f1[0]*f1[7] - f1[1]*f1[6]) +
125 f2[6]*(f1[1]*f1[5] - f1[2]*f1[4]) -
126 f2[7]*(f1[0]*f1[5] - f1[2]*f1[3]) +
127 f2[8]*(f1[0]*f1[4] - f1[1]*f1[3]);
129 c[0] = f1[0]*t0 - f1[1]*t1 + f1[2]*t2;
131 // solve the cubic equation; there can be 1 to 3 roots ...
132 n = cvSolveCubic( &coeffs, &roots );
137 for( k = 0; k < n; k++, fmatrix += 9 )
139 // for each root form the fundamental matrix
140 double lambda = r[k], mu = 1.;
141 double s = f1[8]*r[k] + f2[8];
143 // normalize each matrix, so that F(3,3) (~fmatrix[8]) == 1
144 if( fabs(s) > DBL_EPSILON )
153 for( i = 0; i < 8; i++ )
154 fmatrix[i] = f1[i]*lambda + f2[i]*mu;
161 /*************************************** 8-point algorithm ******************************/
163 icvFMatrix_8Point( const CvPoint2D64f* m0, const CvPoint2D64f* m1,
164 const uchar* mask, int count, double* fmatrix )
170 CvMat W = cvMat( 1, 9, CV_64F, w);
171 CvMat V = cvMat( 9, 9, CV_64F, v);
174 int i, good_count = 0;
175 CvPoint2D64f m0c = {0,0}, m1c = {0,0}, m0q = {0,0}, m1q = {0,0};
176 double t, scale0, scale1;
180 CV_FUNCNAME( "icvFMatrix_8Point" );
184 assert( m0 && m1 && fmatrix );
186 // compute centers and average distances for each of the two point sets
187 for( i = 0; i < count; i++ )
188 if( !mask || mask[i] )
190 double x = m0[i].x, y = m0[i].y;
191 m0c.x += x; m0c.y += y;
192 m0q.x += x*x; m0q.y += y*y;
194 x = m1[i].x, y = m1[i].y;
195 m1c.x += x; m1c.y += y;
196 m1q.x += x*x; m1q.y += y*y;
203 // calculate the normalizing transformations for each of the point sets:
204 // after the transformation each set will have the mass center at the coordinate origin
205 // and the average distance from the origin will be ~sqrt(2).
207 m0c.x *= t; m0c.y *= t;
208 scale0 = t * sqrt( m0q.x + m0q.y - good_count*(m0c.x*m0c.x + m0c.y*m0c.y) );
209 m1c.x *= t; m1c.y *= t;
210 scale1 = t * sqrt( m1q.x + m1q.y - good_count*(m1c.x*m1c.x + m1c.y*m1c.y) );
212 if( scale0 < FLT_EPSILON || scale1 < FLT_EPSILON )
215 scale0 = sqrt(2.)/scale0;
216 scale1 = sqrt(2.)/scale1;
218 CV_CALL( A = cvCreateMat( good_count, 9, CV_64F ));
220 a_step = A->step / sizeof(a[0]);
222 // form a linear system: for each selected pair of points m0 & m1,
223 // the row of A(=a) represents the equation: (m1, 1)'*F*(m0, 1) = 0
224 for( i = 0; i < count; i++ )
226 if( !mask || mask[i] )
228 double x0 = (m0[i].x - m0c.x)*scale0;
229 double y0 = (m0[i].y - m0c.y)*scale0;
230 double x1 = (m1[i].x - m1c.x)*scale1;
231 double y1 = (m1[i].y - m1c.y)*scale1;
246 cvSVD( A, &W, 0, &V, CV_SVD_MODIFY_A + CV_SVD_V_T );
248 for( i = 0; i < 8; i++ )
250 if( fabs(w[i]) < FLT_EPSILON )
257 F0 = cvMat( 3, 3, CV_64F, v + 9*8 ); // take the last column of v as a solution of Af = 0
259 // make F0 singular (of rank 2) by decomposing it with SVD,
260 // zeroing the last diagonal element of W and then composing the matrices back.
262 // use v as a temporary storage for different 3x3 matrices
269 cvSVD( &F0, &W, &U, &V, CV_SVD_MODIFY_A + CV_SVD_U_T + CV_SVD_V_T );
272 // F0 <- U*diag([W(1), W(2), 0])*V'
273 cvGEMM( &U, &W, 1., 0, 0., &TF, CV_GEMM_A_T );
274 cvGEMM( &TF, &V, 1., 0, 0., &F0, 0/*CV_GEMM_B_T*/ );
276 // apply the transformation that is inverse
277 // to what we used to normalize the point coordinates
279 double tt0[] = { scale0, 0, -scale0*m0c.x, 0, scale0, -scale0*m0c.y, 0, 0, 1 };
280 double tt1[] = { scale1, 0, -scale1*m1c.x, 0, scale1, -scale1*m1c.y, 0, 0, 1 };
287 cvGEMM( &T1, &F0, 1., 0, 0., &TF, CV_GEMM_A_T );
288 F0.data.db = fmatrix;
289 cvGEMM( &TF, &T0, 1., 0, 0., &F0, 0 );
292 if( fabs(F0.data.db[8]) > FLT_EPSILON )
293 cvScale( &F0, &F0, 1./F0.data.db[8] );
305 /************************************ RANSAC algorithm **********************************/
307 icvFMatrix_RANSAC( const CvPoint2D64f* m0, const CvPoint2D64f* m1,
308 uchar* mask, int count, double* fmatrix,
309 double threshold, double p,
310 unsigned rng_seed, int use_8point )
314 const int max_random_iters = 1000;
315 const int sample_size = 7;
316 uchar* curr_mask = 0;
317 uchar* temp_mask = 0;
319 CV_FUNCNAME( "icvFMatrix_RANSAC" );
324 CvRNG rng = cvRNG(rng_seed);
325 int i, j, k, sample_count, max_samples = 500;
326 int best_good_count = 0;
328 assert( m0 && m1 && fmatrix && 0 < p && p < 1 && threshold > 0 );
330 threshold *= threshold;
332 CV_CALL( curr_mask = (uchar*)cvAlloc( count ));
333 if( !mask && use_8point )
335 CV_CALL( temp_mask = (uchar*)cvAlloc( count ));
339 // find the best fundamental matrix (giving the least backprojection error)
340 // by picking at most <max_samples> 7-tuples of corresponding points
341 // <max_samples> may be updated (decreased) within the loop based on statistics of outliers
342 for( sample_count = 0; sample_count < max_samples; sample_count++ )
344 int idx[sample_size], n;
345 CvPoint2D64f ms0[sample_size], ms1[sample_size];
347 // choose random <sample_size> (=7) points
348 for( i = 0; i < sample_size; i++ )
350 for( k = 0; k < max_random_iters; k++ )
352 idx[i] = cvRandInt(&rng) % count;
353 for( j = 0; j < i; j++ )
354 if( idx[j] == idx[i] )
363 if( k >= max_random_iters )
367 if( i < sample_size )
370 // find 1 or 3 fundamental matrices out of the 7 point correspondences
371 n = icvFMatrix_7Point( ms0, ms1, ff );
376 // for each matrix calculate the backprojection error
377 // (distance to the corresponding epipolar lines) for each point and thus find
378 // the number of in-liers.
379 for( k = 0; k < n; k++ )
381 const double* f = ff + k*9;
384 for( i = 0; i < count; i++ )
386 double d0, d1, s0, s1;
388 double a = f[0]*m0[i].x + f[1]*m0[i].y + f[2];
389 double b = f[3]*m0[i].x + f[4]*m0[i].y + f[5];
390 double c = f[6]*m0[i].x + f[7]*m0[i].y + f[8];
393 d1 = m1[i].x*a + m1[i].y*b + c;
395 a = f[0]*m1[i].x + f[3]*m1[i].y + f[6];
396 b = f[1]*m1[i].x + f[4]*m1[i].y + f[7];
397 c = f[2]*m1[i].x + f[5]*m1[i].y + f[8];
400 d0 = m0[i].x*a + m0[i].y*b + c;
402 curr_mask[i] = d1*d1 < threshold*s1 && d0*d0 < threshold*s0;
403 good_count += curr_mask[i];
406 if( good_count > MAX( best_good_count, 6 ) )
411 // update the current best fundamental matrix and "goodness" flags
413 memcpy( mask, curr_mask, count );
414 memcpy( fmatrix, f, 9*sizeof(f[0]));
415 best_good_count = good_count;
417 // try to update (decrease) <max_samples>
418 ep = (double)(count - good_count)/count;
420 lep = log(1. - pow(ep,7.));
421 if( lp < lep || lep >= 0 )
425 new_max_samples = cvRound(lp/lep);
426 max_samples = MIN( new_max_samples, max_samples );
432 if( best_good_count < 7 )
437 // optionally, use 8-point algorithm to compute fundamental matrix using only the in-liers
438 if( best_good_count >= 8 && use_8point )
439 result = icvFMatrix_8Point( m0, m1, mask, count, fmatrix );
443 cvFree( &temp_mask );
444 cvFree( &curr_mask );
450 /***************************** Least Median of Squares algorithm ************************/
452 static CV_IMPLEMENT_QSORT( icvSortDistances, int, CV_LT )
454 /* the algorithm is quite similar to RANSAC, but here we choose the matrix that gives
455 the least median of d(m0[i], F'*m1[i])^2 + d(m1[i], F*m0[i])^2 (0<=i<count),
456 instead of choosing the matrix that gives the least number of outliers (as it is done in RANSAC) */
458 icvFMatrix_LMedS( const CvPoint2D64f* m0, const CvPoint2D64f* m1,
459 uchar* mask, int count, double* fmatrix,
460 double threshold, double p,
461 unsigned rng_seed, int use_8point )
465 const int max_random_iters = 1000;
466 const int sample_size = 7;
469 uchar* curr_mask = 0;
470 uchar* temp_mask = 0;
472 CV_FUNCNAME( "icvFMatrix_LMedS" );
477 CvRNG rng = cvRNG(rng_seed);
478 int i, j, k, sample_count, max_samples = 500;
479 double least_median = DBL_MAX, median;
480 int best_good_count = 0;
482 assert( m0 && m1 && fmatrix && 0 < p && p < 1 && threshold > 0 );
484 threshold *= threshold;
486 CV_CALL( curr_mask = (uchar*)cvAlloc( count ));
487 CV_CALL( dist = (float*)cvAlloc( count*sizeof(dist[0]) ));
489 if( !mask && use_8point )
491 CV_CALL( temp_mask = (uchar*)cvAlloc( count ));
495 // find the best fundamental matrix (giving the least backprojection error)
496 // by picking at most <max_samples> 7-tuples of corresponding points
497 // <max_samples> may be updated (decreased) within the loop based on statistics of outliers
498 for( sample_count = 0; sample_count < max_samples; sample_count++ )
500 int idx[sample_size], n;
501 CvPoint2D64f ms0[sample_size], ms1[sample_size];
503 // choose random <sample_size> (=7) points
504 for( i = 0; i < sample_size; i++ )
506 for( k = 0; k < max_random_iters; k++ )
508 idx[i] = cvRandInt(&rng) % count;
509 for( j = 0; j < i; j++ )
510 if( idx[j] == idx[i] )
519 if( k >= max_random_iters )
523 if( i < sample_size )
526 // find 1 or 3 fundamental matrix out of the 7 point correspondences
527 n = icvFMatrix_7Point( ms0, ms1, ff );
532 // for each matrix calculate the backprojection error
533 // (distance to the corresponding epipolar lines) for each point and thus find
534 // the number of in-liers.
535 for( k = 0; k < n; k++ )
537 const double* f = ff + k*9;
540 for( i = 0; i < count; i++ )
544 double a = f[0]*m0[i].x + f[1]*m0[i].y + f[2];
545 double b = f[3]*m0[i].x + f[4]*m0[i].y + f[5];
546 double c = f[6]*m0[i].x + f[7]*m0[i].y + f[8];
549 d1 = m1[i].x*a + m1[i].y*b + c;
552 a = f[0]*m1[i].x + f[3]*m1[i].y + f[6];
553 b = f[1]*m1[i].x + f[4]*m1[i].y + f[7];
554 c = f[2]*m1[i].x + f[5]*m1[i].y + f[8];
557 d0 = m0[i].x*a + m0[i].y*b + c;
560 curr_mask[i] = d1 < threshold && d0 < threshold;
561 good_count += curr_mask[i];
563 dist[i] = (float)(d0 + d1);
566 icvSortDistances( (int*)dist, count, 0 );
567 median = (double)dist[count/2];
569 if( median < least_median )
574 // update the current best fundamental matrix and "goodness" flags
576 memcpy( mask, curr_mask, count );
577 memcpy( fmatrix, f, 9*sizeof(f[0]));
578 least_median = median;
579 best_good_count = good_count;
581 // try to update (decrease) <max_samples>
582 ep = (double)(count - good_count)/count;
584 lep = log(1. - pow(ep,7.));
585 if( lp < lep || lep >= 0 )
589 new_max_samples = cvRound(lp/lep);
590 max_samples = MIN( new_max_samples, max_samples );
596 if( best_good_count < 7 )
601 // optionally, use 8-point algorithm to compute fundamental matrix using only the in-liers
602 if( best_good_count >= 8 && use_8point )
603 result = icvFMatrix_8Point( m0, m1, mask, count, fmatrix );
607 cvFree( &temp_mask );
608 cvFree( &curr_mask );
616 cvFindFundamentalMat( const CvMat* points0, const CvMat* points1,
617 CvMat* fmatrix, int method,
618 double param1, double param2, CvMat* status )
620 const unsigned rng_seed = 0xffffffff;
622 int pt_alloc_flag[2] = { 0, 0 };
624 CvPoint2D64f* pt[2] = { 0, 0 };
627 CV_FUNCNAME( "cvFindFundamentalMat" );
633 uchar* status_data = 0;
634 double fmatrix_data0[9*3];
635 double* fmatrix_data = 0;
637 if( !CV_IS_MAT(points0) )
638 CV_ERROR( !points0 ? CV_StsNullPtr : CV_StsBadArg, "points0 is not a valid matrix" );
640 if( !CV_IS_MAT(points1) )
641 CV_ERROR( !points1 ? CV_StsNullPtr : CV_StsBadArg, "points1 is not a valid matrix" );
643 if( !CV_ARE_TYPES_EQ(points0, points1) )
644 CV_ERROR( CV_StsUnmatchedFormats, "The matrices of points should have the same data type" );
646 if( !CV_ARE_SIZES_EQ(points0, points1) )
647 CV_ERROR( CV_StsUnmatchedSizes, "The matrices of points should have the same size" );
649 depth = CV_MAT_DEPTH(points0->type);
650 cn = CV_MAT_CN(points0->type);
651 if( depth != CV_32S && depth != CV_32F && depth != CV_64F || cn != 1 && cn != 2 && cn != 3 )
652 CV_ERROR( CV_StsUnsupportedFormat, "The format of point matrices is unsupported" );
654 if( points0->rows > points0->cols )
656 dims = cn*points0->cols;
657 count = points0->rows;
661 if( points0->rows > 1 && cn > 1 || points0->rows == 1 && cn == 1 )
662 CV_ERROR( CV_StsBadSize, "The point matrices do not have a proper layout (2xn, 3xn, nx2 or nx3)" );
663 dims = cn * points0->rows;
664 count = points0->cols;
667 if( dims != 2 && dims != 3 )
668 CV_ERROR( CV_StsOutOfRange, "The dimensionality of points must be 2 or 3" );
670 if( method == CV_FM_7POINT && count != 7 ||
671 method != CV_FM_7POINT && count < 7 + (method == CV_FM_8POINT) )
672 CV_ERROR( CV_StsOutOfRange,
673 "The number of points must be 7 for 7-point algorithm, "
674 ">=8 for 8-point algorithm and >=7 for other algorithms" );
676 if( !CV_IS_MAT(fmatrix) )
677 CV_ERROR( !fmatrix ? CV_StsNullPtr : CV_StsBadArg, "fmatrix is not a valid matrix" );
679 if( CV_MAT_TYPE(fmatrix->type) != CV_32FC1 && CV_MAT_TYPE(fmatrix->type) != CV_64FC1 )
680 CV_ERROR( CV_StsUnsupportedFormat, "fundamental matrix must have 32fC1 or 64fC1 type" );
682 if( fmatrix->cols != 3 || (fmatrix->rows != 3 && (method != CV_FM_7POINT || fmatrix->rows != 9)))
683 CV_ERROR( CV_StsBadSize, "fundamental matrix must be 3x3 or 3x9 (for 7-point method only)" );
685 fmatrix_data = fmatrix->data.db;
686 if( !CV_IS_MAT_CONT(fmatrix->type) || CV_MAT_TYPE(fmatrix->type) != CV_64FC1 ||
687 method == CV_FM_7POINT && fmatrix->rows != 9 )
688 fmatrix_data = fmatrix_data0;
692 if( !CV_IS_MAT(status) )
693 CV_ERROR( CV_StsBadArg, "The output status is not a valid matrix" );
695 if( status->cols != 1 && status->rows != 1 || status->cols + status->rows - 1 != count )
696 CV_ERROR( CV_StsUnmatchedSizes,
697 "The status matrix must have the same size as the point matrices" );
699 if( method == CV_FM_7POINT || method == CV_FM_8POINT )
700 cvSet( status, cvScalarAll(1.) );
703 status_data = status->data.ptr;
704 if( !CV_IS_MAT_CONT(status->type) || !CV_IS_MASK_ARR(status) )
706 CV_CALL( _status = cvCreateMat( status->rows, status->cols, CV_8UC1 ));
707 status_data = _status->data.ptr;
712 for( k = 0; k < 2; k++ )
714 const CvMat* spt = k == 0 ? points0 : points1;
715 CvPoint2D64f* dpt = pt[k] = (CvPoint2D64f*)spt->data.db;
716 int plane_stride, stride, elem_size;
718 if( CV_IS_MAT_CONT(spt->type) && CV_MAT_DEPTH(spt->type) == CV_64F &&
719 dims == 2 && (spt->rows == 1 || spt->rows == count) )
722 elem_size = CV_ELEM_SIZE(depth);
724 if( spt->rows == dims )
726 plane_stride = spt->step / elem_size;
732 stride = spt->rows == 1 ? dims : spt->step / elem_size;
735 CV_CALL( dpt = pt[k] = (CvPoint2D64f*)cvAlloc( count*sizeof(dpt[0]) ));
736 pt_alloc_flag[k] = 1;
738 if( depth == CV_32F )
740 const float* xp = spt->data.fl;
741 const float* yp = xp + plane_stride;
742 const float* zp = dims == 3 ? yp + plane_stride : 0;
744 for( i = 0; i < count; i++ )
746 double x = *xp, y = *yp;
763 const double* xp = spt->data.db;
764 const double* yp = xp + plane_stride;
765 const double* zp = dims == 3 ? yp + plane_stride : 0;
767 for( i = 0; i < count; i++ )
769 double x = *xp, y = *yp;
786 if( method == CV_FM_7POINT )
787 result = icvFMatrix_7Point( pt[0], pt[1], fmatrix_data );
788 else if( method == CV_FM_8POINT )
789 result = icvFMatrix_8Point( pt[0], pt[1], 0, count, fmatrix_data );
793 CV_ERROR( CV_StsOutOfRange, "param1 (threshold) must be > 0" );
795 if( param2 < 0 || param2 > 1 )
796 CV_ERROR( CV_StsOutOfRange, "param2 (confidence level) must be between 0 and 1" );
798 if( param2 < DBL_EPSILON || param2 > 1 - DBL_EPSILON )
801 if( method < CV_FM_RANSAC_ONLY )
802 result = icvFMatrix_LMedS( pt[0], pt[1], status_data, count, fmatrix_data,
803 param1, param2, rng_seed, method & CV_FM_8POINT );
805 result = icvFMatrix_RANSAC( pt[0], pt[1], status_data, count, fmatrix_data,
806 param1, param2, rng_seed, method & CV_FM_8POINT );
809 if( result && fmatrix->data.db != fmatrix_data )
813 hdr = cvMat( MIN(fmatrix->rows, result*3), fmatrix->cols, CV_64F, fmatrix_data );
814 cvConvert( &hdr, fmatrix );
817 if( status && status_data && status->data.ptr != status_data )
818 cvConvert( _status, status );
822 cvReleaseMat( &_status );
823 for( k = 0; k < 2; k++ )
824 if( pt_alloc_flag[k] )
832 cvComputeCorrespondEpilines( const CvMat* points, int pointImageID,
833 const CvMat* fmatrix, CvMat* lines )
835 CV_FUNCNAME( "cvComputeCorrespondEpilines" );
839 int abc_stride, abc_plane_stride, abc_elem_size;
840 int plane_stride, stride, elem_size;
841 int i, dims, count, depth, cn, abc_dims, abc_count, abc_depth, abc_cn;
843 const uchar *xp, *yp, *zp;
845 CvMat F = cvMat( 3, 3, CV_64F, f );
847 if( !CV_IS_MAT(points) )
848 CV_ERROR( !points ? CV_StsNullPtr : CV_StsBadArg, "points parameter is not a valid matrix" );
850 depth = CV_MAT_DEPTH(points->type);
851 cn = CV_MAT_CN(points->type);
852 if( depth != CV_32F && depth != CV_64F || cn != 1 && cn != 2 && cn != 3 )
853 CV_ERROR( CV_StsUnsupportedFormat, "The format of point matrix is unsupported" );
855 if( points->rows > points->cols )
857 dims = cn*points->cols;
858 count = points->rows;
862 if( points->rows > 1 && cn > 1 || points->rows == 1 && cn == 1 )
863 CV_ERROR( CV_StsBadSize, "The point matrix does not have a proper layout (2xn, 3xn, nx2 or nx3)" );
864 dims = cn * points->rows;
865 count = points->cols;
868 if( dims != 2 && dims != 3 )
869 CV_ERROR( CV_StsOutOfRange, "The dimensionality of points must be 2 or 3" );
871 if( !CV_IS_MAT(fmatrix) )
872 CV_ERROR( !fmatrix ? CV_StsNullPtr : CV_StsBadArg, "fmatrix is not a valid matrix" );
874 if( CV_MAT_TYPE(fmatrix->type) != CV_32FC1 && CV_MAT_TYPE(fmatrix->type) != CV_64FC1 )
875 CV_ERROR( CV_StsUnsupportedFormat, "fundamental matrix must have 32fC1 or 64fC1 type" );
877 if( fmatrix->cols != 3 || fmatrix->rows != 3 )
878 CV_ERROR( CV_StsBadSize, "fundamental matrix must be 3x3" );
880 if( !CV_IS_MAT(lines) )
881 CV_ERROR( !lines ? CV_StsNullPtr : CV_StsBadArg, "lines parameter is not a valid matrix" );
883 abc_depth = CV_MAT_DEPTH(lines->type);
884 abc_cn = CV_MAT_CN(lines->type);
885 if( abc_depth != CV_32F && abc_depth != CV_64F || abc_cn != 1 && abc_cn != 3 )
886 CV_ERROR( CV_StsUnsupportedFormat, "The format of the matrix of lines is unsupported" );
888 if( lines->rows > lines->cols )
890 abc_dims = abc_cn*lines->cols;
891 abc_count = lines->rows;
895 if( lines->rows > 1 && abc_cn > 1 || lines->rows == 1 && abc_cn == 1 )
896 CV_ERROR( CV_StsBadSize, "The lines matrix does not have a proper layout (3xn or nx3)" );
897 abc_dims = abc_cn * lines->rows;
898 abc_count = lines->cols;
902 CV_ERROR( CV_StsOutOfRange, "The lines matrix does not have a proper layout (3xn or nx3)" );
904 if( abc_count != count )
905 CV_ERROR( CV_StsUnmatchedSizes, "The numbers of points and lines are different" );
907 elem_size = CV_ELEM_SIZE(depth);
908 abc_elem_size = CV_ELEM_SIZE(abc_depth);
910 if( points->rows == dims )
912 plane_stride = points->step;
917 plane_stride = elem_size;
918 stride = points->rows == 1 ? dims*elem_size : points->step;
921 if( lines->rows == 3 )
923 abc_plane_stride = lines->step;
924 abc_stride = abc_elem_size;
928 abc_plane_stride = abc_elem_size;
929 abc_stride = lines->rows == 1 ? 3*abc_elem_size : lines->step;
932 CV_CALL( cvConvert( fmatrix, &F ));
933 if( pointImageID == 2 )
934 cvTranspose( &F, &F );
936 xp = points->data.ptr;
937 yp = xp + plane_stride;
938 zp = dims == 3 ? yp + plane_stride : 0;
940 ap = lines->data.ptr;
941 bp = ap + abc_plane_stride;
942 cp = bp + abc_plane_stride;
944 for( i = 0; i < count; i++ )
949 if( depth == CV_32F )
951 x = *(float*)xp; y = *(float*)yp;
953 z = *(float*)zp, zp += stride;
957 x = *(double*)xp; y = *(double*)yp;
959 z = *(double*)zp, zp += stride;
962 xp += stride; yp += stride;
964 a = f[0]*x + f[1]*y + f[2]*z;
965 b = f[3]*x + f[4]*y + f[5]*z;
966 c = f[6]*x + f[7]*y + f[8]*z;
968 nu = nu ? 1./sqrt(nu) : 1.;
969 a *= nu; b *= nu; c *= nu;
971 if( abc_depth == CV_32F )
973 *(float*)ap = (float)a;
974 *(float*)bp = (float)b;
975 *(float*)cp = (float)c;
994 cvConvertPointsHomogenious( const CvMat* src, CvMat* dst )
999 CV_FUNCNAME( "cvConvertPointsHomogenious" );
1003 int i, s_count, s_dims, d_count, d_dims;
1004 CvMat _src, _dst, _ones;
1007 if( !CV_IS_MAT(src) )
1008 CV_ERROR( !src ? CV_StsNullPtr : CV_StsBadArg,
1009 "The input parameter is not a valid matrix" );
1011 if( !CV_IS_MAT(dst) )
1012 CV_ERROR( !dst ? CV_StsNullPtr : CV_StsBadArg,
1013 "The output parameter is not a valid matrix" );
1015 if( src == dst || src->data.ptr == dst->data.ptr )
1017 if( src != dst && (!CV_ARE_TYPES_EQ(src, dst) || !CV_ARE_SIZES_EQ(src,dst)) )
1018 CV_ERROR( CV_StsBadArg, "Invalid inplace operation" );
1022 if( src->rows > src->cols )
1024 if( !((src->cols > 1) ^ (CV_MAT_CN(src->type) > 1)) )
1025 CV_ERROR( CV_StsBadSize, "Either the number of channels or columns or rows must be =1" );
1027 s_dims = CV_MAT_CN(src->type)*src->cols;
1028 s_count = src->rows;
1032 if( !((src->rows > 1) ^ (CV_MAT_CN(src->type) > 1)) )
1033 CV_ERROR( CV_StsBadSize, "Either the number of channels or columns or rows must be =1" );
1035 s_dims = CV_MAT_CN(src->type)*src->rows;
1036 s_count = src->cols;
1039 if( src->rows == 1 || src->cols == 1 )
1040 src = cvReshape( src, &_src, 1, s_count );
1042 if( dst->rows > dst->cols )
1044 if( !((dst->cols > 1) ^ (CV_MAT_CN(dst->type) > 1)) )
1045 CV_ERROR( CV_StsBadSize,
1046 "Either the number of channels or columns or rows in the input matrix must be =1" );
1048 d_dims = CV_MAT_CN(dst->type)*dst->cols;
1049 d_count = dst->rows;
1053 if( !((dst->rows > 1) ^ (CV_MAT_CN(dst->type) > 1)) )
1054 CV_ERROR( CV_StsBadSize,
1055 "Either the number of channels or columns or rows in the output matrix must be =1" );
1057 d_dims = CV_MAT_CN(dst->type)*dst->rows;
1058 d_count = dst->cols;
1061 if( dst->rows == 1 || dst->cols == 1 )
1062 dst = cvReshape( dst, &_dst, 1, d_count );
1064 if( s_count != d_count )
1065 CV_ERROR( CV_StsUnmatchedSizes, "Both matrices must have the same number of points" );
1067 if( CV_MAT_DEPTH(src->type) < CV_32F || CV_MAT_DEPTH(dst->type) < CV_32F )
1068 CV_ERROR( CV_StsUnsupportedFormat,
1069 "Both matrices must be floating-point (single or double precision)" );
1071 if( s_dims < 2 || s_dims > 4 || d_dims < 2 || d_dims > 4 )
1072 CV_ERROR( CV_StsOutOfRange,
1073 "Both input and output point dimensionality must be 2, 3 or 4" );
1075 if( s_dims < d_dims - 1 || s_dims > d_dims + 1 )
1076 CV_ERROR( CV_StsUnmatchedSizes,
1077 "The dimensionalities of input and output point sets differ too much" );
1079 if( s_dims == d_dims - 1 )
1081 if( d_count == dst->rows )
1083 ones = cvGetSubRect( dst, &_ones, cvRect( s_dims, 0, 1, d_count ));
1084 dst = cvGetSubRect( dst, &_dst, cvRect( 0, 0, s_dims, d_count ));
1088 ones = cvGetSubRect( dst, &_ones, cvRect( 0, s_dims, d_count, 1 ));
1089 dst = cvGetSubRect( dst, &_dst, cvRect( 0, 0, d_count, s_dims ));
1093 if( s_dims <= d_dims )
1095 if( src->rows == dst->rows && src->cols == dst->cols )
1097 if( CV_ARE_TYPES_EQ( src, dst ) )
1100 cvConvert( src, dst );
1104 if( !CV_ARE_TYPES_EQ( src, dst ))
1106 CV_CALL( temp = cvCreateMat( src->rows, src->cols, dst->type ));
1107 cvConvert( src, temp );
1110 cvTranspose( src, dst );
1114 cvSet( ones, cvRealScalar(1.) );
1118 int s_plane_stride, s_stride, d_plane_stride, d_stride, elem_size;
1120 if( !CV_ARE_TYPES_EQ( src, dst ))
1122 CV_CALL( temp = cvCreateMat( src->rows, src->cols, dst->type ));
1123 cvConvert( src, temp );
1127 elem_size = CV_ELEM_SIZE(src->type);
1129 if( s_count == src->cols )
1130 s_plane_stride = src->step / elem_size, s_stride = 1;
1132 s_stride = src->step / elem_size, s_plane_stride = 1;
1134 if( d_count == dst->cols )
1135 d_plane_stride = dst->step / elem_size, d_stride = 1;
1137 d_stride = dst->step / elem_size, d_plane_stride = 1;
1139 CV_CALL( denom = cvCreateMat( 1, d_count, dst->type ));
1141 if( CV_MAT_DEPTH(dst->type) == CV_32F )
1143 const float* xs = src->data.fl;
1144 const float* ys = xs + s_plane_stride;
1145 const float* zs = 0;
1146 const float* ws = xs + (s_dims - 1)*s_plane_stride;
1148 float* iw = denom->data.fl;
1150 float* xd = dst->data.fl;
1151 float* yd = xd + d_plane_stride;
1156 zs = ys + s_plane_stride;
1157 zd = yd + d_plane_stride;
1160 for( i = 0; i < d_count; i++, ws += s_stride )
1163 iw[i] = t ? t : 1.f;
1166 cvDiv( 0, denom, denom );
1169 for( i = 0; i < d_count; i++ )
1172 float x = *xs * w, y = *ys * w, z = *zs * w;
1173 xs += s_stride; ys += s_stride; zs += s_stride;
1174 *xd = x; *yd = y; *zd = z;
1175 xd += d_stride; yd += d_stride; zd += d_stride;
1178 for( i = 0; i < d_count; i++ )
1181 float x = *xs * w, y = *ys * w;
1182 xs += s_stride; ys += s_stride;
1184 xd += d_stride; yd += d_stride;
1189 const double* xs = src->data.db;
1190 const double* ys = xs + s_plane_stride;
1191 const double* zs = 0;
1192 const double* ws = xs + (s_dims - 1)*s_plane_stride;
1194 double* iw = denom->data.db;
1196 double* xd = dst->data.db;
1197 double* yd = xd + d_plane_stride;
1202 zs = ys + s_plane_stride;
1203 zd = yd + d_plane_stride;
1206 for( i = 0; i < d_count; i++, ws += s_stride )
1212 cvDiv( 0, denom, denom );
1215 for( i = 0; i < d_count; i++ )
1218 double x = *xs * w, y = *ys * w, z = *zs * w;
1219 xs += s_stride; ys += s_stride; zs += s_stride;
1220 *xd = x; *yd = y; *zd = z;
1221 xd += d_stride; yd += d_stride; zd += d_stride;
1224 for( i = 0; i < d_count; i++ )
1227 double x = *xs * w, y = *ys * w;
1228 xs += s_stride; ys += s_stride;
1230 xd += d_stride; yd += d_stride;
1237 cvReleaseMat( &denom );
1238 cvReleaseMat( &temp );