opencv 笔记09Imgproc_Smoothing

来源:互联网 发布:mac应用程序没图标 编辑:程序博客网 时间:2024/06/05 15:08

 归一化块滤波器 (Normalized Box Filter)

  • 最简单的滤波器, 输出像素值是核窗口内像素值的 均值 ( 所有像素加权系数相等)

  • 核如下:

    K = \dfrac{1}{K_{width} \cdot K_{height}} \begin{bmatrix}    1 & 1 & 1 & ... & 1 \\    1 & 1 & 1 & ... & 1 \\    . & . & . & ... & 1 \\    . & . & . & ... & 1 \\    1 & 1 & 1 & ... & 1   \end{bmatrix}

    void blur(const Mat& srcMat& dst, Size ksize, Point anchor=Point(-1, -1), int borderType=BORDER_DEFAULT)

    Parameters:

    • src – The source image
    • dst – The destination image; will have the same size and the same type as src
    • ksize – The smoothing kernel size
    • anchor – The anchor point. The default value Point(-1,-1) means that the anchor is at the kernel center
    • borderType – The border mode used to extrapolate pixels outside of the image

 高斯滤波器 (Gaussian Filter)

  • 最有用的滤波器 (尽管不是最快的)。 高斯滤波是将输入数组的每一个像素点与 高斯内核 卷积将卷积和当作输出像素值。

  • 还记得1维高斯函数的样子吗?

    ../../../../_images/Smoothing_Tutorial_theory_gaussian_0.jpg

    假设图像是1维的,那么观察上图,不难发现中间像素的加权系数是最大的, 周边像素的加权系数随着它们远离中间像素的距离增大而逐渐减小。

    void GaussianBlur(const Mat& srcMat& dst, Size ksize, double sigmaX, double sigmaY=0, int borderType=BORDER_DEFAULT)

    Parameters:                                         

    • src – The source image
    • dst – The destination image; will have the same size and the same type as src
    • ksize – The Gaussian kernel size; ksize.width and ksize.height can differ, but they both must be positive and odd. Or, they can be zero’s, then they are computed from sigma*
    • sigmaX, sigmaY – The Gaussian kernel standard deviations in X and Y direction. If sigmaY is zero, it is set to be equal to sigmaX . If they are both zeros, they are computed from ksize.width and ksize.height , respectively, seegetGaussianKernel() . To fully control the result regardless of possible future modification of all this semantics, it is recommended to specify all of ksize , sigmaX and sigmaY
    • borderType – The pixel extrapolation method; see borderInterpolate()

Note

 

2维高斯函数可以表达为 :

G_{0}(x, y) = A  e^{ \dfrac{ -(x - \mu_{x})^{2} }{ 2\sigma^{2}_{x} } +  \dfrac{ -(y - \mu_{y})^{2} }{ 2\sigma^{2}_{y} } }

其中 \mu 为均值 (峰值对应位置), \sigma 代表标准差 (变量 x 和 变量 y 各有一个均值,也各有一个标准差)

 中值滤波器 (Median Filter)

中值滤波将图像的每个像素用邻域 (以当前像素为中心的正方形区域)像素的 中值 代替 。

void medianBlur(const Mat& srcMat& dst, int ksize)

Parameters:     

  • src – The source 1-, 3- or 4-channel image. When ksize is 3 or 5, the image depth should be CV_8U , CV_16U orCV_32F . For larger aperture sizes it can only be CV_8U
  • dst – The destination array; will have the same size and the same type as src
  • ksize – The aperture linear size. It must be odd and more than 1, i.e. 3, 5, 7 ...

 双边滤波 (Bilateral Filter)

  • 目前我们了解的滤波器都是为了 平滑 图像, 问题是有些时候这些滤波器不仅仅削弱了噪声, 连带着把边缘也给磨掉了。 为避免这样的情形 (至少在一定程度上 ), 我们可以使用双边滤波。
  • 类似于高斯滤波器,双边滤波器也给每一个邻域像素分配一个加权系数。 这些加权系数包含两个部分, 第一部分加权方式与高斯滤波一样,第二部分的权重则取决于该邻域像素与当前像素的灰度差值。
  • 详细的解释可以查看 链接


void bilateralFilter(const Mat& srcMat& dst, int d, double sigmaColor, double sigmaSpace, int borderType=BORDER_DEFAULT)
Parameters:                         
  • src – The source 8-bit or floating-point, 1-channel or 3-channel image
  • dst – The destination image; will have the same size and the same type as src
  • d –  像素的邻域直径,The diameter of each pixel neighborhood, that is used during filtering. If it is non-positive, it’s computed fromsigmaSpace
  • sigmaColor – 颜色空间的标准方差Filter sigma in the color space. Larger value of the parameter means that farther colors within the pixel neighborhood (see sigmaSpace ) will be mixed together, resulting in larger areas of semi-equal color
  • sigmaSpace – 坐标空间的标准方差Filter sigma in the coordinate space. Larger value of the parameter means that farther pixels will influence each other (as long as their colors are close enough; see sigmaColor ). Then d>0 , it specifies the neighborhood size regardless of sigmaSpace , otherwise d is proportional to sigmaSpace

原创粉丝点击