Ask Your Question
0

How to pass the cv::Mat or cv::cuda::GpuMat to custom CUDA kernel?

asked 2019-06-06 08:30:15 -0600

hua gravatar image

Is there an efficient way to pass the Mat to CUDA custom kernel? Can someone please help me with an example?

Thanks a million, Hua

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-06-24 12:55:51 -0600

Dhruvin Naik gravatar image

I usually pass GpuMat to kernel like this:

__global__ void Kernel_CalPhaseInfo2D(const cv::cuda::PtrStepSzf dReal, const cv::cuda::PtrStepSzf dImag, const float d_Pi, cv::cuda::PtrStepSzf dOutput) { int iCol = blockIdx.x * blockDim.x + threadIdx.x; int iRow = blockIdx.y * blockDim.y + threadIdx.y;

    if (iCol < dReal.cols && iRow < dReal.rows && iRow >= 0 && iCol >= 0)
    {
        dOutput(iRow, iCol) = (atan2(dImag(iRow, iCol), dReal(iRow, iCol)) * (-1) + d_Pi) / (2 * d_Pi);
    }
}

void CalculatePhaseInfo2D(const cv::InputArray _input0, const cv::InputArray _input1, cv::OutputArray _output) { const cv::cuda::GpuMat input0 = _input0.getGpuMat(); const cv::cuda::GpuMat input1 = _input1.getGpuMat();

_output.create(input0.size(), input0.type());

cv::cuda::GpuMat output0 = _output.getGpuMat();

dim3 cthreads(32, 32);
dim3 cblocks(
    static_cast<int>(std::ceil(input0.size().width /
        static_cast<double>(cthreads.x))),
    static_cast<int>(std::ceil(input0.size().height /
        static_cast<double>(cthreads.y))));

Kernel_CalPhaseInfo2D << <cblocks, cthreads >> > (input0, input1, M_PI, output0);

if (cudaSuccess != cudaGetLastError())
    std::cout << "CalculatePhaseInfo2D(): gave an error" << std::endl;

return;

}

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-06-06 08:30:15 -0600

Seen: 1,558 times

Last updated: Jun 06 '19