How to access cv::cuda::Gpumat in cuda kernel

tags: C++

High-performance image algorithms usually use GPU acceleration, and the cuda module in OpenCV provides commonly used algorithm functions that can be run directly in the GPU. For complex applications, the functions in the cuda module cannot meet the requirements. In this case, you need to write your own cuda kernel.

The following code is an example of accessing OpenCV's data structure cv::cuda::Gpumat in the cuda kernel:

#include <cuda_runtime.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/cudaarithm.hpp>


__global__ void Kernel_CalPhase2D(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 CalculatePhase2D(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, 3.14159f, output0);

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

    return;
}

 

Intelligent Recommendation

OpenCV+CUDA introductory tutorial part 6 --- visit every element of GpuMat

table of Contents 1. CUDA minimalist tutorial Second, visit each element of GpuMat 1. CUDA minimalist tutorial This part is just a super simple and incomplete content of CUDA. For CUDA configuration a...

CUDA tasted ----- how to enter debug kernel function

                                 &n...

CUDA | Kernel Concurrency

Want to implement a kernel function concurrency function Study the code in an official Samples concurrentKernels...

printf in CUDA kernel function

Excerpt from "cuda-c-programming-guide" B.17. Formatted Output http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#formatted-output Formatted output is only supported by devices ...

More Recommendation

CUDA programming - kernel nested

Written in front: Many algorithms need kernel nesting, just learn CUDA may be more chaotic, so I have a record here. (Nuclear nested cannot be compatible with cmakelist, there is no particularly good ...

CUDA kernel discusses parallel

If the image is reversed, the result is correct. I want to use multiple current parallel computing, but the use of NSIGHT to see the scheduling is serial. Where is the problem? NSIGHT Observation CUDA...

CUDA programming (two) CUDA initialization and kernel function

CUDA programming (two) CUDA initialization and kernel function CUDA initialization As mentioned in the last time, after the CUDA installation is successful, it is very simple to create a new project. ...

Qt + Cuda, Problem: No Kernel Image Is Available for Execution On The Device In Function 'CV :: Cudev :: Grid

The main reason is that the graphics card computing power is not corresponding. Dang, first check the power version corresponding to the graphics card in your computer! The CUDA integration settings s...

CUDA

Open a picture, listen to me later Knowledge preparation 1.1 Central Processing Unit (CPU) The Central Processing Unit (CPU) is a very large-scale integrated circuit that is the computing core (Core) ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top