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

2019-09-14 23:22发布

问题:

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 in this question, 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.

回答1:

Take a look here

std::vector<uchar> array;
if (mat.isContinuous()) {
  array.assign(mat.datastart, mat.dataend);
} else {
  for (int i = 0; i < mat.rows; ++i) {
    array.insert(array.end(), mat.ptr<uchar>(i), mat.ptr<uchar>(i)+mat.cols);
  }
}

//p.s.: For cv::Mats of other types, like CV_32F, you should do like this:

std::vector<float> array;
if (mat.isContinuous()) {
  array.assign((float*)mat.datastart, (float*)mat.dataend);
} else {
  for (int i = 0; i < mat.rows; ++i) {
    array.insert(array.end(), (float*)mat.ptr<uchar>(i), (float*)mat.ptr<uchar>(i)+mat.cols);
  }
}