2017-04-18 47 views
-1

我需要做一個RGB2GRAY圖像處理算法。我只需要一些幫助來完成全局函數或我如何訪問* d_src指針。這是我的代碼,您的幫助將不勝感激。RGB2GRAY與CUDA和CImg庫

#include "cuda_runtime.h" 
#include "device_launch_parameters.h" 

#include "CImg.h" 
#include <iostream> 

using namespace std; 
using namespace cimg_library; 

__global__ void rgb2gray(unsigned char * d_src, unsigned char * d_dst, int width, int height){ 

    int pos_x = blockIdx.x * blockDim.x + threadIdx.x; 
    int pos_y = blockIdx.y * blockDim.y + threadIdx.y; 

    if (pos_x >= width || pos_y >= height) 
     return; 

} 


int main(){ 
    //Load image 
    CImg<unsigned char> src("lena.jpg"); 
    int width = src.width(); 
    int height = src.height(); 
    unsigned long sizee = src.size(); 

    int sze = width * height; 

    cout << sze << endl; 

    //create pointer to image 
    unsigned char *h_src = src.data(); 

    CImg<unsigned char> dst(width, height, 1, 1); 
    unsigned char *h_dst = dst.data(); 

    unsigned char *d_src; 
    unsigned char *d_dst; 

    cout << sizee << endl; 

    cudaMalloc((void**)&d_src, sizee); 
    cudaMalloc((void**)&d_dst, width*height*sizeof(int)); 

    cudaMemcpy(d_src, h_src, sizee, cudaMemcpyHostToDevice); 

    //launch the kernel 
    rgb2gray << <(width/16,height/16,1), (16, 16, 1) >> >(d_src, d_dst, width, height); 

    //force the printf()s to flush 
    cudaDeviceSynchronize(); 
    // copy back the result array to the CPU 
    cudaMemcpy(h_dst, d_dst, width*height, cudaMemcpyDeviceToHost); 

    cudaFree(d_src); 
    cudaFree(d_dst); 


    CImgDisplay main_disp(dst, "After Processing"); 
    while (!main_disp.is_closed()) 
     main_disp.wait(); 


    return 0; 
} 

回答

1

首先,由於你的dst對象由unsigned char,分配d_dst如下;

cudaMalloc((void**)&d_dst, width*height*sizeof(unsigned char)); 

接下來,電網必須覆蓋每一個像素,審理案件時widthheight是不是16的倍數。使用以下內核配置啓動內核。

dim3 blkDim (16, 16, 1); 
dim3 grdDim ((width + 15)/16, (height + 15)/16, 1); 
rgb2gray<<<grdDim, blkDim>>>(d_src, d_dst, width, height); 

最後,你的內核看起來應該是這樣的。請注意,RGB通道分爲d_src

int pos_x = blockIdx.x * blockDim.x + threadIdx.x; 
int pos_y = blockIdx.y * blockDim.y + threadIdx.y; 

if (pos_x >= width || pos_y >= height) 
    return; 

unsigned char r = d_src[pos_y * width + pos_x]; 
unsigned char g = d_src[(height + pos_y) * width + pos_x]; 
unsigned char b = d_src[(height * 2 + pos_y) * width + pos_x]; 

unsigned int _gray = (unsigned int)((float)(r + g + b)/3.0f + 0.5); 
unsigned char gray = _gray > 255 ? 255 : _gray; 

d_dst[pos_y * width + pos_x] = gray; 

您可以看到完整的代碼here

+0

非常感謝您的解釋,我不確定渠道是如何拆分的。 – JA7

+0

@ JA7有關「像素數據如何與CImg一起存儲」的更多信息,請參見[this](http://cimg.eu/reference/group__cimg__storage.html)。 – nglee