开发者

Copying a GpuMat row to a std::vector

开发者 https://www.devze.com 2023-04-06 16:26 出处:网络
How can I transfer cv::gpu::GpuMat rows to std::vector with as little 开发者_StackOverflowas possible copy operations?

How can I transfer cv::gpu::GpuMat rows to std::vector with as little 开发者_StackOverflowas possible copy operations?

The fastest way I can think of is:

GpuMat fooGpu(numRows, numCols, CV_32FC1);
Mat foo = fooGpu;

Mat fooRow = foo.row(i);

std::vector<float> vec;
vec.resize(numCols);

memcpy(&vec[0], fooRow.data, sizeof(float)*numCols);

But I'm not even sure if this works, because the fooRow content would have to be aligned...

Is there another (better) way to do it?


Here is the method that does not produce any unnecessary copying:

GpuMat fooGpu(numRows, numCols, CV_32FC1);
std::vector<float> vec;
vec.resize(numCols);
fooGpu.row(i).download(Mat(vec).reshape(1/*channels*/, 1/*rows*/));


I think std::copy is better:

std::vector<float> vec;
vec.resize(numCols); 
std::copy(fooRow.data, fooRow.data + numCols, vec.begin());

Note that the second argument is : fooRow.data + numCols, as opposed to fooRow.data + sizeof(float)* numCols.

Also, in your code vec.resize(numRows); doesn't seem to be correct. It should be :

 vec.resize(numCols);

Because fooRow is ONE row, and hasnumCols number of values in it.


Possibly this post may answer your question.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号