mans
mans

Reputation: 18148

How to get a section of a matrix in opencv

I have this matrix in openCV:

  cv::Matx44d m;

and I want to get the top left 3x3 matrix out of this matrix. What is the simplest 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 a simpler and faster way if it exist!

Upvotes: 6

Views: 2780

Answers (3)

karsten
karsten

Reputation: 693

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 );

Upvotes: 0

Michael Burdinov
Michael Burdinov

Reputation: 4428

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

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

Template parameters <3,3> is the height and width of small matrix. Value (0,0) is the starting point from which the matrix is cropped.

Upvotes: 11

berak
berak

Reputation: 39796

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) );

Upvotes: 1

Related Questions