简体   繁体   English

OpenCV - 将CUDA设备数据复制到GPU Mat中

[英]OpenCV - copy CUDA device data into GPU Mat

Is there a way to directly copy previously allocated CUDA device data into an OpenCV GPU Mat? 有没有办法直接将以前分配的CUDA设备数据复制到OpenCV GPU Mat中? I would like to copy my data, previously initialized and filled by CUDA, into the OpenCV GPU mat. 我想将以前初始化并由CUDA填充的数据复制到OpenCV GPU垫中。 I would like to do so because I want solve a linear system of equations Ax = B by computing the inverse of the matrix A using OpenCV. 我想这样做是因为我想通过使用OpenCV计算矩阵A的逆来求解线性方程组Ax = B

What I want to do is something like this: 我想做的是这样的:

float *dPtr; 
gpuErrchk( cudaMalloc( (void**) &dPtr, sizeof(float) * height * width));    
gpuErrchk( cudaMemset(dPtr, 0, sizeof(float) * height * width));

// modify dPtr in some way on the GPU 
modify_dPtr(); 

// copy previously allocated and modified dPtr into OpenCV GPU mat? 

// process GPU mat later - e.x. do a matrix inversion operation. 

// extract raw pointer from GPU mat

EDIT: The OpenCV documentation provides a GPU upload function. 编辑: OpenCV 文档提供了GPU upload功能。

Can the device pointer just be passed into that function as a parameter? 设备指针是否可以作为参数传递给该函数? If not, is there no other way to do such a data transfer? 如果没有,是否没有其他方法可以进行这样的数据传输? I don't want to copy data back and forth between the host and device memory, do my computation on a normal OpenCV Mat container, and copy back the results; 我不想在主机和设备内存之间来回复制数据,在普通的OpenCV Mat容器上进行计算,并复制结果; my application is real-time. 我的申请是实时的。 I am assuming that since there is no .at() function for a GPU Mat , as in the normal OpenCV Mat , there is no way to access the element at a particular location in the matrix? 我假设因为GPU Mat没有.at()函数,就像在普通的OpenCV Mat ,没有办法访问矩阵中特定位置的元素? Also, does an explicit matrix inversion operation exist for the GPU Mat? 此外,GPU Mat是否存在显式矩阵求逆运算? The documentation does not provide a GPU Mat inv() function. 该文档未提供GPU Mat inv()函数。

Just as talonmies posted in the comments, there is a constructor in the header of the GPU mat structure that allows the creation of a GPUMat header pointing to my previously allocated CUDA device data. 正如评论中发布的talonmies一样,GPU垫结构的标题中有一个构造函数,允许创建指向我之前分配的CUDA设备数据的GPUMat标头。 This is what I had used: 这是我用过的:

cv::gpu::GpuMat dst(height, width, CV_32F, d_Ptr);

There is no need to figure out the step size because the constructor automatically evaluates it, given the width and height of the image. 没有必要弄清楚步长,因为在给定图像的宽度和高度的情况下,构造函数会自动计算它。 Hopefully, when the support for OpenCV GPU functions becomes better, this post may be useful to someone. 希望当对OpenCV GPU功能的支持变得更好时,这篇文章可能对某些人有用。

EDIT 编辑

Another (probably) useful way is to utilize unified memory in CUDA. 另一种(可能)有用的方法是在CUDA中使用统一内存。 Pass the data into an OpenCV GPU and CPU mat, and continue operations from there. 将数据传递到OpenCV GPU和CPU垫,然后从那里继续操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM