解线性超定方程

int numPoints=10;

double **a=new double *[2*numPoints];
 for(int i = 0; i <2*numPoints;i++)
  a[i] = new double[8];
 double *b =new double[2*numPoints];

 CvMat A = cvMat (2*numPoints,8, CV_64F, a);
 CvMat B = cvMat (2*numPoints,1, CV_64F, b);
 CvMat *X = cvCreateMat (8, 1, CV_64F);
 for( int i = 0; i <numPoints; ++i )
 {
  a[i][0] = a[i+numPoints][3] =points1[i].x;
  a[i][1] = a[i+numPoints][4] =points1[i].y;
  a[i][2] = a[i+numPoints][5] = 1;
  a[i][3] = a[i][4] = a[i][5] =0;
  a[i+numPoints][0] = a[i+numPoints][1] = a[i+numPoints][2] = 0;

  a[i][6] = -points1[i].x*points2[i].x;
  a[i][7] = -points1[i].y*points2[i].x;
  a[i+numPoints][6] = -points1[i].x*points2[i].y;
  a[i+numPoints][7] = -points1[i].y*points2[i].y;

  b[i] =points2[i].x;
  b[i+numPoints] =points2[i].y;
 }
 
 cvSolve(&A,&B,X,CV_SVD);
 for(int i=0;i<8;i++)
  cvmSet(H_Mat2,i/3,i%3,cvmGet(X,i,0));
 cvmSet(H_Mat2,2,2,1.0);

 for(int i = 0; i<2*numPoints;i++)
 {
  delete [8]a[i];
  a[i]=NULL;
 }
 delete [2*numPoints]a;
 delete [2*numPoints]b;
 a=NULL;
 b=NULL;

你可能感兴趣的:(解线性超定方程)