How to get a matrix section in opencv

I have this matrix in openCV:

  cv::Matx44d m;

      

and I want to get the top left 3x3 matrix from this matrix. What's the easiest and fastest way to do this?

I can do it in the following ways:

cv::Matx44d m;
cv::Matx33d o;
for(int i=0;i<3;i++)
{
    for(int j=0;j<3;j++)
    {
       o(i,j)=m(i,j);
    }
 }

      

but I am looking for an easier and faster way if it exists!

+3


source to share


3 answers


Matx has a get_minor () function that does exactly what you want. I don't see it in the OpenCV documentation, but it is present inside the implementation. In your case, it would be:

o = m.get_minor<3,3>(0,0);

      



Template parameters <3,3> are the height and width of the small matrix. The value (0,0) is the starting point from which the matrix is ​​clipped.

+6


source


why not use a simple constructor?



Matx44d m = ...;
Mat33xd o( m(0), m(1), m(2),
           m(4), m(5), m(6),
           m(8), m(9), m(10) );

      

+1


source


How about this?

//! creates a matrix header for a part of the bigger matrix
Mat(const Mat& m, const Range& rowRange, const Range& colRange=Range::all());
Mat(const Mat& m, const Rect& roi);
Mat(const Mat& m, const Range* ranges);

      

So you can write: Mat part = Mat (A, rect);

0


source







All Articles