1

I need to convert a opencv::gpumat to a thrust::device_vector, then perform a thrust::copy_if , then copy the resultant thrust::device_vector to a cv::gpumat again.

How to convert from cv::gpumat to thrust::device_vector & vice-versa?

My code(below) gives compilation error: error: expected a type specifier

gcc (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609

nvcc: Cuda compilation tools, release 8.0, V8.0.44

Thrust v1.8

ubuntu 16.04

My code:

#include <thrust/random.h>

#include <thrust/device_ptr.h>
#include <thrust/device_vector.h>

#include <opencv2/core/cuda.hpp>

int main(void)
{
    cv::Mat input_host(1,100,CV_32F);
    cv::Mat output_host(1,100,CV_32F);

    cv::randu(input_host, cv::Scalar(-10), cv::Scalar(10));
    cv::cuda::GpuMat input_device(1, 100, CV_32F);
    input_device.upload(input_host);

    thrust::device_ptr<CV_32F> input_ptr(input_device.ptr());

    thrust::device_vector<CV_32F>   input_device_vector  (input_ptr,100);
    return 0;
}
user27665
  • 673
  • 7
  • 27
  • https://docs.opencv.org/3.4/d0/d60/classcv_1_1cuda_1_1GpuMat.html#ab7210166f4bd124855b520b3dde28fb1 – talonmies Oct 14 '18 at 12:01
  • the link gives an idea of how to go from the raw pointer to the gpuMat. I need to first convert a gpuMat to a thrust::device_ptr and then from a device_ptr to a gpuMat – user27665 Oct 14 '18 at 17:37

1 Answers1

4

How to convert from cv::gpumat to thrust::device_vector ....

This cannot be done. Thrust doesn't expose a mechanism to construct a device_vector from an existing pointer. Copy construction is the only way that a device_vector can be instantiated from existing data.

.... & vice-versa?

This can be done:

  1. Cast the device_vector to a raw pointer (see here)
  2. Create an openCV gpumat instance from the raw pointer (see here)

Be aware that if you let the device_vector fall out of scope, its destructor will be called and the memory which backs it (and anything else which uses the same memory) will be freed and you are in undefined behaviour territory.

talonmies
  • 70,661
  • 34
  • 192
  • 269