Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Convert cv::Mat to std::vector without copying

I've this function:

void foo(cv::Mat &mat){
  float *p = mat.ptr<float>(0);
  //modify mat values through p
}

And I have this code that calls the function:

void bar(std::vector<unsigned char> &vec){
  //...
  cv::Mat res(m, n, CV_32FC1, (void *)&workspace.front());
}

However, the code above has a performance problem: vec isn't probably aligned. In fact, the Intel compiler says that reference *out has unaligned access. I hope that this is the reason.

Btw, as I found out, cv::Mat is going to be aligned. So a simple workaround would be to:

  1. Create cv::Mat res(m,n)
  2. Call foo(m);
  3. Assign the vec pointer to m.ptr<float>(0)

As you can imagine, performance here are crucial, so deep-copies are out of questions.

I tried the code in this question:

vec = res.data;

But I get a compiler error. Besides, I don't know if this code above is going to do an (inefficient) copy, or just change the pointed data by vec.

How can I do this smoothly? Otherwise, another workaround would be appreciated.