Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a section of a matrix in opencv

Tags:

c++

opencv

matrix

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!

like image 444
mans Avatar asked Oct 20 '14 09:10

mans


People also ask

What is CV_32FC1?

CV_32F defines the depth of each element of the matrix, while. CV_32FC1 defines both the depth of each element and the number of channels.

What is use of mat class in OpenCV?

The Mat class of OpenCV library is used to store the values of an image. It represents an n-dimensional array and is used to store image data of grayscale or color images, voxel volumes, vector fields, point clouds, tensors, histograms, etc.

What is a CV :: mat?

In OpenCV the main matrix class is called Mat and is contained in the OpenCV-namespace cv. This matrix is not templated but nevertheless can contain different data types. These are indicated by a certain type-number. Additionally, OpenCV provides a templated class called Mat_, which is derived from Mat.

What is cv_64f?

Here's example for CV_64FC1 : CV_ - this is just a prefix. 64 - number of bits per base matrix element (e.g. pixel value in grayscale image or single color element in BGR image) F - type of the base element.


2 Answers

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.

like image 64
Michael Burdinov Avatar answered Nov 14 '22 21:11

Michael Burdinov


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) );
like image 25
berak Avatar answered Nov 14 '22 21:11

berak