How to copy a continuous Mat to an uncontinuous Mat

79 views Asked by At

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.

0

There are 0 answers