1

I'm working with OpenCV (3.3.1) in C++. I have an initialized Mat object and an array

Mat mat(2, 3, CV_32F, Scalar::all(0.5));
float arr[6] = {1,2,3,4,5,6};

I would like to assign the data from the array to the Mat object after the latter has been initialized. How can one do this efficiently?

I know that I can initialize the Mat object with the array using Mat mat(2, 3, CV_32F, arr); but I want to do the assignment after the initialization.

1 Answer 1

2

One option could be:

std::memcpy(mat.data, arr, mat.rows*mat.cols*sizeof(float));

Another option (maybe no suitable for you since maybe you need to keep some part of the old header):

mat = Mat(2, 3, CV_32F, arr)
Sign up to request clarification or add additional context in comments.

3 Comments

The first method looks good, but is it the most efficient way even when working with large arrays?
@DanielP I am not sure. However, if you do not really need to copy the data, you should try to change your design and accept the array in the initializing stage. Show us your original problem, someone could find you a better solution
Yes, if I didn't need to copy the data, the second option you mentioned would work. For copying the first option seems to work well! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.