Ask Your Question

Passing an array of cv::GpuMat to a cuda kernel?

asked 2016-03-01 22:41:15 -0500

liquidmetal gravatar image

updated 2016-03-02 08:10:36 -0500

I'm trying to write my own cuda kernel but I can't find a way to pass an array of cv::GpuMat to the cuda kernel.

Here is what I have until now:

__global__ void somename(cv::cuda::PtrStepSz<float> *array_of_images) {
int main(...) {
    cv::cuda::GpuMat *mats = new cv::cuda::GpuMat[180]();
    // Initialize eachof the mats
    somename<<<...>>>(mats);           // Doesn't work

The compile error that the above snippet produces is: error: argument of type "cv::cuda::GpuMat *" is incompatible with parameter of type "cv::cuda::PtrStepSz<float> *"

What's the correct way to pass an array of images to a cuda kernel?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2016-03-03 01:01:56 -0500

Vladislav Vinogradov gravatar image

You need to manually convert array of GpuMat to array of PtrStepSz:

cv::cuda::GpuMat *mats = new cv::cuda::GpuMat[180];
// Initialize eachof the mats

cv::cuda::PtrStepSz<float> h_ptrs = new cv::cuda::PtrStepSz<float>[180];
for (int i = 0; i < 180; ++i)
    h_ptrs[i] = mats[i];

Then you need to copy h_ptrs to GPU memory:

cv::cuda::PtrStepSz<float> *d_ptrs = NULL;
cudaMalloc(&d_ptrs, 180 * sizeof(cv::cuda::PtrStepSz<float>));

cudaMemcpy(d_ptrs, h_ptrs, 180 * sizeof(cv::cuda::PtrStepSz<float>), cudaMemcpyHostToDevice);

Then you can pass d_ptrs to kernel:

edit flag offensive delete link more

answered 2017-05-26 00:28:19 -0500

josh97 gravatar image

updated 2020-06-13 11:18:58 -0500

HYPEREGO gravatar image

You can directly pass the array of GpuMats to kernels, but use them as an array of PtrStepSz in the kernel. You don't have to copy it to GPU memory.


In the kernel, access the array in this way:

__global__ void somename(PtrStepSz<float> *mats)
    mats[0] (y,x)=3;
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2016-03-01 22:41:15 -0500

Seen: 3,586 times

Last updated: Jun 13