6

When I compile this example:

#include <iostream>
#include "opencv2/opencv.hpp"
#include "opencv2/gpu/gpu.hpp"

int main (int argc, char* argv[])

{
    try
    {
        cv::Mat src_host = cv::imread("file.png", CV_LOAD_IMAGE_GRAYSCALE);
        cv::gpu::GpuMat dst, src;
        src.upload(src_host);

        cv::gpu::threshold(src, dst, 128.0, 255.0, CV_THRESH_BINARY);

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

I got the following error:

threshold.cpp: In function ‘int main(int, char**)’:
threshold.cpp:19: error: conversion from ‘cv::gpu::GpuMat’ to non-scalar type ‘cv::Mat’ requested

Does anybody knows why?

sgarizvi
  • 16,623
  • 9
  • 64
  • 98
Chito Webster
  • 73
  • 2
  • 5

2 Answers2

6

In the current versions of OpenCV, the cv::Mat class has no overloaded assignment operator or copy constructor which takes argument of type cv::gpu::GpuMat. So the following line of your code will not compile.

cv::Mat result_host = dst;

There are 2 alternatives to this.

First you can pass the dst as the argument of the constructor of result_host.

cv::Mat result_host(dst);

Second is that you can call the download function of dst

cv::Mat result_host;
dst.download(result_host);
sgarizvi
  • 16,623
  • 9
  • 64
  • 98
  • thanks for your response. Now when executing this file the following error: /// OpenCV Error: Unknown error code -216 (The library is compiled without CUDA support) in copy, file /home/cbib/Descargas/OpenCV-2.4.3/modules/core/src/gpumat.cpp, line 736 ///// you know what can be caused? – Chito Webster Jan 23 '13 at 17:56
  • @ChitoWebster... This is because you are using the OpenCV binaries compiled without CUDA support. Check [this answer](http://stackoverflow.com/questions/13228762/opencv-2-4-3rc-and-cuda-4-2-opencv-error-no-gpu-support/13231205#13231205) in which the same issue has been resolved. – sgarizvi Jan 24 '13 at 06:28
3

It seems that you should use download method of gpuMat to convert it to cv::Mat:

//! downloads data from device to host memory. Blocking calls.
        void download(cv::Mat& m) const;

See this doc.

ArtemStorozhuk
  • 8,715
  • 4
  • 35
  • 53
  • 1
    @ChitoWebster my answer was actually also correct (you can try to +1 it). About your new error read here: http://stackoverflow.com/questions/12910902/opencv-error-no-gpu-support-library-is-compiled-without-cuda-support – ArtemStorozhuk Jan 23 '13 at 23:20