C++ 逆矩阵 OpenCV。Matrix.inv() 无法正常工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11015119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:44:50  来源:igfitidea点击:

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

c++opencvmatrix-inverse

提问by Ivánovick

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.

那当然是错误的,因为我已经在 Matlab 中检查了结果。两个矩阵都作为浮点数显示和读取,它们的深度是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.

但错误不在printMatrix,因为如果我单独打印元素,我会在逆数中获得相同的奇怪结果。

回答by Ivánovick

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_64Fas float, it is a mistake, it needs to be treated as double, for writing values and reading them.(<double>)

我将数据CV_64F视为浮点数,这是一个错误,需要将其视为双精度值,以便写入值和读取值。( <double>)

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

但是CV_32F可以将其视为浮动,访问将使用<float>.