I couldn't find an answer to this question but I believe it should be easily done.
Here I have following data structures;
Mat MyMatrix = Mat(3, 1, CV_64F, &targetArray) //Some 3x1 data in it
// Some process...
array <double ,3> MyArray
MyMatrix.convertTo(MyArray, double, 0 , DBL_MAX)
I want to convert MyMatrix (which I guarantee to be 3x1) to an 1D Array (Array elements should be double). How can I do that with C++ and opencv3.0.1?
You cannot convert a cv::Mat
to a std::array
. You can only copy the data into an array.
Given a
Mat m = Mat(3, 1, CV_64F);
// fill with some value
you can use memcpy
:
array<double, 3> a;
memcpy(a.data(), m.ptr<double>(0), 3*sizeof(double));
or std::copy
:
array<double, 3> b;
copy(m.begin<double>(), m.end<double>(), b.begin());
or, since it's only 3 elements, the array constructor:
array<double, 3> c = {m.at<double>(0), m.at<double>(1), m.at<double>(2)};
or, obviously, with a loop:
array<double, 3> d;
for (int i = 0; i < d.size(); ++i) { d[i] = m.at<double>(i); }
Conversion is instead possible with std::vector
:
vector<double> e = m;
Note that, instead of a matrix with only 3 values, you can use Vec3d
, or Matx31
or Matx13
. Using Mat1d
would however simplify the notation and make the code less verbose.