据我所知,cv::cuda::PtrStep
用于将 GpuMat
数据直接传递到自定义内核。我找到了单 channel 访问的示例here不过我的情况是 2 channel 垫 (CV_32FC2
)。在这种情况下,我试图实现复数绝对平方值,其中复数值的编码如下:实部是给定 Mat 的第一个平面,虚部是第二个平面。
我尝试过:
__global__ void testKernel(const cv::cuda::PtrStepSz<cv::Vec2f> input, cv::cuda::PtrStepf output)
{
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x <= input.cols - 1 && y <= input.rows - 1 && y >= 0 && x >= 0)
{
float val_re = input(x, y)[0];
float val_im = input(x, y) [1];
output(x, y) = val_re * val_re + val_im * val_im;
}
}
但这会导致以下错误:
calling a __host__ function("cv::Vec<float, (int)2> ::operator []") from a __global__ function("gpuholo::testKernel") is not allowed
我明白了。 []
是 __host__
受限函数,因为它的 cv::Vec2f
不是 cv::cuda::Vec2f
(其中显然不存在)。但我仍然很想访问这些数据。
设备端是否有其他类似Vec2f
的机制来访问2 channel 数据?
我想到了一种解决方法,将input
分成两个CV_32FC1
Mat
,这样内核看起来像这样:
__global__ void testKernel(const cv::cuda::PtrStepSzf re, const cv::cuda::PtrStepSzf im, cv::cuda::PtrStepf output)
但我想知道是否有一种“更干净”的解决方案,例如Vec2f
。
最佳答案
您可以使用原始数据类型来访问自定义 CUDA 内核中的 GpuMat
数据。例如CUDA运行时提供的float2
类型可以用作cv::Vec2f
的部分替换。下面是一个示例代码,演示了如何使用原始数据类型来访问 GpuMat 数据。
#include <iostream>
#include <cuda_runtime.h>
#include <opencv2/opencv.hpp>
using std::cout;
using std::endl;
__global__ void kernel_absolute(float2* src, float* dst, int rows, int cols, int iStep, int oStep)
{
int i = blockIdx.y * blockDim.y + threadIdx.y; //Row number
int j = blockIdx.x * blockDim.x + threadIdx.x; //Column number
if (i<rows && j<cols)
{
/* Compute linear index from 2D indices */
int tidIn = i * iStep + j;
int tidOut = i * oStep + j;
/* Read input value */
float2 input = src[tidIn];
/* Calculate absolute value */
float output = sqrtf(input.x * input.x + input.y * input.y);
/* Write output value */
dst[tidOut] = output;
}
}
int main(int argc, char** argv)
{
/* Example to compute absolute value of each element of a complex matrix */
int rows = 10;
int cols = 10;
int input_data_type = CV_32FC2; //input is complex
int output_data_type = CV_32FC1; //output is real
/* Create input matrix on host */
cv::Mat input = cv::Mat::zeros(rows,cols,input_data_type) + cv::Vec2f(1,1) /* Initial value is (1,1) */;
/* Display input */
cout<<input<<endl;
/* Create input matrix on device */
cv::cuda::GpuMat input_d;
/* Copy from host to device */
input_d.upload(input);
/* Create output matrix on device */
cv::cuda::GpuMat output_d(rows,cols, output_data_type);
/* Compute element step value of input and output */
int iStep = input_d.step / sizeof(float2);
int oStep = output_d.step / sizeof(float);
/* Choose appropriate block size */
dim3 block(8,8);
/* Compute grid size using input size and block size */
dim3 grid ( (cols + block.x -1)/block.x, (rows + block.y -1)/block.y );
/* Launch CUDA kernel to compute absolute value */
kernel_absolute<<<grid, block>>>( reinterpret_cast<float2*>(input_d.data), reinterpret_cast<float*>(output_d.data), rows, cols, iStep, oStep );
/* Check kernel launch errors */
assert( cudaSuccess == cudaDeviceSynchronize() );
cv::Mat output;
/* Copy results from device to host */
output_d.download(output);
/* Display output */
cout<<endl<<output<<endl;
return 0;
}
在带有 CUDA 8.0 的 Ubuntu 14.04 上使用以下命令进行编译和测试:
nvcc -o complex complex.cu -arch=sm_61 -L/usr/local/lib -lopencv_core
关于c++ - GpuMat - 访问自定义内核中的 2 channel float 据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46389154/