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

6

我正在尝试解决一个问题,需要将彩色图像转换为灰度图像。为此,我使用了CUDA并行处理的方法。

我在GPU上调用的内核代码如下。

__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());
}


我看到第一个像素行中有一排点。

我遇到的错误是

libdc1394错误:无法初始化libdc1394
位置51处的差异超过了5的容差
参考值:255
GPU : 0
我的输入/输出图像 有人能帮我解决吗?提前感谢。


1
请给您的问题起一个更有意义的标题。目前这个标题对于除了你自己以外的任何人都毫无意义。如果有其他人有类似的图像处理问题,他们怎么可能通过搜索找到它呢? - talonmies
@talonmies:希望现在标题有意义了。 - Ashish Singh
2
这是Udacity的“并行编程入门”课程的一项作业。您应该自己解决它,而不是使用Stack Overflow让他人为您解决。 - RoBiK
6
@RoBiK: 我只是好奇,并且同时尝试着解决它,至于“让别人为你解决问题”的问题,我认为我的目的不是把答案提交给Udacity并计入成绩,而是更多地与编程社区中的其他人讨论并从他们的专业知识中学习,希望这对你有意义。 - Ashish Singh
12个回答

0

您正在运行以下块和网格数量:

  const dim3 blockSize(numCols/32, numCols/32 , 1);  //TODO
  const dim3 gridSize(numRows/12, numRows/12 , 1);  //TODO

然而你的内核代码中没有使用任何线程!

 int absolute_image_position_x = blockIdx.x;  
 int absolute_image_position_y = blockIdx.y;

想象一下,一张图片的宽度可以被分成absolute_image_position_x列,高度可以被分成absolute_image_position_y行。现在,每个交叉部分所创建的框中,你需要同时根据灰度图像更改/重绘所有像素。这已经足够为一个任务提供提示了 :)

谢谢你的回答,我已经想通了,我没有使用任何线程,这太愚蠢了。 - Ashish Singh

0

1- int x = (blockIdx.x * blockDim.x) + threadIdx.x;

2- int y = (blockIdx.y * blockDim.y) + threadIdx.y;

在网格和块大小中

1- const dim3 blockSize(32, 32, 1);

2- const dim3 gridSize((numCols/32+1), (numRows/32+1) , 1);

代码执行时间为0.036992毫秒。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接