OpenCV Development Notes (xv): Foundations of linear filtering algorithm - mean 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/104475771

table of Contents

Foreword

Demo

Mean Filter

Outline

Function prototype

Source Demo

Project templates: corresponds to the version number v1.10.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 (xv): Foundations of linear filtering algorithm - mean filter

 

Foreword

This chapter learning mean linear filtering with filtering.

 

Demo

 

Mean Filter

Outline

      Mean filter to cover only a weighted average of all pixel values ​​within the template accounting sub-region, with which the average gray value of a pixel point field instead of the return point. As is common 3x3 sub-accounting, the elements within the template region 9, mean filtering means to the current center pixel of FIG replaced by the following equation.

      Is well understood from the equation: accounting for the 3x3 sub-matrix, the size of the intermediate point is replaced with the average of 9 points.

      Mean filter algorithm faster rate.

      Mean Filter Algorithm presence fixed defect that it can not well protect the image detail, and then the image denoising also undermined the details of the image, whereby an image is blurred, can not properly remove the noise points.

Function prototype

      Mean filter function prototype is as follows:

void blur( InputArray src, 
           OutputArray dst,
           Size ksize, 
           Point anchor = Point(-1,-1),
           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-accounts.
  • Four parameters: Point type, showing the anchor (the point value of the smoothed). Note: The default value Point (-1, -1). If the point is negative, it means taking the center core of the anchor.
  • Five 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::testBlurFilter()
{
    QString fileName1 = "E:/qtProject/openCVDemo/openCVDemo/modules/openCVManager/images/1.jpg";
    cv::Mat matSrc = cv::imread(fileName1.toStdString());

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

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

    cv::Mat dstMat;
    dstMat = cv::Mat::zeros(matSrc.size(), matSrc.type());
    cv::Mat windowMat = cv::Mat(cv::Size(dstMat.cols * 3, dstMat.rows),
                                matSrc.type());
    int ksize = 3;      // 核心大小
    int anchor = -1;    // 锚点, 正数的时候必须小于核心大小,即:-1 <= anchor < ksize
    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, matSrc.rows),
                                    cv::Range(0, matSrc.cols));
        cv::addWeighted(leftMat, 1.0f, matSrc, 1.0f, 0.0f, leftMat);
        // 中间为调整滤波参数的相关设置
        cvui::printf(windowMat, 375, 40, "ksize");
        cvui::trackbar(windowMat, 375, 50, 165, &ksize, 1, 10);
        if(anchor >= ksize)
        {
            anchor = ksize - 1;
        }
        cvui::printf(windowMat, 375, 100, "anchor");
        cvui::trackbar(windowMat, 375, 110, 165, &anchor, -1, ksize-1);

        // 均值滤波:方框滤波比均值滤波多了颜色深度的参数
        cv::blur(matSrc,
                 dstMat,
                 cv::Size(ksize, ksize),
                 cv::Point(anchor, anchor));

        // 效果图copy到右边
        // 注意:rang从位置1到位置2,不是位置1+宽度
        cv::Mat rightMat = windowMat(cv::Range(0, matSrc.rows),
                                     cv::Range(matSrc.cols * 2, matSrc.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.10.0

      The corresponding version number v1.10.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 / 104475771

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

Guess you like

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