OpenCV Development Notes (XVIII): Foundations of Nonlinear Filtering Algorithm - median 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/104529643

table of Contents

Foreword

Demo

Median filter

Outline

Function prototype

Source Demo

Project templates: corresponds to the version number v1.13.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 (XVIII): Foundations of Nonlinear Filtering Algorithm - median filter

 

Foreword

Learning this chapter median filter to nonlinear filtering.

 

Demo

 

Median filter

Outline

      Median filter means sorting all the pixel values ​​within the area covered by the sub-accounting template, the pixel values ​​of the intermediate position is used to update a value of the current pixel point.

Common accounting sub 3x3 , 9 within the template area elements, the 9 th all elements resolved in accordance with small to large order of A1, A2, A3, A4, A5, A6, A7, A8, A9 , median filtering is to take intermediate a value a5 alternative intermediate point.

      Since the median filter mean filter, a filter is often used, but when the template is gradually increased, will run a certain confusion in terms of fuzzy boundaries boundary, sharpness of the picture remains substantially median filter processing pepper noise is very effective, median filtering can reduce or eliminate high frequency components of the Fourier control, but also to low frequency component.

      Median filter to the noise effect depends on two factors: the number of pixels of the spatial extent and relates value calculation fields. Generally, the filter is less than half of the area of light or dark objects are filtered out essentially while the larger objects will be preserved almost intact, and therefore the size of the space must be based on the median filter conventional problems be adjusted.

      Median filter non-linear filtering, a linear filtering is easy to implement, and easily from the viewpoint of frequency response analysis, single particles if the noise is Gaussian noise rather than save early, linear filtering can not remove the noise. The image appears extreme points, linear filtering noise transfer just a gentle but still visible particulate, the best solution is to filter out the noise by non-linear filtering.

Function prototype

void medianBlur( InputArray src,
               OutputArray dst,
               int ksize );
  • A parameter : InputArray type generally cv :: Mat, 3 or 4 may be processed picture number of channels. Note, however, when ksize 3 or 5:00, picture depth should CV_8U , CV_16U , CV_16S , CV_32F , and for larger aperture size of the picture, he can only be CV_8U .
  • 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-accounting, must be an odd number greater than or equal to 1 (equal to 1 there is no filtering effect).

 

Source Demo

void OpenCVManager::testMedianBlur()
{
    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;      // 核心大小
    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, 140, "ksize");
        cvui::trackbar(windowMat, 375, 150, 165, &ksize, 0, 10);

        // 中值滤波
        cv::medianBlur(srcMat, dstMat, ksize * 2 + 1);

        // 效果图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.13.0

      The corresponding version number v1.13.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 / 104529643

发布了239 篇原创文章 · 获赞 259 · 访问量 43万+

Guess you like

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