opencv图像平滑处理

blur 

Blurs an image using the normalized box filter.

C++:  void  blur (InputArray  src, OutputArray  dst, Size  ksize, Point  anchor=Point(-1,-1), int  borderType=BORDER_DEFAULT  )
Python:   cv2. blur (src, ksize [, dst [, anchor [, borderType ] ] ] ) → dst
Parameters:
  • src – input image; it can have any number of channels, which are processed independently, but the depth should be CV_8UCV_16UCV_16SCV_32F or CV_64F.
  • dst – output image of the same size and type as src.
  • ksize – blurring kernel size.
  • anchor – anchor point; default value Point(-1,-1) means that the anchor is at the kernel center.
  • borderType – border mode used to extrapolate pixels outside of the image.
  • 边界模式用于推断图片以外的像素
  • The function smoothes an image using the kernel:

\texttt{K} =  \frac{1}{\texttt{ksize.width*ksize.height}} \begin{bmatrix} 1 & 1 & 1 &  \cdots & 1 & 1  \\ 1 & 1 & 1 &  \cdots & 1 & 1  \\ \hdotsfor{6} \\ 1 & 1 & 1 &  \cdots & 1 & 1  \\ \end{bmatrix}

The call blur(src, dst, ksize, anchor, borderType) is equivalent to boxFilter(src, dst, src.type(), anchor, true, borderType) .

归一化块滤波器 (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}

归一化块滤波器:

OpenCV函数 blur 执行了归一化块平滑操作。

for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
    { blur( src, dst, Size( i, i ), Point(-1,-1) );
      if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }

我们输入4个实参 (详细的解释请参考 Reference):

  • src: 输入图像
  • dst: 输出图像
  • Size( w,h ): 定义内核大小( w 像素宽度, h 像素高度)
  • Point(-1, -1): 指定锚点位置(被平滑点), 如果是负值,取核的中心为锚点。

GaussianBlur

Blurs an image using a Gaussian filter.

C++:  void  GaussianBlur (InputArray  src, OutputArray  dst, Size  ksize, double  sigmaX, double  sigmaY=0, int  borderType=BORDER_DEFAULT  )
Python:   cv2. GaussianBlur (src, ksize, sigmaX [, dst [, sigmaY [, borderType ] ] ] ) → dst
Parameters:
  • src – input image; the image can have any number of channels, which are processed independently, but the depth should be CV_8UCV_16UCV_16SCV_32F or CV_64F.
  • dst – output image of the same size and type as src.
  • ksize – Gaussian kernel size. ksize.width and ksize.height can differ but they both must be positive and odd. Or, they can be zero’s and then they are computed from sigma* .
  • sigmaX – Gaussian kernel standard deviation in X direction.
  • sigmaY – Gaussian kernel standard deviation in Y direction; if sigmaY is zero, it is set to be equal to sigmaX, if both sigmas are zeros, they are computed from ksize.width andksize.height , respectively (see getGaussianKernel() for details); to fully control the result regardless of possible future modifications of all this semantics, it is recommended to specify all of ksizesigmaX, and sigmaY.
  • borderType – pixel extrapolation method (see borderInterpolate() for details).

The function convolves the source image with the specified Gaussian kernel. In-place filtering is supported.

高斯滤波器 (Gaussian Filter)

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

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

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

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

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 各有一个均值,也各有一个标准差)

  1. 高斯滤波器:

    OpenCV函数 GaussianBlur 执行高斯平滑 :

    for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
        { GaussianBlur( src, dst, Size( i, i ), 0, 0 );
          if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
    

我们输入4个实参 (详细的解释请参考 Reference):

  • src: 输入图像
  • dst: 输出图像
  • Size(w, h): 定义内核的大小(需要考虑的邻域范围)。 w 和 h 必须是正奇数,否则将使用 \sigma_{x} 和 \sigma_{y} 参数来计算内核大小。
  • \sigma_{x}: x 方向标准方差, 如果是 0 则 \sigma_{x} 使用内核大小计算得到。
  • \sigma_{y}: y 方向标准方差, 如果是 0 则 \sigma_{y} 使用内核大小计算得到。.

medianBlur

Blurs an image using the median filter.

C++:  void  medianBlur (InputArray  src, OutputArray  dst, int  ksize )
Python:   cv2. medianBlur (src, ksize [, dst ] ) → dst
Parameters:
  • src – input 1-, 3-, or 4-channel image; when ksize is 3 or 5, the image depth should be CV_8UCV_16U, or CV_32F, for larger aperture sizes, it can only be CV_8U.
  • dst – destination array of the same size and type as src.
  • ksize – aperture linear size; it must be odd and greater than 1, for example: 3, 5, 7 ...

The function smoothes an image using the median filter with the \texttt{ksize} \times \texttt{ksize} aperture. Each channel of a multi-channel image is processed independently. In-place operation is supported.

中值滤波器 (Median Filter)

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

中值滤波器:

OpenCV函数 medianBlur 执行中值滤波操作:

for ( int i = 3; i < MAX_KERNEL_LENGTH; i = i + 2 )
    { medianBlur ( src, dst, i );
      if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }

我们用了3个参数:

  • src: 输入图像
  • dst: 输出图像, 必须与 src 相同类型
  • i: 内核大小 (只需一个值,因为我们使用正方形窗口),必须为奇数。

bilateralFilter

Applies the bilateral filter to an image.

C++:  void  bilateralFilter (InputArray  src, OutputArray  dst, int  d, double  sigmaColor, double  sigmaSpace, int  borderType=BORDER_DEFAULT  )
Python:   cv2. bilateralFilter (src, d, sigmaColor, sigmaSpace [, dst [, borderType ] ] ) → dst
Parameters:
  • src – Source 8-bit or floating-point, 1-channel or 3-channel image.
  • dst – Destination image of the same size and type as src .
  • d – Diameter of each pixel neighborhood that is used during filtering. If it is non-positive, it is computed from sigmaSpace .
  • sigmaColor – Filter sigma in the color space. A 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. A larger value of the parameter means that farther pixels will influence each other as long as their colors are close enough (see sigmaColor ). When d>0 , it specifies the neighborhood size regardless of sigmaSpace . Otherwise, d is proportional to sigmaSpace .

The function applies bilateral filtering to the input image, as described in http://www.dai.ed.ac.uk/CVonline/LOCAL_COPIES/MANDUCHI1/Bilateral_Filtering.html bilateralFilter can reduce unwanted noise very well while keeping edges fairly sharp. However, it is very slow compared to most filters.

Sigma values: For simplicity, you can set the 2 sigma values to be the same. If they are small (< 10), the filter will not have much effect, whereas if they are large (> 150), they will have a very strong effect, making the image look “cartoonish”.

Filter size: Large filters (d > 5) are very slow, so it is recommended to use d=5 for real-time applications, and perhaps d=9 for offline applications that need heavy noise filtering.

This filter does not work inplace.

双边滤波 (Bilateral Filter)

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

双边滤波器

OpenCV函数 bilateralFilter 执行双边滤波操作:

for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
    { bilateralFilter ( src, dst, i, i*2, i/2 );
      if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }

我们使用了5个参数:

  • src: 输入图像
  • dst: 输出图像
  • d: 像素的邻域直径
  • \sigma_{Color}: 颜色空间的标准方差
  • \sigma_{Space}: 坐标空间的标准方差(像素单位)

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace std;
using namespace cv;

/// 全局变量
int DELAY_CAPTION = 1500;
int DELAY_BLUR = 100;
int MAX_KERNEL_LENGTH = 31;

Mat src; Mat dst;
char window_name[] = "Filter Demo 1";

/// 函数申明
int display_caption( char* caption );
int display_dst( int delay );

/**
 *  main 函数
 */
 int main( int argc, char** argv )
 {
   namedWindow( window_name, CV_WINDOW_AUTOSIZE );

   /// 载入原图像
   src = imread( "../images/lena.jpg", 1 );

   if( display_caption( "Original Image" ) != 0 ) { return 0; }

   dst = src.clone();
   if( display_dst( DELAY_CAPTION ) != 0 ) { return 0; }

   /// 使用 均值平滑
   if( display_caption( "Homogeneous Blur" ) != 0 ) { return 0; }

   for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
       { blur( src, dst, Size( i, i ), Point(-1,-1) );
         if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }

    /// 使用高斯平滑
    if( display_caption( "Gaussian Blur" ) != 0 ) { return 0; }

    for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
        { GaussianBlur( src, dst, Size( i, i ), 0, 0 );
          if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }

     /// 使用中值平滑
     if( display_caption( "Median Blur" ) != 0 ) { return 0; }

     for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
         { medianBlur ( src, dst, i );
           if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }

     /// 使用双边平滑
     if( display_caption( "Bilateral Blur" ) != 0 ) { return 0; }

     for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
         { bilateralFilter ( src, dst, i, i*2, i/2 );
           if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }

     /// 等待用户输入
     display_caption( "End: Press a key!" );

     waitKey(0);
     return 0;
 }

 int display_caption( char* caption )
 {
   dst = Mat::zeros( src.size(), src.type() );
   putText( dst, caption,
            Point( src.cols/4, src.rows/2),
            CV_FONT_HERSHEY_COMPLEX, 1, Scalar(255, 255, 255) );

   imshow( window_name, dst );
   int c = waitKey( DELAY_CAPTION );
   if( c >= 0 ) { return -1; }
   return 0;
  }

  int display_dst( int delay )
  {
    imshow( window_name, dst );
    int c = waitKey ( delay );
    if( c >= 0 ) { return -1; }
    return 0;
  }

转自:http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/imgproc/gausian_median_blur_bilateral_filter/gausian_median_blur_bilateral_filter.html#smoothing

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值