Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inverse Matrix OpenCV. Matrix.inv() not working properly

I have one problem for which I could not find any solution.

I have to make some calculations with the inverse of one known matrix.

Matrix homography=

1.1688, 0.23, 62.2,

-0.013,1.225, -6.29,

0, 0, 1, 

and then:

Mat homoInv=homography.inv();

The content of the matrix would be:

1.81381e-29, 15.1628, -7.57361e+17, 

0, -0, 0, 

5.4561e-33, -2.40123e+34, -1.38198e-05

That of course is wrong as I already checked the result in Matlab. Both matrix are displayed and read as floats, and their depth is a 64FC1.

Does anyone have any idea of what could be done?

Thanks All

More code:

int main(int argc, char ** argv )
{ 
  Mat homogra(3,3,CV_64FC1);
  Mat coord(3,1,CV_64FC1);
  Mat result(target.size(),CV_8UC1);
  homogra.at<float>(0,0)=1.1688;
  homogra.at<float>(0,1)=0.23;
  homogra.at<float>(0,2)=(-62.20);
  homogra.at<float>(1,0)=(-0.013);
  homogra.at<float>(1,1)=1.225;
  homogra.at<float>(1,2)=-6.29;
  homogra.at<float>(2,0)=0;
  homogra.at<float>(2,1)=0;
  homogra.at<float>(2,2)=1;
  printMatrix(homogra);

  Mat inverse=homogra.inv();
  printMatrix(inverse);
}

function printMatrix:

void printMatrix(Mat M){
cout<<"Tipo de matriz:"<<M.type()<<endl;
 // dont print empty matrices
  if (M.empty()){
    cout << "---" << endl;
    return;
  }
  // loop through columns and rows of the matrix
  for(int i=0; i < M.rows; i++){
      for(int j=0; j < M.cols ; j++){
      cout << M.at<float>(i,j) << ", "<<endl;
      }
    cout<<"Change\n"<<endl;
}
  }

But the error is not in printMatrix, as If i print the elements separately I obtain the same strange result in the numbers of inverse.

like image 419
Ivánovick Avatar asked Jun 13 '12 12:06

Ivánovick


1 Answers

The problem was, as Peter pointed out, in my code. I still don't understand the reason deeply but it is like:

I was treating the data CV_64F as float, it is a mistake, it needs to be treated as double, for writing values and reading them.(<double>)

However CV_32F can be treated as float, the access would be with <float>.

like image 143
Ivánovick Avatar answered Oct 18 '22 11:10

Ivánovick