I’m trying to learn how to use OpenCV’s new C++ interface.
How do I access elements of a multi channel matrix? For example:
Mat myMat(size(3, 3), CV_32FC2);
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
//myMat_at_(i,j) = (i,j);
}
}
What is the easiest way to do this? Something like cvSet2D of the old interface.
What is the most efficient way? Similar to using direct pointers in the old interface.
Update : for OpenCV2.0
1. choose one type to represent the element
Mat (or CvMat) has 3 dimensions: row, col, channel.
We can access one element (or pixel) in the matrix by specifying the row and col.
CV_32FC2means the element is 32bit floating point value with 2 channels.So elem in above code is one acceptable representation of
CV_32FC2.You can use other representations you like. For example :
OpenCV2.0 adds some new types to represent the element in the matrix,like :
So we can use
Vec<float,2>to representCV_32FC2, or use :See the source code to get more type that can represent your element.
Here we use
Vec2f2. access the element
The easiest and efficiant way to access the element in the Mat class is Mat::at.
It has 4 overloads :
3. interact with old interface
Mat provides 2 conversion functions: