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

I have one problem for which I have not found any solution.

I need to do 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 contents of the matrix will be:

 1.81381e-29, 15.1628, -7.57361e+17, 0, -0, 0, 5.4561e-33, -2.40123e+34, -1.38198e-05 

This, of course, is wrong, since I already checked the result in Matlab. Both matrices are displayed and read as floating, and their depth is 64FC1 .

Does anyone know what can be done?

Thank you 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 , because if I print the elements separately, I get the same strange result in the return numbers.

+4
source share
1 answer

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

I interpreted the CV_64F data as floating, this is an error, they need to be treated as double, for writing values ​​and reading them. ( <double> )

However, CV_32F can be considered floating, access will be from <float> .

+11
source

All Articles