• Advertisement
Sign in to follow this  

using array values to fill a Matrix

This topic is 2159 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I am a rather unexperienced programmer and new to this forum.

In my current C++ project I am working a lot with matrices (using openCV) and arrays.

For calculations I need to use cv::Mat and often I want to put the values from an array in an cv::Mat

say I got the previously filled array "double A[3][3]" and I want to get a matrix with the same values. Now I would like to copy the values directly, something like so: Mat MatA = Mat(A). But as far as I know, cv::Mat doesn't take an array in any of it's constructors. So my workaround for this is to create another type cv::CvMat which CAN take an array, and then copying the data to a cv::Mat type, like so:

CvMat CvMA = cvMat(3, 3, CV_64FC1, A);
const CvMat* ptr = &CvMA;
Mat MatA = Mat(ptr, true);


This annoys me as I have the feeling it could be done way better. Does anyone know how? Otherwise I feel like writing a function just for that, so I don't have to type 3 lines each time.

Also, if I wanted to fill an array again with the values from a cv::Mat, I only know how to do so by fetching each value individually, like
A[1][2] = MatA.at<double>(1,2);

I could write a for loop to fill them all. But once again, is there a better way to do this?



I

Share this post


Link to post
Share on other sites
Advertisement
Ok, I worked around it by making this function:

cv::Mat Transform::arrayMat(double *a, int r, int c)
{
CvMat CvMA = cvMat(r, c, CV_64FC1, a);
const CvMat* ptr = &CvMA;
Mat MatA = Mat(ptr, true);
return MatA;
}


then calling it like this (where A is a previously filled 3x3 array)

Mat MatA = arrayMat(&A[0][0], 3, 3);

If there is a standard way to achieve the same, I'd like to know

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement