Using gpu::GpuMat in OpenCV C++

好久不见. 提交于 2019-12-10 23:46:58

问题


I would like to know how can I modify a gpu::GpuMat. In fact I would like to know if it is possible to use a gpu::GpuMat like a cv::Mat.

I would like to do something like that:

    cv::namedWindow("Result");
    cv::Mat src_host = cv::imread("lena.jpg", CV_LOAD_IMAGE_GRAYSCALE);
    cv::gpu::GpuMat dst, src;
    src.upload(src_host);


    for (unsigned int y = 0; y < src.rows; y++){
        for (unsigned int x = 0; x < src.cols; x++){
            src.at<uchar>(y,x) = 0;
        }
    }

    cv::Mat result_host;
    dst.download(result_host);
    cv::imshow("Result", result_host);
    cv::waitKey();
}
catch(const cv::Exception& ex)
{
    std::cout << "Error: " << ex.what() << std::endl;
}
return 0;

It seems to be not possible. Does someone know an alternative ? Do I have to download my gpu::GpuMat to a cv::Mat each time I want to make something ? Thanks in advance

Edit: I could probably use gpu::PtrStepSz (have to test...)


回答1:


GpuMat is allocated in GPU memory. You can't modify it from CPU code. You can

  1. Set all matrix pixels to the same value (setTo method).
  2. Fill data on CPU using cv::Mat and then upload it to GpuMat.
  3. Implement your own CUDA kernel and fill GpuMat in it.


来源:https://stackoverflow.com/questions/17679470/using-gpugpumat-in-opencv-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!