【问题标题】:How to create thrust device_vector from opencv gpumat如何从 opencv gpumat 创建推力 device_vector
【发布时间】:2019-03-18 21:22:58
【问题描述】:

我需要将 opencv::gpumat 转换为推力::device_vector,然后执行推力::copy_if,然后将生成的推力::device_vector 再次复制到 cv::gpumat。

如何从 cv::gpumat 转换为推力::device_vector 反之亦然?

我的代码(如下)给出了编译错误: 错误:需要一个类型说明符

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

nvcc:Cuda 编译工具,8.0 版,V8.0.44

推力 v1.8

ubuntu 16.04

我的代码:

#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;
}

【问题讨论】:

  • 该链接提供了如何从原始指针到 gpuMat 的想法。我需要先将 gpuMat 转换为推力::device_ptr,然后从 device_ptr 转换为 gpuMat

标签: compiler-errors cuda opencv3.0 thrust


【解决方案1】:

如何从 cv::gpumat 转换为推力::device_vector ....

这是无法做到的。 Thrust 没有公开从现有指针构造device_vector 的机制。复制构造是 device_vector 可以从现有数据实例化的唯一方法。

.... 反之亦然?

这是可以做到的:

  1. device_vector 强制转换为原始指针(请参阅here
  2. 从原始指针创建一个 openCV gpumat 实例(参见here

请注意,如果您让device_vector 超出范围,则会调用其析构函数,并且支持它的内存(以及使用相同内存的任何其他内容)将被释放,并且您处于未定义的行为领域。

【讨论】:

    猜你喜欢
    • 2012-03-18
    • 2015-05-11
    • 1970-01-01
    • 2013-01-04
    • 2016-10-29
    • 2013-06-08
    • 2018-11-25
    • 2012-06-22
    相关资源
    最近更新 更多