2013-01-15 240 views
13

平均滤波器是线性类窗口滤波器,用于平滑信号(图像)。该滤波器作为低通滤波器工作。滤波器背后的基本思想是信号(图像)的任何元素在其邻域取平均值。Cuda图像平均滤波器


如果我们已经在m x n矩阵,我们希望与它大小k应用平均滤波器,则矩阵中的每个点p:(i,j)点的值是所有点的平方平均

Square Kernel

这个数字是与大小2过滤广场的内核,那黄色的盒子将被平均的像素,和所有的电网相邻像素的平方,即像素的新值将是它们的平均值。

问题是这个算法很慢,特别是在大图像上,所以我想到了使用GPGPU

现在的问题是,如果可能的话,如何在cuda中执行此操作?

+0

嗨@SamehKamal,我很好奇只是好奇。使用CUDA的代码与结果中的传统代码相比速度有多快? –

+2

这是一段很长的时间,我不记得这个算法的加速因子,但是我一直在使用的算法的性能从一个算法到另一个从x7到x22的加速比。 –

回答

16

这是embarrassingly parallel图像处理问题的一个经典案例,可以很容易地映射到CUDA框架。平均滤波器在图像处理领域中被认为是Box Filter

最简单的方法是将CUDA纹理用于过滤过程,因为边界条件可以通过纹理很容易地处理。

假设您在主机上分配了源和目标指针。程序会是这样的。

  1. 分配足够大的内存来保存设备上的源图像和目标图像。
  2. 将源图像从主机复制到设备。
  3. 将源图像设备指针绑定到纹理。
  4. 指定适当的块大小和足够大的网格以覆盖图像的每个像素。
  5. 使用指定的网格和块大小启动过滤内核。
  6. 将结果复制回主机。
  7. 解除绑定纹理
  8. 空闲设备指针。

示例实现箱过滤

内核

texture<unsigned char, cudaTextureType2D> tex8u; 

//Box Filter Kernel For Gray scale image with 8bit depth 
__global__ void box_filter_kernel_8u_c1(unsigned char* output,const int width, const int height, const size_t pitch, const int fWidth, const int fHeight) 
{ 
    int xIndex = blockIdx.x * blockDim.x + threadIdx.x; 
    int yIndex = blockIdx.y * blockDim.y + threadIdx.y; 

    const int filter_offset_x = fWidth/2; 
    const int filter_offset_y = fHeight/2; 

    float output_value = 0.0f; 

    //Make sure the current thread is inside the image bounds 
    if(xIndex<width && yIndex<height) 
    { 
     //Sum the window pixels 
     for(int i= -filter_offset_x; i<=filter_offset_x; i++) 
     { 
      for(int j=-filter_offset_y; j<=filter_offset_y; j++) 
      { 
       //No need to worry about Out-Of-Range access. tex2D automatically handles it. 
       output_value += tex2D(tex8u,xIndex + i,yIndex + j); 
      } 
     } 

     //Average the output value 
     output_value /= (fWidth * fHeight); 

     //Write the averaged value to the output. 
     //Transform 2D index to 1D index, because image is actually in linear memory 
     int index = yIndex * pitch + xIndex; 

     output[index] = static_cast<unsigned char>(output_value); 
    } 
} 

包装函数:

void box_filter_8u_c1(unsigned char* CPUinput, unsigned char* CPUoutput, const int width, const int height, const int widthStep, const int filterWidth, const int filterHeight) 
{ 

    /* 
    * 2D memory is allocated as strided linear memory on GPU. 
    * The terminologies "Pitch", "WidthStep", and "Stride" are exactly the same thing. 
    * It is the size of a row in bytes. 
    * It is not necessary that width = widthStep. 
    * Total bytes occupied by the image = widthStep x height. 
    */ 

    //Declare GPU pointer 
    unsigned char *GPU_input, *GPU_output; 

    //Allocate 2D memory on GPU. Also known as Pitch Linear Memory 
    size_t gpu_image_pitch = 0; 
    cudaMallocPitch<unsigned char>(&GPU_input,&gpu_image_pitch,width,height); 
    cudaMallocPitch<unsigned char>(&GPU_output,&gpu_image_pitch,width,height); 

    //Copy data from host to device. 
    cudaMemcpy2D(GPU_input,gpu_image_pitch,CPUinput,widthStep,width,height,cudaMemcpyHostToDevice); 

    //Bind the image to the texture. Now the kernel will read the input image through the texture cache. 
    //Use tex2D function to read the image 
    cudaBindTexture2D(NULL,tex8u,GPU_input,width,height,gpu_image_pitch); 

    /* 
    * Set the behavior of tex2D for out-of-range image reads. 
    * cudaAddressModeBorder = Read Zero 
    * cudaAddressModeClamp = Read the nearest border pixel 
    * We can skip this step. The default mode is Clamp. 
    */ 
    tex8u.addressMode[0] = tex8u.addressMode[1] = cudaAddressModeBorder; 

    /* 
    * Specify a block size. 256 threads per block are sufficient. 
    * It can be increased, but keep in mind the limitations of the GPU. 
    * Older GPUs allow maximum 512 threads per block. 
    * Current GPUs allow maximum 1024 threads per block 
    */ 

    dim3 block_size(16,16); 

    /* 
    * Specify the grid size for the GPU. 
    * Make it generalized, so that the size of grid changes according to the input image size 
    */ 

    dim3 grid_size; 
    grid_size.x = (width + block_size.x - 1)/block_size.x; /*< Greater than or equal to image width */ 
    grid_size.y = (height + block_size.y - 1)/block_size.y; /*< Greater than or equal to image height */ 

    //Launch the kernel 
    box_filter_kernel_8u_c1<<<grid_size,block_size>>>(GPU_output,width,height,gpu_image_pitch,filterWidth,filterHeight); 

    //Copy the results back to CPU 
    cudaMemcpy2D(CPUoutput,widthStep,GPU_output,gpu_image_pitch,width,height,cudaMemcpyDeviceToHost); 

    //Release the texture 
    cudaUnbindTexture(tex8u); 

    //Free GPU memory 
    cudaFree(GPU_input); 
    cudaFree(GPU_output); 
} 

好消息是,你不必执行过滤自己。 CUDA Toolkit附带由NVIDIA制造的名为NVIDIA Performance Primitives aka NPP的免费信号和图像处理库。 NPP使用支持CUDA的GPU来加速处理。平均过滤器已在NPP中实施。当前版本的NPP(5.0)支持8位,1通道和4通道图像。 的功能是:

  • nppiFilterBox_8u_C1R 1通道图像。
  • nppiFilterBox_8u_C4R 4通道图像。
+0

你的答案似乎非常好,但我并没有真正意识到你在那里描述的是什么,因为我主要在matlab上编程,并且我对C编程有很好的了解,我需要的是代码帮助,我认为内核函数原型是: '__global__ void ApplyAverageFilter(int ** Image,int ** Result,int filterSize);',我需要代码的帮助。 –

+1

哦。我已经更新了我的答案,并为CUDA内核添加了一个链接来进行框式过滤。但是你必须先学习CUDA才能使用它。否则,如果您没有太多的CUDA背景,NPP是更好的选择。 – sgarizvi

+0

我认为你的答案对于现在的问题已经足够了,非常感谢:) –

4

几个基本想法/步骤:

  1. 复制来自CPU的图像数据传送到GPU
  2. 调用内核来构建平均每行(水平)并将其存储在共享存储器中。
  3. 调用内核来构建每列(垂直)的平均值并将其存储在全局内存中。
  4. 将数据复制回CPU内存。

你应该能够与2D内存和多维内核调用扩展这个漂亮容易。

3

如果过滤器的大小是正常的并且不是很大,那么平均过滤器是使用CUDA实施的一个非常好的案例。您可以使用方块设置它,并且块的每个线程都负责计算一个像素的值,方法是对其邻域进行求和和平均。

如果将图像存储在全局内存中,那么它可以很容易地编程,但是会产生很多银行冲突。一种可能的优化是将图像的块加载到块的共享内存中。使用幻像元素(以便在查找相邻像素时不会超出共享块的尺寸),可以计算块内像素的平均值。

唯一需要注意的是如何在最后完成“拼接”,因为共享内存块会重叠(由于多余的“填充”像素),而且您不希望两次计算它们的值。