Reputation: 30086
I'm using opencv with Eigen.
Here is a sample on how to link an opencv Mat to an Eigen Matrix: OpenCV CV::Mat and Eigen::Matrix
The key is to use Eigen's Map type, which can point to the memory allocated by opencv.
According to the Eigen docs, the Map is supposed to be transparent, virtually all operations that work on matrices should work on Map, too. https://eigen.tuxfamily.org/dox/group__TutorialMapClass.html
Here is a some code from the link above. m2map
points to the memory of a matrix called m2
:
m2map(3) = 7; // this will change m2, since they share the same array
But for me, even the simplest assignment fails:
Eigen::Map<Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>> eigenHeatmap(heatmap.ptr<float>(), heatmap.rows, heatmap.cols);
eigenHeatmap=0.f;
produces:
/.../Code.cpp:69:25: error: no match for ‘operator=’ (operand types are ‘Eigen::Map<Eigen::Matrix<float, -1, -1, 1> >’ and ‘float’)
eigenHeatmap=0.f;
The whole error message is rather long: https://pastebin.com/i3AWs6C7
I'm using eigen3.3.3, opencv3.2.0 and g++ 5.4.0
Upvotes: 1
Views: 1522
Reputation: 18817
Alternative to Avi's answer, if you are doing lots of element-wise operations, use Array
instead of Matrix
, i.e.
Eigen::Map<Eigen::Array<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>> eigenHeatmap(heatmap.ptr<float>(), heatmap.rows, heatmap.cols);
eigenHeatmap=0.f;
And if you use that a lot, make some typedefs first:
typedef Eigen::Array<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> ArrayXXfR;
typedef Eigen::Map<ArrayXXfR> ArrayMap;
ArrayMap eigenHeatmap(heatmap.ptr<float>(), heatmap.rows, heatmap.cols);
Addendum: For more details about the Array
class, read the corresponding tutorial here.
Upvotes: 3
Reputation: 10596
You should try eigenHeatmap.setZero();
or eigenHeatmap.setConstant(0.f);
instead.
Upvotes: 3