1

I'm not very familiar with cv::Mat. I met a question while memory copy.

I get some data from gpu and create a Mat using these data.

// frame_height and frame_width = 112, frame_step = 512
cv::Mat frame = cv::Mat(frame_height, frame_width, CV_8UC3, src_data, frame_step);

However, the memory of these data is not Continuous which causes lots of trouble. Then I do some operations on the Mat frame. For example, the cv::warpPerspective:

cv::warpPerspective(frame, warpImg, M, cv::Size(112, 112),cv::INTER_LINEAR);

the output Mat warpImg has the same size of frame: 112 * 112 * 3. But it is Continuous, which means I can't cover it back to gpu directly since the size of memory in gpu is: 112 * 512 * 3.

I copy them by rows temporarily:

    for (int i = 0; i < frame.rows; i++)
    {
        uchar *data = frame.ptr<uchar>(i);
        uchar *data2 = warpImg.ptr<uchar>(i);
        for (int j = 0; j < frame.cols * frame.channels(); j++)
        {
            data[j] = data2[j];
            
        }
    }

It works though it is ugly, I want to know if there is a better way to achieve this.

Thanks in advance.

  • How did you try to copy? If type and number of rows/cols is identicsl I would assume mat.copyTo assume to work, even if the destination has a padding, but I didnt try it ... – Micka Jul 08 '22 at 09:33
  • The other way could be to allocate pixels of warpImg before warping and allocate including the padding. – Micka Jul 08 '22 at 09:34
  • You could just create your matrix with the right stride. – Goswin von Brederlow Jul 08 '22 at 10:45
  • 1
    @Micka Thanks! It does work using `copyTo`, also, I found that I can directly use `Mat frame` as **src** and **dst** at the same time which avoids coping step. Anyway, thanks you guys. – Scarlet Zhou Jul 11 '22 at 08:18

0 Answers0