2017-09-24 65 views
2

據我所知cv::cuda::PtrStep用於將GpuMat數據直接傳遞給定製內核。我發現一個頻道訪問的例子here但是我的情況是2頻道墊(CV_32FC2)。在這種情況下,我試圖實現複數絕對平方值,其中複數值編碼如下:實部是第一平面,虛部是給定的第二平面MatGpuMat - 訪問定製內核中的2通道浮點數據

我想:

__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(顯然不存在)。但我仍然很想訪問這些數據。

是否有其他機制訪問設備端的2通道數據,類似於Vec2f


我以爲解決辦法的分裂input的形式導入兩款CV_32FC1Mat這麼內核看起來像:

__global__ void testKernel(const cv::cuda::PtrStepSzf re, const cv::cuda::PtrStepSzf im, cv::cuda::PtrStepf output) 

但我不知道是否有一個「乾淨」的解決方案,Vec2f像一個。

+0

你可以用'float2',而不是'CV :: Vec2f'。另外'input(x,y)'應該是'input(y,x)',因爲第一個參數是行,第二個參數是列。 – dari

回答

3

您可以使用原始數據類型在自定義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; 
} 

編譯和下面的命令在Ubuntu 14.04測試了CUDA 8.0:

nvcc -o complex complex.cu -arch=sm_61 -L/usr/local/lib -lopencv_core

+0

我喜歡它。沒有cv ::拆分飛機 - 沒有數據複製。 – michelson

+0

@michelson ...你可能會考慮接受答案,如果它解決了問題:)。 – sgarizvi

1

如果你想使用單個輸入到你的內核,你可以flatten your 2 channel image to a 1 channel image。現在

// test image 
Mat h_mat(Size(50,50),CV_32FC2,Scalar(0.0)); 

// Mat::reshape takes number of channels and rows, for your example 1,1 
Mat h_mat_flat = h_mat.reshape(1,1); 

// to upload to gpu 
GpuMat d_mat_flat(h_mat_flat.size(), h_mat_flat.type()); 
d_mat_flat.upload(h_mat_flat); 

可以通過d_mat_flat你的內核爲PtrStepSzf