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.
45 #define Sgn(x) ( (x)<0 ? -1:1 ) /* Sgn(0) = 1 ! */
46 /*===========================================================================*/
48 icvLMedS( int *points1, int *points2, int numPoints, CvMatrix3 * fundamentalMatrix )
50 int sample, j, amount_samples, done;
69 if( fundamentalMatrix == 0 )
70 return CV_BADFACTOR_ERR;
76 return CV_BADFACTOR_ERR;
79 ml = (int *) cvAlloc( sizeof( int ) * num * 3 );
80 mr = (int *) cvAlloc( sizeof( int ) * num * 3 );
82 for( i = 0; i < num; i++ )
85 ml[i * 3] = points1[i * 2];
86 ml[i * 3 + 1] = points1[i * 2 + 1];
90 mr[i * 3] = points2[i * 2];
91 mr[i * 3 + 1] = points2[i * 2 + 1];
100 amount_samples = 1000; /* ------- Must be changed ! --------- */
102 for( sample = 0; sample < amount_samples; sample++ )
105 icvChoose7( ml, mr, num, ml7, mr7 );
106 icvPoint7( ml7, mr7, F_try, &amount_solutions );
108 for( i = 0; i < amount_solutions / 9; i++ )
111 Mj_new = icvMedian( ml, mr, num, F_try + i * 9 );
113 if( Mj_new >= 0 && (Mj == -1 || Mj_new < Mj) )
116 for( j = 0; j < 9; j++ )
119 F[j] = F_try[i * 9 + j];
128 return CV_BADFACTOR_ERR;
130 done = icvBoltingPoints( ml, mr, num, F, Mj, &new_ml, &new_mr, &new_num );
137 return CV_OUTOFMEM_ERR;
141 error = icvPoints8( new_ml, new_mr, new_num, F );
149 error = icvPoint7( ml, mr, F, &i );
152 if( error == CV_NO_ERR )
153 error = icvRank2Constraint( F );
155 for( i = 0; i < 3; i++ )
156 for( j = 0; j < 3; j++ )
157 fundamentalMatrix->m[i][j] = (float) F[i * 3 + j];
163 /*===========================================================================*/
164 /*===========================================================================*/
166 icvChoose7( int *ml, int *mr, int num, int *ml7, int *mr7 )
168 int indexes[7], i, j;
170 if( !ml || !mr || num < 7 || !ml7 || !mr7 )
173 for( i = 0; i < 7; i++ )
176 indexes[i] = (int) ((double) rand() / RAND_MAX * num);
178 for( j = 0; j < i; j++ )
181 if( indexes[i] == indexes[j] )
186 for( i = 0; i < 21; i++ )
189 ml7[i] = ml[3 * indexes[i / 3] + i % 3];
190 mr7[i] = mr[3 * indexes[i / 3] + i % 3];
194 /*===========================================================================*/
195 /*===========================================================================*/
197 icvCubic( double a2, double a1, double a0, double *squares )
199 double p, q, D, c1, c2, b1, b2, ro1, ro2, fi1, fi2, tt;
204 return CV_BADFACTOR_ERR;
206 p = a1 - a2 * a2 / 3;
207 q = (9 * a1 * a2 - 27 * a0 - 2 * a2 * a2 * a2) / 27;
208 D = q * q / 4 + p * p * p / 27;
218 ro1 = sqrt( c1 * c1 - D );
221 fi1 = atan2( b1, c1 );
227 c1 = q / 2 + sqrt( D );
228 c2 = q / 2 - sqrt( D );
234 fi1 = CV_PI * (1 - SIGN( c1 )) / 2;
235 fi2 = CV_PI * (1 - SIGN( c2 )) / 2;
238 for( i = 0; i < 6; i++ )
245 squares[i] = x[i][i % 2];
248 if( !REAL_ZERO( ro1 ))
250 tt = SIGN( ro1 ) * pow( fabs( ro1 ), 0.333333333333 );
251 c1 = tt - p / (3. * tt);
252 c2 = tt + p / (3. * tt);
255 if( !REAL_ZERO( ro2 ))
257 tt = SIGN( ro2 ) * pow( fabs( ro2 ), 0.333333333333 );
258 b1 = tt - p / (3. * tt);
259 b2 = tt + p / (3. * tt);
262 for( i = 0; i < 6; i++ )
268 if( !REAL_ZERO( ro1 ))
271 x[i][0] = cos( fi1 / 3. + 2 * CV_PI * (i % 3) / 3. ) * c1 - a2 / 3;
272 x[i][1] = sin( fi1 / 3. + 2 * CV_PI * (i % 3) / 3. ) * c2;
283 if( !REAL_ZERO( ro2 ))
286 x[i][0] = cos( fi2 / 3. + 2 * CV_PI * (i % 3) / 3. ) * b1 - a2 / 3;
287 x[i][1] = sin( fi2 / 3. + 2 * CV_PI * (i % 3) / 3. ) * b2;
299 for( i = 0; i < 6; i++ )
305 squares[t++] = x[i][0];
306 squares[t++] = x[i][1];
309 for( j = i + 1; j < 6; j++ )
312 if( !x[j][2] && REAL_ZERO( x[i][0] - x[j][0] )
313 && REAL_ZERO( x[i][1] - x[j][1] ))
325 /*======================================================================================*/
334 value = M[0] * M[4] * M[8] + M[2] * M[3] * M[7] + M[1] * M[5] * M[6] -
335 M[2] * M[4] * M[6] - M[0] * M[5] * M[7] - M[1] * M[3] * M[8];
341 /*===============================================================================*/
343 icvMinor( double *M, int x, int y )
345 int row1, row2, col1, col2;
348 if( !M || x < 0 || x > 2 || y < 0 || y > 2 )
351 row1 = (y == 0 ? 1 : 0);
352 row2 = (y == 2 ? 1 : 2);
353 col1 = (x == 0 ? 1 : 0);
354 col2 = (x == 2 ? 1 : 2);
356 value = M[row1 * 3 + col1] * M[row2 * 3 + col2] - M[row2 * 3 + col1] * M[row1 * 3 + col2];
358 value *= 1 - (x + y) % 2 * 2;
364 /*======================================================================================*/
366 icvGetCoef( double *f1, double *f2, double *a2, double *a1, double *a0 )
371 if( !f1 || !f2 || !a0 || !a1 || !a2 )
372 return CV_BADFACTOR_ERR;
374 for( i = 0; i < 9; i++ )
377 G[i] = f1[i] - f2[i];
383 return CV_BADFACTOR_ERR;
389 for( i = 0; i < 9; i++ )
392 *a2 += f2[i] * icvMinor( G, (int) (i % 3), (int) (i / 3) );
393 *a1 += G[i] * icvMinor( f2, (int) (i % 3), (int) (i / 3) );
404 /*===========================================================================*/
406 icvMedian( int *ml, int *mr, int num, double *F )
408 double l1, l2, l3, d1, d2, value;
412 if( !ml || !mr || !F )
415 deviation = (double *) cvAlloc( (num) * sizeof( double ));
420 for( i = 0, i3 = 0; i < num; i++, i3 += 3 )
423 l1 = F[0] * mr[i3] + F[1] * mr[i3 + 1] + F[2];
424 l2 = F[3] * mr[i3] + F[4] * mr[i3 + 1] + F[5];
425 l3 = F[6] * mr[i3] + F[7] * mr[i3 + 1] + F[8];
427 d1 = (l1 * ml[i3] + l2 * ml[i3 + 1] + l3) / sqrt( l1 * l1 + l2 * l2 );
429 l1 = F[0] * ml[i3] + F[3] * ml[i3 + 1] + F[6];
430 l2 = F[1] * ml[i3] + F[4] * ml[i3 + 1] + F[7];
431 l3 = F[2] * ml[i3] + F[5] * ml[i3 + 1] + F[8];
433 d2 = (l1 * mr[i3] + l2 * mr[i3 + 1] + l3) / sqrt( l1 * l1 + l2 * l2 );
435 deviation[i] = (double) (d1 * d1 + d2 * d2);
438 if( icvSort( deviation, num ) != CV_NO_ERR )
441 cvFree( &deviation );
445 value = deviation[num / 2];
446 cvFree( &deviation );
451 /*===========================================================================*/
453 icvSort( double *array, int length )
458 if( !array || length < 1 )
459 return CV_BADFACTOR_ERR;
461 for( i = 0; i < length - 1; i++ )
466 for( j = i + 1; j < length; j++ )
469 if( array[j] < array[index] )
477 array[i] = array[index];
478 array[index] = swapd;
486 /*===========================================================================*/
488 icvBoltingPoints( int *ml, int *mr,
489 int num, double *F, double Mj, int **new_ml, int **new_mr, int *new_num )
491 double l1, l2, l3, d1, d2, sigma;
495 if( !ml || !mr || num < 1 || !F || Mj < 0 )
498 index = (int *) cvAlloc( (num) * sizeof( int ));
504 sigma = (double) (2.5 * 1.4826 * (1 + 5. / (num - 7)) * sqrt( Mj ));
506 for( i = 0; i < num * 3; i += 3 )
509 l1 = F[0] * mr[i] + F[1] * mr[i + 1] + F[2];
510 l2 = F[3] * mr[i] + F[4] * mr[i + 1] + F[5];
511 l3 = F[6] * mr[i] + F[7] * mr[i + 1] + F[8];
513 d1 = (l1 * ml[i] + l2 * ml[i + 1] + l3) / sqrt( l1 * l1 + l2 * l2 );
515 l1 = F[0] * ml[i] + F[3] * ml[i + 1] + F[6];
516 l2 = F[1] * ml[i] + F[4] * ml[i + 1] + F[7];
517 l3 = F[2] * ml[i] + F[5] * ml[i + 1] + F[8];
519 d2 = (l1 * mr[i] + l2 * mr[i + 1] + l3) / sqrt( l1 * l1 + l2 * l2 );
521 if( d1 * d1 + d2 * d2 <= sigma * sigma )
536 *new_ml = (int *) cvAlloc( (length * 3) * sizeof( int ));
545 *new_mr = (int *) cvAlloc( (length * 3) * sizeof( int ));
557 for( i = 0; i < num * 3; )
563 (*new_ml)[j] = ml[i];
564 (*new_mr)[j++] = mr[i++];
565 (*new_ml)[j] = ml[i];
566 (*new_mr)[j++] = mr[i++];
567 (*new_ml)[j] = ml[i];
568 (*new_mr)[j++] = mr[i++];
577 } /* cs_BoltingPoints */
579 /*===========================================================================*/
581 icvPoints8( int *ml, int *mr, int num, double *F )
584 double l1, l2, w, old_norm = -1, new_norm = -2, summ;
585 int i3, i9, j, num3, its = 0, a, t;
587 if( !ml || !mr || num < 8 || !F )
588 return CV_BADFACTOR_ERR;
590 U = (double *) cvAlloc( (num * 9) * sizeof( double ));
593 return CV_OUTOFMEM_ERR;
597 while( !REAL_ZERO( new_norm - old_norm ))
604 return CV_BADFACTOR_ERR;
609 for( i3 = 0, i9 = 0; i3 < num3; i3 += 3, i9 += 9 )
612 l1 = F[0] * mr[i3] + F[1] * mr[i3 + 1] + F[2];
613 l2 = F[3] * mr[i3] + F[4] * mr[i3 + 1] + F[5];
615 if( REAL_ZERO( l1 ) && REAL_ZERO( l2 ))
619 return CV_BADFACTOR_ERR;
622 w = 1 / (l1 * l1 + l2 * l2);
624 l1 = F[0] * ml[i3] + F[3] * ml[i3 + 1] + F[6];
625 l2 = F[1] * ml[i3] + F[4] * ml[i3 + 1] + F[7];
627 if( REAL_ZERO( l1 ) && REAL_ZERO( l2 ))
631 return CV_BADFACTOR_ERR;
634 w += 1 / (l1 * l1 + l2 * l2);
637 for( j = 0; j < 9; j++ )
640 U[i9 + j] = w * (double) ml[i3 + j / 3] * (double) mr[i3 + j % 3];
646 for( a = 0; a < num; a++ )
651 for( t = 0; t < 9; t++ )
654 summ += U[a * 9 + t] * F[t];
657 new_norm += summ * summ;
660 new_norm = sqrt( new_norm );
662 icvAnalyticPoints8( U, num, F );
670 /*===========================================================================*/
672 icvAnalyticPoints8( double *A, int num, double *F )
682 double norm, summ, best_norm;
683 int num8 = num * 8, num9 = num * 9;
684 int i, j, j8, j9, value, a, a8, a9, a_num, b, b8, t;
686 /* --------- Initialization data ------------------ */
688 if( !A || num < 8 || !F )
692 U = (double *) cvAlloc( (num8) * sizeof( double ));
697 f = (double *) cvAlloc( (num) * sizeof( double ));
705 temp2 = (double *) cvAlloc( (num8) * sizeof( double ));
714 A_short = (double *) cvAlloc( (num8) * sizeof( double ));
724 for( i = 0; i < 8; i++ )
726 for( j8 = 0, j9 = 0; j9 < num9; j8 += 8, j9 += 9 )
728 A_short[j8 + i] = A[j9 + i + 1];
732 for( i = 0; i < 9; i++ )
735 for( j = 0, j8 = 0, j9 = 0; j < num; j++, j8 += 8, j9 += 9 )
741 A_short[j8 + i - 1] = A[j9 + i - 1];
744 value = icvSingularValueDecomposition( num, 8, A_short, W, 1, U, 1, V );
747 { /* ----------- computing the solution ----------- */
749 /* ----------- W = W(-1) ----------- */
750 for( j = 0; j < 8; j++ )
752 if( !REAL_ZERO( W[j] ))
756 /* ----------- temp1 = V * W(-1) ----------- */
757 for( a8 = 0; a8 < 64; a8 += 8 )
759 for( b = 0; b < 8; b++ )
761 temp1[a8 + b] = V[a8 + b] * W[b];
765 /* ----------- temp2 = V * W(-1) * U(T) ----------- */
766 for( a8 = 0, a_num = 0; a8 < 64; a8 += 8, a_num += num )
768 for( b = 0, b8 = 0; b < num; b++, b8 += 8 )
771 temp2[a_num + b] = 0;
773 for( t = 0; t < 8; t++ )
776 temp2[a_num + b] += temp1[a8 + t] * U[b8 + t];
781 /* ----------- solution = V * W(-1) * U(T) * f ----------- */
782 for( a = 0, a_num = 0; a < 8; a++, a_num += num )
784 for( b = 0; b < num; b++ )
789 for( t = 0; t < num && W[a]; t++ )
791 solution[a] += temp2[a_num + t] * f[t];
796 for( a = 8; a > 0; a-- )
801 solution[a] = solution[a - 1];
808 for( a9 = 0; a9 < num9; a9 += 9 )
813 for( t = 0; t < 9; t++ )
816 summ += A[a9 + t] * solution[t];
824 if( best_norm == -1 || norm < best_norm )
827 for( j = 0; j < 9; j++ )
842 } /* cs_AnalyticPoints8 */
844 /*===========================================================================*/
846 icvRank2Constraint( double *F )
848 double U[9], V[9], W[3];
853 return CV_BADFACTOR_ERR;
855 if( icvSingularValueDecomposition( 3, 3, F, W, 1, U, 1, V ))
856 return CV_BADFACTOR_ERR;
867 if( REAL_ZERO( W[0] ))
875 if( REAL_ZERO( W[2] ))
887 if( REAL_ZERO( W[1] ))
894 if( REAL_ZERO( W[2] ))
901 for( i = 0; i < 3; i++ )
903 for( j3 = 0; j3 < 9; j3 += 3 )
909 for( i = 0, i3 = 0; i < 3; i++, i3 += 3 )
911 for( j = 0, j3 = 0; j < 3; j++, j3 += 3 )
916 for( t = 0; t < 3; t++ )
918 F[i3 + j] += U[i3 + t] * V[j3 + t];
924 } /* cs_Rank2Constraint */
927 /*===========================================================================*/
930 icvSingularValueDecomposition( int M,
933 double *W, int get_U, double *U, int get_V, double *V )
935 int i = 0, j, k, l = 0, i1, k1, l1 = 0;
936 int iterations, error = 0, jN, iN, kN, lN = 0;
938 double c, f, g, h, s, x, y, z, scale, anorm;
939 double af, ag, ah, t;
943 /* max_iterations - maximum number QR-iterations
944 cc - reduces requirements to number stitch (cc>1)
947 int max_iterations = 100;
953 rv1 = (double *) cvAlloc( N * sizeof( double ));
958 for( iN = 0; iN < MN; iN += N )
960 for( j = 0; j < N; j++ )
961 U[iN + j] = A[iN + j];
964 /* Adduction to bidiagonal type (transformations of reflection).
965 Bidiagonal matrix is located in W (diagonal elements)
966 and in rv1 (upperdiagonal elements)
973 for( i = 0, iN = 0; i < N; i++, iN += N )
980 /* Multiplyings on the left */
986 for( kN = iN; kN < MN; kN += N )
987 scale += fabs( U[kN + i] );
989 if( !REAL_ZERO( scale ))
992 for( kN = iN; kN < MN; kN += N )
996 s += U[kN + i] * U[kN + i];
1000 g = -sqrt( s ) * Sgn( f );
1004 for( j = l; j < N; j++ )
1009 for( kN = iN; kN < MN; kN += N )
1012 s += U[kN + i] * U[kN + j];
1017 for( kN = iN; kN < MN; kN += N )
1020 U[kN + j] += f * U[kN + i];
1024 for( kN = iN; kN < MN; kN += N )
1030 /* Multiplyings on the right */
1036 for( k = l; k < N; k++ )
1037 scale += fabs( U[iN + k] );
1039 if( !REAL_ZERO( scale ))
1042 for( k = l; k < N; k++ )
1046 s += (U[iN + k]) * (U[iN + k]);
1050 g = -sqrt( s ) * Sgn( f );
1052 U[i * N + l] = f - g;
1054 for( k = l; k < N; k++ )
1055 rv1[k] = U[iN + k] / h;
1057 for( jN = lN; jN < MN; jN += N )
1062 for( k = l; k < N; k++ )
1063 s += U[jN + k] * U[iN + k];
1065 for( k = l; k < N; k++ )
1066 U[jN + k] += s * rv1[k];
1070 for( k = l; k < N; k++ )
1075 t += fabs( rv1[i] );
1076 anorm = MAX( anorm, t );
1081 /* accumulation of right transformations, if needed */
1086 for( i = N - 1, iN = NN - N; i >= 0; i--, iN -= N )
1092 /* pass-by small g */
1093 if( !REAL_ZERO( g ))
1096 for( j = l, jN = lN; j < N; j++, jN += N )
1097 V[jN + i] = U[iN + j] / U[iN + l] / g;
1099 for( j = l; j < N; j++ )
1104 for( k = l, kN = lN; k < N; k++, kN += N )
1105 s += U[iN + k] * V[kN + j];
1107 for( kN = lN; kN < NN; kN += N )
1108 V[kN + j] += s * V[kN + i];
1112 for( j = l, jN = lN; j < N; j++, jN += N )
1126 /* accumulation of left transformations, if needed */
1131 for( i = N - 1, iN = NN - N; i >= 0; i--, iN -= N )
1138 for( j = l; j < N; j++ )
1141 /* pass-by small g */
1142 if( !REAL_ZERO( g ))
1145 for( j = l; j < N; j++ )
1150 for( kN = lN; kN < MN; kN += N )
1151 s += U[kN + i] * U[kN + j];
1153 f = s / U[iN + i] / g;
1155 for( kN = iN; kN < MN; kN += N )
1156 U[kN + j] += f * U[kN + i];
1159 for( jN = iN; jN < MN; jN += N )
1165 for( jN = iN; jN < MN; jN += N )
1173 /* Iterations QR-algorithm for bidiagonal matrixes
1174 W[i] - is the main diagonal
1175 rv1[i] - is the top diagonal, rv1[0]=0.
1178 for( k = N - 1; k >= 0; k-- )
1187 /* Cycle: checking a possibility of fission matrix */
1188 for( l = k; l >= 0; l-- )
1193 if( REAL_ZERO( rv1[l] ) || REAL_ZERO( W[l1] ))
1197 if( !REAL_ZERO( rv1[l] ))
1200 /* W[l1] = 0, matrix possible to fission
1201 by clearing out rv1[l] */
1206 for( i = l; i <= k; i++ )
1210 rv1[i] = c * rv1[i];
1212 /* Rotations are done before the end of the block,
1213 or when element in the line is finagle.
1221 /* Scaling prevents finagling H ( F!=0!) */
1227 h = ag * sqrt( 1 + (f / g) * (f / g) );
1229 h = af * sqrt( 1 + (f / g) * (f / g) );
1238 for( jN = 0; jN < MN; jN += N )
1243 U[jN + l1] = y * c + z * s;
1244 U[jN + i] = -y * s + z * c;
1251 /* Output in this place of program means,
1252 that rv1[L] = 0, matrix fissioned
1253 Iterations of the process of the persecution
1254 will be executed always for
1255 the bottom block ( from l before k ),
1256 with increase l possible.
1264 /* Completion iterations: lower block
1265 became trivial ( rv1[K]=0) */
1267 if( iterations++ == max_iterations )
1270 /* Shift is computed on the lowest order 2 minor. */
1277 /* consequent fission prevents forming a machine zero */
1278 f = ((y - z) * (y + z) + (g - h) * (g + h)) / (2 * h) / y;
1280 /* prevented overflow */
1284 g *= sqrt( 1 + (1 / f) * (1 / f) );
1287 g = sqrt( f * f + 1 );
1289 f = ((x - z) * (x + z) + h * (y / (f + fabs( g ) * Sgn( f )) - h)) / x;
1293 for( i1 = l; i1 <= k1; i1++ )
1302 /* Scaling at calculation Z prevents its clearing,
1303 however if F and H both are zero - pass-by of fission on Z.
1310 z = ah * sqrt( 1 + (f / h) * (f / h) );
1316 if( !REAL_ZERO( af ))
1317 z = af * sqrt( 1 + (h / f) * (h / f) );
1322 /* if Z=0, the rotation is free. */
1323 if( !REAL_ZERO( z ))
1338 for( jN = 0; jN < NN; jN += N )
1343 V[jN + i1] = x * c + z * s;
1344 V[jN + i] = -x * s + z * c;
1352 z = ah * sqrt( 1 + (f / h) * (f / h) );
1357 if( !REAL_ZERO( af ))
1358 z = af * sqrt( 1 + (h / f) * (h / f) );
1363 if( !REAL_ZERO( z ))
1376 for( jN = 0; jN < MN; jN += N )
1381 U[jN + i1] = y * c + z * s;
1382 U[jN + i] = -y * s + z * c;
1400 for( jN = 0; jN < NN; jN += N )
1410 } /* vm_SingularValueDecomposition */
1412 /*========================================================================*/
1414 /* Obsolete functions. Just for ViewMorping */
1415 /*=====================================================================================*/
1418 icvGaussMxN( double *A, double *B, int M, int N, double **solutions )
1421 int row, swapi, i, i_best = 0, j, j_best = 0, t;
1422 double swapd, ratio, bigest;
1424 if( !A || !B || !M || !N )
1427 variables = (int *) cvAlloc( (size_t) N * sizeof( int ));
1429 if( variables == 0 )
1432 for( i = 0; i < N; i++ )
1437 /* ----- Direct way ----- */
1439 for( row = 0; row < M; row++ )
1444 for( j = row; j < M; j++ )
1445 { /* search non null element */
1446 for( i = row; i < N; i++ )
1448 double a = fabs( A[j * N + i] ), b = fabs( bigest );
1451 bigest = A[j * N + i];
1458 if( REAL_ZERO( bigest ))
1459 break; /* if all shank elements are null */
1464 for( t = 0; t < N; t++ )
1467 swapd = A[row * N + t];
1468 A[row * N + t] = A[j_best * N + t];
1469 A[j_best * N + t] = swapd;
1480 for( t = 0; t < M; t++ )
1481 { /* swap a columns */
1483 swapd = A[t * N + i_best];
1484 A[t * N + i_best] = A[t * N + row];
1485 A[t * N + row] = swapd;
1488 swapi = variables[row];
1489 variables[row] = variables[i_best];
1490 variables[i_best] = swapi;
1493 for( i = row + 1; i < M; i++ )
1494 { /* recounting A and B */
1496 ratio = -A[i * N + row] / A[row * N + row];
1497 B[i] += B[row] * ratio;
1499 for( j = N - 1; j >= row; j-- )
1502 A[i * N + j] += A[row * N + j] * ratio;
1508 { /* if rank(A)<M */
1510 for( j = row; j < M; j++ )
1512 if( !REAL_ZERO( B[j] ))
1515 cvFree( &variables );
1516 return -1; /* if system is antithetic */
1520 M = row; /* decreasing size of the task */
1523 /* ----- Reverse way ----- */
1526 { /* if solution are not exclusive */
1528 *solutions = (double *) cvAlloc( ((N - M + 1) * N) * sizeof( double ));
1530 if( *solutions == 0 )
1532 cvFree( &variables );
1537 for( t = M; t <= N; t++ )
1539 for( j = M; j < N; j++ )
1542 (*solutions)[(t - M) * N + variables[j]] = (double) (t == j);
1545 for( i = M - 1; i >= 0; i-- )
1546 { /* finding component of solution */
1550 (*solutions)[(t - M) * N + variables[i]] = 0;
1554 (*solutions)[(t - M) * N + variables[i]] = B[i] / A[i * N + i];
1557 for( j = i + 1; j < N; j++ )
1560 (*solutions)[(t - M) * N + variables[i]] -=
1561 (*solutions)[(t - M) * N + variables[j]] * A[i * N + j] / A[i * N + i];
1566 cvFree( &variables );
1570 *solutions = (double *) cvAlloc( (N) * sizeof( double ));
1572 if( solutions == 0 )
1575 for( i = N - 1; i >= 0; i-- )
1576 { /* finding exclusive solution */
1578 (*solutions)[variables[i]] = B[i] / A[i * N + i];
1580 for( j = i + 1; j < N; j++ )
1583 (*solutions)[variables[i]] -=
1584 (*solutions)[variables[j]] * A[i * N + j] / A[i * N + i];
1588 cvFree( &variables );
1593 /*=====================================================================================*/
1596 icvGetCoof( double *f1, double *f2, double *a2, double *a1, double *a0 )
1601 if( !f1 || !f2 || !a0 || !a1 || !a2 )
1602 return CV_BADFACTOR_ERR;
1604 for( i = 0; i < 9; i++ )
1607 G[i] = f1[i] - f2[i];
1612 if( REAL_ZERO( a3 ))
1613 return CV_BADFACTOR_ERR;
1619 for( i = 0; i < 9; i++ )
1622 *a2 += f2[i] * icvMinor( G, (int) (i % 3), (int) (i / 3) );
1623 *a1 += G[i] * icvMinor( f2, (int) (i % 3), (int) (i / 3) );
1632 }*/ /* icvGetCoof */
1636 /*======================================================================================*/
1638 /*F///////////////////////////////////////////////////////////////////////////////////////
1653 // CV_NO_ERR if all Ok or error code
1658 icvLMedS7( int *points1, int *points2, CvMatrix3 * matrix )
1659 { /* Incorrect realization */
1660 CvStatus error = CV_NO_ERR;
1667 /* error = cs_Point7( points1, points2, matrix ); */
1668 /* error = icvPoint7 ( points1, points2, matrix,&amount ); */
1674 /*======================================================================================*/
1675 /*F///////////////////////////////////////////////////////////////////////////////////////
1690 // CV_NO_ERR if all Ok or error code
1695 icvPoint7( int *ml, int *mr, double *F, int *amount )
1706 CvStatus error = CV_BADFACTOR_ERR;
1708 /* F = (float*)matrix->m; */
1710 if( !ml || !mr || !F )
1711 return CV_BADFACTOR_ERR;
1713 for( i = 0; i < 7; i++ )
1715 for( j = 0; j < 9; j++ )
1718 A[i * 9 + j] = (double) ml[i * 3 + j / 3] * (double) mr[i * 3 + j % 3];
1725 if( icvGaussMxN( A, B, 7, 9, &solutions ) == 2 )
1727 if( icvGetCoef( solutions, solutions + 9, &a2, &a1, &a0 ) == CV_NO_ERR )
1729 icvCubic( a2, a1, a0, squares );
1731 for( i = 0; i < 1; i++ )
1734 if( REAL_ZERO( squares[i * 2 + 1] ))
1737 for( j = 0; j < 9; j++ )
1740 F[*amount + j] = (float) (squares[i] * solutions[j] +
1741 (1 - squares[i]) * solutions[j + 9]);
1750 cvFree( &solutions );
1755 cvFree( &solutions );
1761 cvFree( &solutions );