Image Processing1 (Smoothing Images)

Goal

    blur(): mean filter
    GaussianBlur(): Gaussian filter
    medianBlur(): median filter

    bilateralFilter(): bilateral filtering

Theory

The theory comes from: "Computer Vision: Algorithms and Applications"

Mean filtering, Gaussian filtering and median filtering are relatively simple and will not be introduced here.

Bilateral filtering is used less, and is introduced in detail.

The weights introduced bilaterally have two parts, where the weights of the first part are the same as those used for Gaussian filtering, and the weights of the second part are used to evaluate the difference in intensities between pixels. The idea of ​​Gaussian filtering: in most cases, the closer the distance to the center pixel, the greater its weight; in general, this is indeed the case, due to the slow transformation of image pixels, but at the frontal edge of the image, due to Pixel information is rich, which is not the case; continuing to use spatial Gaussian filtering may result in blurred image edges. Therefore, the weight should be evaluated according to the difference between pixels. If the color and texture of the pixels are similar, the weight will be larger. Thus bilateral filtering adds another weight. The specific formula is as follows (Mao Xingyun "Introduction to OpenCV3 Programming"):

It's not that I don't put formulas, and once they are put, they will be reviewed. The formula above raises pages 177-178 on the book.

Code

/**
 * file Smoothing.cpp
 * brief Sample code for simple filters
 * author OpenCV team
 */


#include <iostream>
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"

using namespace std;
using namespace cv;

// Global Variables
int DELAY_CAPTION = 1500;
int DELAY_BLUR = 100;
int MAX_KERNEL_LENGTH = 31;

Mat src; Mat dst;
char window_name[] = "Smoothing Demo";

// Function headers
int display_caption( const char* caption );
int display_dst( int delay );


/**
 * function main
 */

int main( int argc, char ** argv )
{
    namedWindow( window_name, WINDOW_AUTOSIZE );

     // Load the source image
    const char* filename = argc >=2 ? argv[1] : "./lena.jpg";

    src = imread( filename, IMREAD_COLOR );
    if(src.empty()){
        printf(" Error opening image\n");
        printf(" Usage: ./Smoothing [image_name -- default ./lena.jpg] \n");
        return -1;
    }

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

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


    // Applying Homogeneous blur
    if( display_caption( "Homogeneous Blur" ) != 0 ) { return 0; }

     //![blur]
    for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
    { blur( src, dst, Size( i, i ), Point(-1,-1) ); // original image, target image, kernel size, anchor point: smooth point
        if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
    //![blur]

    // Applying Gaussian blur
    if( display_caption( "Gaussian Blur" ) != 0 ) { return 0; }

    //![gaussianblur]
    for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
    { GaussianBlur( src, dst, Size( i, i ), 0, 0 ); //Original image, target image, kernel size, standard deviation, standard deviation
        if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
    //![gaussianblur]

     // Applying Median blur
    if( display_caption( "Median Blur" ) != 0 ) { return 0; }

    //![medianblur]
    for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
    { medianBlur ( src, dst, i );//Original image, target image, equivalent to size(i,i)
        if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
    //![medianblur]

    // Applying Bilateral Filter
    if( display_caption( "Bilateral Blur" ) != 0 ) { return 0; }

     //![bilateralfilter ]
    for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
    { bilateralFilter ( src, dst, i, i*2, i/2 ); //Original image, target image, field size, color standard Bad, standard deviation of space
        if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
    //![bilateralfilter]

    // Done
    display_caption( "Done!" );

    return 0;
}

/**
 * @function display_caption
 */

int display_caption( const char* caption )
{
    dst = Mat::zeros( src.size(), src.type() );
    // image, text, point, font type, scale factor, color
    putText( dst, caption,
             Point( src.cols/4, src.rows/2),
             FONT_HERSHEY_COMPLEX, 1, Scalar(255, 255, 255) );

    return display_dst(DELAY_CAPTION);
}

/**
 * @function display_dst
 * /

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

}

Added more detailed notes, which are not explained here.

The CMakeLists.txt file is the same as the previous one without modification.

The test result is a gradual process. Post a screenshot of the intermediate process.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325529421&siteId=291194637