OpenCV  3.1.0
Open Source Computer Vision
Smoothing Images

Goal

In this tutorial you will learn how to apply diverse linear filters to smooth images using OpenCV functions such as:

Theory

Note
The explanation below belongs to the book Computer Vision: Algorithms and Applications by Richard Szeliski and to LearningOpenCV .. container:: enumeratevisibleitemswithsquare

Normalized Box Filter

Gaussian Filter

The median filter run through each element of the signal (in this case the image) and replace each pixel with the median of its neighboring pixels (located in a square neighborhood around the evaluated pixel).

Bilateral Filter

Code

Explanation

  1. Let's check the OpenCV functions that involve only the smoothing procedure, since the rest is already known by now.
  2. Normalized Block Filter:

    OpenCV offers the function cv::blur to perform smoothing with this filter.

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

    We specify 4 arguments (more details, check the Reference):

    • src: Source image
    • dst: Destination image
    • Size( w,h ): Defines the size of the kernel to be used ( of width w pixels and height h pixels)
    • Point(-1, -1): Indicates where the anchor point (the pixel evaluated) is located with respect to the neighborhood. If there is a negative value, then the center of the kernel is considered the anchor point.
  3. Gaussian Filter:

    It is performed by the function cv::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; } }

    Here we use 4 arguments (more details, check the OpenCV reference):

    • src: Source image
    • dst: Destination image
    • Size(w, h): The size of the kernel to be used (the neighbors to be considered). \(w\) and \(h\) have to be odd and positive numbers otherwise thi size will be calculated using the \(\sigma_{x}\) and \(\sigma_{y}\) arguments.
    • \(\sigma_{x}\): The standard deviation in x. Writing \(0\) implies that \(\sigma_{x}\) is calculated using kernel size.
    • \(\sigma_{y}\): The standard deviation in y. Writing \(0\) implies that \(\sigma_{y}\) is calculated using kernel size.
  4. Median Filter:

    This filter is provided by the cv::medianBlur function:

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

    We use three arguments:

    • src: Source image
    • dst: Destination image, must be the same type as src
    • i: Size of the kernel (only one because we use a square window). Must be odd.
  5. Bilateral Filter

    Provided by OpenCV function cv::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; } }

    We use 5 arguments:

    • src: Source image
    • dst: Destination image
    • d: The diameter of each pixel neighborhood.
    • \(\sigma_{Color}\): Standard deviation in the color space.
    • \(\sigma_{Space}\): Standard deviation in the coordinate space (in pixel terms)

Results