Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error with gpumat and mat

Tags:

c++

opencv

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?

like image 600
Chito Webster Avatar asked Jan 22 '13 21:01

Chito Webster


2 Answers

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);
like image 135
sgarizvi Avatar answered Nov 16 '22 11:11

sgarizvi


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.

like image 28
ArtemStorozhuk Avatar answered Nov 16 '22 10:11

ArtemStorozhuk