3

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...)

3
  • 1
    I don't really see how this is a CUDA programming problem. Perhaps you could remove the CUDA tag from the question? Commented Jul 16, 2013 at 16:01
  • 1
    I want to use gpu::Mat which is provided by Opencv over Cuda... Commented Jul 16, 2013 at 16:06
  • 1
    Yes, but nothing in your code or question is actually anything to do with CUDA, only OpenCV classes which tangentially refer to abstractions with CUDA buried inside them somewhere. By the same logic you should tag your question with "windows" or "linux", because your code runs OpenCV over the top of the operating system as well.... Commented Jul 16, 2013 at 17:09

1 Answer 1

12

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.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks ! that's what I was thinking ... :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.