OpenCV Development Notes (XVI): The basic algorithm for linear filtering - Gaussian filter

If the text is the original article, shall not be reproduced without permission
of the original bloggers blog address: https://blog.csdn.net/qq21497936
original bloggers blog Navigation: https://blog.csdn.net/qq21497936/article/details / 102 478 062
This article blog address: https://blog.csdn.net/qq21497936/article/details/104490409

table of Contents

Foreword

Demo

Gaussian filter

Outline

Feature

Function prototype

Source Demo

Project templates: corresponds to the version number v1.11.0


 

OpenCV development Box

" OpenCV Development Notes (square): Use mingw530_32 compiled openCV3.4.1 source code, build Qt5.9.3 of openCV development environment ."

" OpenCV development notes (a): OpenCV presentation, compiled "

" OpenCV Development Notes (b): cvui interface "

" OpenCV Development Notes (c): OpenCV image concept and basic operation ."

" OpenCV Development Notes (d): OpenCV fetch and store pictures and video data ."

" The OpenCV Development Notes (V): OpenCV reading operation Camera "

" The OpenCV Development Notes (VI): OpenCV underlying data structure, color, and color space conversion function "

" OpenCV development notes (seven): OpenCV basic graphics drawing "

" OpenCV Development Notes (VIII): OpenCV timing of the common operations, scaling, rotation, mirroring "

" The OpenCV Development Notes (IX) : the OpenCV area image (the ROI ) and the whole, partial images mixed "

" The OpenCV Development Notes ( ten): OpenCV image color channel separation image color and multi-channel mixing "

" OpenCV Development Notes (XI): OpenCV compile support Gpu (CUDA) win-qt-mingw32 accelerate the development of the compiler ."

" OpenCV Development Notes (xii): OpenCV compile support Gpu (cuda) to accelerate the development of win-qt-msvc2015 compiler (opencv3.4.0 , cuda9.0 , VS2015) "

" The OpenCV Development Notes (XIII): OpenCV image contrast, brightness adjustment "

" The OpenCV Development Notes (XIV): The basic algorithm for linear filtering - block filter "

" OpenCV Development Notes (xv): Foundations of linear filtering algorithm - mean filter "

" The OpenCV Development Notes (XVI): The basic algorithm for linear filtering - Gaussian filter "

" The OpenCV Development Notes (17): The basis for comparison with a linear filtering algorithm - block, mean, Gaussian "

" The OpenCV Development Notes (XVIII): Foundations of Nonlinear Filtering Algorithm - median filtering "

" OpenCV Development Notes (XIX): the basis of nonlinear filtering algorithm - bilateral filtering "

"OpenCV Development Notes (XX): nonlinear filtering basis for comparison of the algorithm - the value of bilateral filtering": to be released

"OpenCV Development Notes (xxi): Foundations of morphological filtering algorithm - inflation": to be released

"OpenCV Development Notes (XXII): basis of morphological filtering algorithm - corrosion": to be released

"OpenCV Development Notes (XXIII): basis of morphological filtering algorithm - open operation (after the first expansion of corrosion)": To be published

"OpenCV Development Notes (24): the basis of morphological filtering algorithm - closing operation (after the first expansion of corrosion)": To be published

"OpenCV Development Notes (XXV): basis of morphological filtering algorithm - white hat": to be released

"OpenCV Development Notes (26): the basis of morphological filtering algorithm - Black Hat": to be released

Continued to supplement ...

 

    OpenCV Development Notes (XVI): The basic algorithm for linear filtering - Gaussian filter

 

Foreword

This chapter explains the linear filtering with the Gaussian filter.

 

Demo

 

Gaussian filter

Outline

      Gaussian filtering is input to each pixel array is convolved with a Gaussian kernel acid, and a convolution output as a pixel value. After the image is smoothed Gaussian filtering process depends on the standard deviation.

      Gaussian filter output is a weighted average of the art items, while the closer the pixel from the center the higher the weight, and therefore, with respect to the mean filter, it is more gentle smoothing and edge preservation was also better.

In image processing, a Gaussian filter is generally implemented in two ways:

  • Sliding window discrete convolution window: the discrete window when very large amount of calculation with a sliding window is very large (i.e., using separable filters implemented), the implementation may be considered based on Fourier transform .
  • Fourier transform;

Feature

      Gaussian filtering is the most useful filter filter characteristics are as follows:

  • Less distortion : single-valued function is a Gaussian function, Gaussian field pixel weighted mean value of the store instead of the pixel, the pixel will change with the weight monotonically distance, to reduce distortion.
  • Rotational symmetry: Gaussian function having a resistance against rotation, in the respective directions Gaussian smoothing Chengdu is the same, it is difficult to estimate the noise present in the directivity, ensure smooth performance is not biased in any direction.
  • Not affect the frequency: the Fourier transform of the Gaussian function is a single lobe of the spectrum, a Gaussian filter is the smoothed image will not be affected by the unwanted high frequency signals, while retaining most of the desired signal.
  • Controllable noise parameters: Gaussian smoothing filter by the degree of the variance [sigma] determined, [sigma] , the more wide band, the better the degree of smoothing, the image noise may be provided with a controllable parameter.
  • Separability: a two-dimensional Gaussian convolution can be performed in two steps, the first image and the one-dimensional Gaussian function convolution, the convolution result and a direction perpendicular to the same one-dimensional Gaussian function convolution.

Function prototype

void GaussianBlur( InputArray src, 
                OutputArray dst,
                Size ksize, 
                double sigmaX,
                double sigmaY = 0,
                int borderType = BORDER_DEFAULT );
  • A parameter : InputArray type generally cv :: Mat, and can handle any number of image channels. Note, however, the depth of the picture should be treated as CV_8U , CV_16U , CV_16S , CV_32F , CV_64F one.
  • Parameter Two ; OutputArray type, output target image, you need the original picture and have the same size and type.
  • Three parameters : Size type ksize, the size of the sub-accounts. General use Size (w, h) to indicate the size of the sub-accounting, Size (3,3) on a 3x3 size represented by the sub-accounting, w and h may be different sizes.
  • Four parameters : double type sigmaX, represents the standard deviation of the Gaussian kernel function of the X-direction.
  • Five parameters : double type sigmaY, represents the standard deviation of the Gaussian kernel function of the Y-direction.

Above, when sigmaX and sigmaY are 0, the default calculated using sub-accounts:

  • Six parameters : int type borderType, inferring mode for a certain image of the external boundary pixels, the default values BORDER_DEFULAT, generally without use.

 

Source Demo

void OpenCVManager::testGaussianBlurFilter()
{
    QString fileName1 = "E:/qtProject/openCVDemo/openCVDemo/modules/openCVManager/images/1.jpg";
    cv::Mat srcMat = cv::imread(fileName1.toStdString());

    cv::String windowName = _windowTitle.toStdString();
    cvui::init(windowName);

    if(!srcMat.data)
    {
        qDebug() << __FILE__ << __LINE__
                 << "Failed to load image:" << fileName1;
        return;
    }

    cv::Mat dstMat;
    dstMat = cv::Mat::zeros(srcMat.size(), srcMat.type());
    cv::Mat windowMat = cv::Mat(cv::Size(dstMat.cols * 3, dstMat.rows),
                                srcMat.type());
    int ksize = 1;      // 核心大小
    int sigmaX = 0;  // x方向的标准偏差
    int sigmaY = 0;  // y方向的标准偏差
    cvui::window(windowMat, dstMat.cols, 0, dstMat.cols, dstMat.rows, "settings");
    while(true)
    {
        windowMat = cv::Scalar(0, 0, 0);
        // 原图先copy到左边
        cv::Mat leftMat = windowMat(cv::Range(0, srcMat.rows),
                                    cv::Range(0, srcMat.cols));
        cv::addWeighted(leftMat, 1.0f, srcMat, 1.0f, 0.0f, leftMat);
        // 中间为调整滤波参数的相关设置
        cvui::printf(windowMat, 375, 20, "ksize = size *  2 + 1");
        cvui::trackbar(windowMat, 375, 30, 165, &ksize, 0, 10);

        cvui::printf(windowMat, 375, 80, "sigmaX");
        cvui::trackbar(windowMat, 375, 90, 165, &sigmaX, 0, 100);

        cvui::printf(windowMat, 375, 140, "sigmaY");
        cvui::trackbar(windowMat, 375, 150, 165, &sigmaY, 0, 100);

        // 高斯滤波
        cv::GaussianBlur(srcMat, dstMat, cv::Size(ksize * 2 + 1, ksize * 2 + 1), sigmaX / 10.f, sigmaY / 10.f);

        // 效果图copy到右边
        // 注意:rang从位置1到位置2,不是位置1+宽度
        cv::Mat rightMat = windowMat(cv::Range(0, srcMat.rows),
                                     cv::Range(srcMat.cols * 2, srcMat.cols * 3));
        cv::addWeighted(rightMat, 0.0f, dstMat, 1.0f, 0.0f, rightMat);
        // 更新
        cvui::update();
        // 显示
        cv::imshow(windowName, windowMat);
        // esc键退出
        if(cv::waitKey(25) == 27)
        {
            break;
        }
    }
}

 

Project templates: corresponds to the version number v1.11.0

      The corresponding version number v1.11.0


The original bloggers blog address: https://blog.csdn.net/qq21497936
original bloggers blog Navigation: https://blog.csdn.net/qq21497936/article/details/102478062
This article blog address: HTTPS: // Blog .csdn.net / qq21497936 / article / details / 104490409

 

 

Published 239 original articles · won praise 259 · views 430 000 +

Guess you like

Origin blog.csdn.net/qq21497936/article/details/104490409