Question

I have detected a number of points from 2 images. I am trying to find a transformation matrix between these 2 images. So I need to do an inverse of the matrix coordinates of one matrix and then multiply it to the coordinates of the other Mat matrix. I used very simple opencv methods but I get this error http://i.imgur.com/eDF1e9p.jpg. I do not understand why. Here is my code http://pastebin.com/Tef42E2Q. Can anybody guide me here please?

Was it helpful?

Solution

You should make sure at least two things before Mat inverse.

  1. The Matrix should be square.

  2. The Matrix should be non-singular, that is determinant should be non-zero.

Eg:

    Mat A(3,3,CV_32FC1);
    A=(Mat_<float>(3,3)<< 0,2,3,\
                          4,5,6,\
                          7,8,9);
    cout<<A <<endl;

    if(determinant(A)!=0){
    Mat B=A.inv();
    cout<<B <<endl;
    }

Also see the answer Mat.inv() yielding all zeroes in opencv

Edit:-

Here is some bug I found in your code

The piece of code

    Mat matrix_f(2,3,CV_32F);
    matrix_f=Mat(coordinates_f);

should changed to

    Mat matrix_f(2,3,CV_32F);
    matrix_f=Mat(coordinates_f);
    matrix_f=matrix_f.reshape(1,2);

because later you are going to multiply with a 3X3 Mat, So we will make it's rows to 3

And next is change the lines

    Mat new_left(3,3,CV_32F);
    new_left=Mat(coordinates_l_new);

to

      Mat new_left(3,3,CV_32F);
      new_left=Mat(coordinates_l_new);
      new_left=new_left.reshape(1,3);

as you are going to find the inverse of new_left, it should be square matrix.

And finally make sure the Mat is non-singular by finding determinant

 if(determinant(new_left)!=0) {
        Mat T(3,3,CV_32F);
        T=matrix_f * (new_left.inv());
  } 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top