我正在尝试解决一个我应该将彩色图像更改为灰度图像的问题。为此,我正在使用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()); 
} 

我在第一条像素行中看到了一行点。

我得到的错误是
libdc1394错误:无法初始化libdc1394
pos 51处的差异超过5的公差
引用号:255
GPU:0
my input/output images
谁能帮我这个???提前致谢。

请您参考如下方法:

我最近参加了本类(class)并尝试了您的解决方案,但是它不起作用,所以我尝试了自己的解决方案。你几乎是正确的。正确的解决方案是这样的:

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

其余与您的代码相同。


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!