高斯平滑 高斯模糊 高斯滤波器 ( Gaussian Smoothing, Gaussian Blur, Gaussian Filter )

转自:http://blog.csdn.net/hhygcy/archive/2009/07/07/4329056.aspx

发展到现在这个平滑算法的时候, 我已经完全不知道如何去命名这篇文章了, 只好罗列出一些关键字来方便搜索了.

在之前我们提到过了均值滤波器, 就是说某像素的颜色, 由以其为中心的九宫格的像素平均值来决定. 在这个基础上又发展成了带权的平均滤波器, 这里的高斯平滑或者说滤波器就是这样一种带权的平均滤波器. 那么这些权重如何分布呢? 我们先来看几个经典的模板例子:

尝试了使用这些滤波器对我们原来的图进行操作, 得到了这样的一组结果:

原图:

raw

3x3 高斯:

3x3

5x5 高斯:

5x5

单纯从效果来看, 两个模板都起到了平滑的作用, 只是程度有深浅的区分. 那么从理论上来说为什么能起到平滑的作用呢? 很显然, 像素的颜色不仅由自身决定了, 同时有其周围的像素加权决定, 客观上减小了和周围像素的差异. 同时这些权重的设定满足了越近权重越大的规律. 从理论来讲, 这些权重的分布满足了著名的所谓高斯分布:

   这就是1维的计算公式

 这就是2维的计算公式

x, y表示的就是当前点到对应点的距离, 而那些具体的模板就是由这里公式中的一些特例计算而来. 需要说明的是不只有这么一些特例, 从wikipedia可以方便地找到那些复杂的模板比如像:

Sample Gaussian matrix

This is a sample matrix, produced by sampling the Gaussian filter kernel (with σ = 0.84089642) at the midpoints of each pixel and then normalising. Note that the center element (at [4, 4]) has the largest value, decreasing symmetrically as distance from the center increases.

0.000000670.000022920.000191170.000387710.000191170.000022920.00000067
0.000022920.000786330.006559650.013303730.006559650.000786330.00002292
0.000191170.006559650.054721570.110981640.054721570.006559650.00019117
0.000387710.013303730.110981640.225083520.110981640.013303730.00038771
0.000191170.006559650.054721570.110981640.054721570.006559650.00019117
0.000022920.000786330.006559650.013303730.006559650.000786330.00002292
0.000000670.000022920.000191170.000387710.000191170.000022920.00000067

是不是看到就头大了:) 不过没关系, 对于一般的应用来说, 前面的例子已经可以完成任务了.  代码的话我们还是给一份5x5的example:

 

  1. /** 
  2. ** method to remove noise from the corrupted image by gaussian filter value 
  3. * @param corrupted input grayscale binary array with corrupted info 
  4. * @param smooth output data for smooth result, the memory need to be allocated outside of the function 
  5. * @param width width of the input grayscale image 
  6. * @param height height of the input grayscale image 
  7. */  
  8. void gaussianFilter2 (unsigned char* corrupted, unsigned char* smooth, int width, int height)  
  9. {  
  10.     int templates[25] = { 1, 4, 7, 4, 1,   
  11.                           4, 16, 26, 16, 4,   
  12.                           7, 26, 41, 26, 7,  
  13.                           4, 16, 26, 16, 4,   
  14.                           1, 4, 7, 4, 1 };        
  15.       
  16.     memcpy ( smooth, corrupted, width*height*sizeof(unsigned char) );  
  17.     for (int j=2;j<height-2;j++)  
  18.     {  
  19.         for (int i=2;i<width-2;i++)  
  20.         {  
  21.             int sum = 0;  
  22.             int index = 0;  
  23.             for ( int m=j-2; m<j+3; m++)  
  24.             {  
  25.                 for (int n=i-2; n<i+3; n++)  
  26.                 {  
  27.                     sum += corrupted [ m*width + n] * templates[index++] ;  
  28.                 }  
  29.             }  
  30.             sum /= 273;  
  31.             if (sum > 255)  
  32.                 sum = 255;  
  33.             smooth [ j*width+i ] = sum;  
  34.         }  
  35.     }  
  36. }  

 

附带说一些,很明显,和均值滤波器类似, 这个滤波器没有消除校验噪声的作用.

是的,高斯平滑和中值平滑都可以使用卷积运算对矩阵进行处理。 在使用卷积运算进行图像平滑时,需要先定义一个卷积核。对于高斯平滑,卷积核通常是一个二维的高斯函数,可以使用`cv2.getGaussianKernel()`函数生成。对于中值平滑,卷积核通常是一个正方形的窗口,窗口大小可以根据需要进行调整。 以下是使用numpy库进行高斯平滑和中值平滑的示例代码: ```python import numpy as np # 定义输入矩阵 matrix = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) # 定义高斯卷积核 gaussian_kernel = cv2.getGaussianKernel(3, 1) # 进行高斯平滑 gaussian = cv2.filter2D(matrix, -1, gaussian_kernel) # 定义中值卷积核 median_kernel = np.ones((3, 3)) # 进行中值平滑 median = cv2.medianBlur(matrix, 3) # 显示结果 print('Original matrix:\n', matrix) print('Gaussian smoothing:\n', gaussian) print('Median smoothing:\n', median) ``` 这段代码首先定义了一个输入矩阵,然后分别定义高斯卷积核和中值卷积核。对于高斯卷积核,使用`cv2.getGaussianKernel()`函数生成一个大小为3x3的高斯核。对于中值卷积核,使用`np.ones()`函数生成一个大小为3x3的全1矩阵。然后使用`cv2.filter2D()`函数对输入矩阵进行高斯平滑。最后使用`cv2.medianBlur()`函数对输入矩阵进行中值平滑。可以根据需要调整卷积核的大小和类型来得到不同的平滑结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值