使用CUDA并行处理将彩色图像转换为灰度图像

Ash*_*ngh 5 parallel-processing cuda gpu image-processing

我试图解决一个问题,我应该将彩色图像更改为灰度图像.为此我使用CUDA并行方法.

我在GPU上调用的kerne代码如下.

__global__
void rgba_to_greyscale(const uchar4* const rgbaImage,
                   unsigned char* const greyImage,
                   int numRows, int numCols)
{
    int absolute_image_position_x = blockIdx.x;  
    int absolute_image_position_y = blockIdx.y;

  if ( absolute_image_position_x >= numCols ||
   absolute_image_position_y >= numRows )
 {
     return;
 }
uchar4 rgba = rgbaImage[absolute_image_position_x + absolute_image_position_y];
float channelSum = .299f * rgba.x + .587f * rgba.y + .114f * rgba.z;
greyImage[absolute_image_position_x + absolute_image_position_y] = channelSum;

}

void your_rgba_to_greyscale(const uchar4 * const h_rgbaImage,
                            uchar4 * const d_rgbaImage,
                            unsigned char* const d_greyImage,
                            size_t numRows,
                            size_t numCols)
{
  //You must fill in the correct sizes for the blockSize and gridSize
  //currently only one block with one thread is being launched
  const dim3 blockSize(numCols/32, numCols/32 , 1);  //TODO
  const dim3 gridSize(numRows/12, numRows/12 , 1);  //TODO
  rgba_to_greyscale<<<gridSize, blockSize>>>(d_rgbaImage,
                                             d_greyImage,
                                             numRows,
                                             numCols);

  cudaDeviceSynchronize(); checkCudaErrors(cudaGetLastError());
}
Run Code Online (Sandbox Code Playgroud)


我在第一个像素行中看到一行点.

我得到的

错误是libdc1394错误:无法初始化libdc1394
pos 51的差异超过5的容差
参考:255
GPU:0
我的输入/输出图像 任何人都可以帮我这个??? 提前致谢.

Ash*_*ngh 5

现在,因为我发布了这个问题,我一直在努力解决这个问题
,为了使这个问题正确,我应该做一些改进,我现在意识到我的初始解决方案是错误的.
要做的改变: -

 1. absolute_position_x =(blockIdx.x * blockDim.x) + threadIdx.x;
 2. absolute_position_y = (blockIdx.y * blockDim.y) + threadIdx.y;
Run Code Online (Sandbox Code Playgroud)

其次,

 1. const dim3 blockSize(24, 24, 1);
 2. const dim3 gridSize((numCols/16), (numRows/16) , 1);
Run Code Online (Sandbox Code Playgroud)

在解决方案中,我们使用numCols/16*numCols/16的网格
和24*24的块大小

代码以0.040576毫秒执行

@datenwolf:谢谢你的回答!!!

  • 任何想法为什么blockSize需要是'24,24`和gridSize`numCols/16,numRows/16`?有原因吗?其他数字可以工作吗? (2认同)

小智 5

我最近加入了这个课程,并尝试了你的解决方案,但它不起作用,我尝试了自己的.你几乎是对的.正确的解决方案是:

__global__`
void rgba_to_greyscale(const uchar4* const rgbaImage,
               unsigned char* const greyImage,
               int numRows, int numCols)
{`

int pos_x = (blockIdx.x * blockDim.x) + threadIdx.x;
int pos_y = (blockIdx.y * blockDim.y) + threadIdx.y;
if(pos_x >= numCols || pos_y >= numRows)
    return;

uchar4 rgba = rgbaImage[pos_x + pos_y * numCols];
greyImage[pos_x + pos_y * numCols] = (.299f * rgba.x + .587f * rgba.y + .114f * rgba.z); 

}
Run Code Online (Sandbox Code Playgroud)

其余的与您的代码相同.