1

I try to read a camera frame and show it through cv::namedWindow using cv::cuda::GpuMat.

Here is my C++ code:

cv::namedWindow("frame", cv::WINDOW_OPENGL);
cv::resizeWindow("frame", FRAME_WIDTH, FRAME_HEIGHT);
while (true) {
    cv::Mat frame;
    cv::cuda::GpuMat frame_gpu;

    camera.read(frame);
    frame_gpu.upload(frame);

    cv::imshow("frame", frame_gpu);

    //frame_gpu.download(frame);

    if (cv::waitKey(1) == 27) {
        break;
    }
}
cv::destroyAllWindows();

If I close the window I got this error:

OpenCV Error: The function/feature is not implemented (You should explicitly call download method for cuda::GpuMat object) in getMat_, file /home/nvidia/opencv-3.2.0/modules/core/src/matrix.cpp, line 1276
terminate called after throwing an instance of 'cv::Exception'
what():  /home/nvidia/opencv-3.2.0/modules/core/src/matrix.cpp:1276: error: (-213) You should explicitly call download method for cuda::GpuMat object in function getMat_

Aborted (core dumped)

If I type Esc key to end the logic, it does not raise any exception.

Why do I get this error and how can I solve this?

ghchoi
  • 4,812
  • 4
  • 30
  • 53

1 Answers1

0

I guess the error is about you trying to display GpuMat image using imshow. You need to download it to another Mat before you can display it using imshow. Try this

cv::Mat host;
frame_gpu.upload(frame);
frame_gpu.download(host)
cv::imshow("frame", host);
MarKS
  • 494
  • 6
  • 22
  • Then isn't there any advantage to use `cv::cuda::GpuMat`? – ghchoi Aug 18 '17 at 08:39
  • What kind of advantage do you expect from using GpuMat the way you do? – KjMag Aug 18 '17 at 08:46
  • @GyuHyeon Choi you see the advantage processing a GpuMat on GPU. What you are doing is copying the Mat from CPU to GPU and vice-versa and nothing else. Maybe try reading what GPU does actually. http://opencv.org/platforms/cuda.html – MarKS Aug 18 '17 at 09:04
  • Uh... I omitted other parts of code to make it more readable. There are multiple things to do with GPU so I thought it would be better to pass `GpuMat` to `imshow` if `GpuMat.download` is a bit expensive? – ghchoi Aug 18 '17 at 19:16
  • yes! it is expensive. Uploading and downloading from CPU to GPU takes time. – MarKS Aug 23 '17 at 08:36