OpenCV Development Notes (17): The basic algorithm for linear filtering contrast - block, mean, Gaussian

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/104514873

table of Contents

Foreword

Comparison of three kinds of linear filter

Salt and pepper noise

Impulse noise

Demo Demonstration

Source Demo

Project templates: corresponds to the version number v1.12.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 (17): The basic algorithm for linear filtering contrast - block, mean, Gaussian

 

Foreword

      Several of Three former linear filtering has been detailed, but how linear filtering options, or need to try a variety of pictures.

 

Comparison of three kinds of linear filter

Filter Type

Fundamental

Feature

Filter block

Pixels within the template using convolution

 

Mean Filter

Instead of using the average pixel grayscale template center values ​​of all pixels within the template, the mean filter is a special form of block filtering

Yi received noise interference, can not completely eliminate the noise, only the relative weakening of noise

Gaussian filter

Gaussian filtering is input to each pixel array is a Gaussian convolution kernel. When an image is smoothed pixel neighborhood, neighborhood pixels of different locations are given different weights

Smoothing the image while at the same time the overall gradation can be retained more images of distribution

 

Salt and pepper noise

Also referred to as salt and pepper noise impulse noise, a noise image is often seen, it is a random white spots or black spots, there may be a bright region with a black pixel or a white pixel in the dark region ( or a combination of both). Causes of salt and pepper noise may be subject to strong interference sudden image signal is generated, analog to digital converter bit or transmission errors. For example, results in failure of the sensor pixel value is a minimum value, the sensor leads to a saturated pixel value is a maximum value.

Impulse noise

Impulse noise (pulse noise) collectively discrete noise occurs in communication. It consists of bursty interference occurring irregular time.

Impulse noise (impulsive noise) is discontinuous, by a short duration and large amplitude of the irregular pulses or noise spikes composition. Causes impulsive noise are numerous, including faults and defects electromagnetic interference and a communication system, it may also be generated when the electrical switches and relays the communication system changes state.

Impulse noise on the analog data is generally only a small trouble. However, in a digital data communication, impulse noise is the main reason for the error.

Impulse noise, it is less than 1 second duration, greater than 10dB noise intensity than the peak RMS value, and the repetition frequency of less than 10Hz intermittent noise.

Impulse noise: sudden burst and disappear quickly, for example: the duration ≤0.5s, intervals> 1s, the effective value of the sound pressure changes ≥40dB (A) of noise.

(Added: against noise, follow-up will be dedicated to writing a comprehensive analysis and process, summarize and generalize the characteristics and treatment as well as the corresponding Demo demo)

 

Demo Demonstration

      Were loaded with 5 different types of diagrams do linear filtering, in fact, the gap does not look great, there is a small bit of filtering effect may be better, but I Yanzhuo did not see a big difference, but for the abrupt noise (salt and pepper noise ) is indeed the results were good, the first filtering and then sharpening, should be able to remove salt and pepper noise.

 

Source Demo

void OpenCVManager::testBoxAndBlurAndGaussianBlurFilter()
{
    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);

    cv::resize(srcMat, srcMat, cv::Size(400, 300));

    qDebug() << __FILE__ <<__LINE__ << srcMat.rows << srcMat.cols;
    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(srcMat.cols * 3, srcMat.rows * 3),
                                srcMat.type());
    bool isBoxFilter = true;
    int ksize = 3;      // 核心大小
    int anchor = -1;    // 锚点, 正数的时候必须小于核心大小,即:-1 <= anchor < ksize
    int ksize2 = 3;      // 核心大小
    int anchor2 = -1;    // 锚点, 正数的时候必须小于核心大小,即:-1 <= anchor < ksize
    int ksize3 = 3;      // 核心大小
    int sigmaX = 0;  // x方向的标准偏差
    int sigmaY = 0;  // y方向的标准偏差
    while(true)
    {
        windowMat = cv::Scalar(0, 0, 0);
        cvui::window(windowMat, dstMat.cols, dstMat.rows * 0, dstMat.cols, dstMat.rows, "boxFilter settings");
        cvui::window(windowMat, dstMat.cols, dstMat.rows * 1, dstMat.cols, dstMat.rows, "blurFilter settings");
        cvui::window(windowMat, dstMat.cols, dstMat.rows * 2, dstMat.cols, dstMat.rows, "gaussianBlurFilter settings");
        {
            // 原图先copy到左边
            cv::Mat leftMat = windowMat(cv::Range(0, srcMat.rows),
                                    cv::Range(0, srcMat.cols));
            cv::addWeighted(leftMat, 0.0f, srcMat, 1.0f, 0.0f, leftMat);
            // 中间为调整方框滤波参数的相关设置
            // 是否方框滤波
            cvui::checkbox(windowMat, 500, 60, "boxFilter", &isBoxFilter);
            cvui::printf(windowMat, 500, 120, "ksize");
            cvui::trackbar(windowMat, 500, 130, 200, &ksize, 1, 10);
            if(anchor >= ksize)
            {
                anchor = ksize - 1;
            }
            cvui::printf(windowMat, 500, 180, "anchor");
            cvui::trackbar(windowMat, 500, 190, 200, &anchor, -1, ksize-1);

            // 方框滤波
            cv::boxFilter(srcMat,
                          dstMat,
                          -1,
                          cv::Size(ksize, ksize),
                          cv::Point(anchor, anchor),
                          isBoxFilter);

            // 效果图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);
        }
        {
            // 原图先copy到左边
            cv::Mat leftMat = windowMat(cv::Range(srcMat.rows * 1, srcMat.rows * 2),
                                    cv::Range(0, srcMat.cols));
            cv::addWeighted(leftMat, 0.0f, srcMat, 1.0f, 0.0f, leftMat);
            // 中间为调整滤波参数的相关设置
            cvui::printf(windowMat, 500, 100 + 300, "ksize");
            cvui::trackbar(windowMat, 500, 110 + 300, 200, &ksize2, 1, 10);
            if(anchor2 >= ksize2)
            {
                anchor2 = ksize2 - 1;
            }
            cvui::printf(windowMat, 500, 160 + 300, "anchor");
            cvui::trackbar(windowMat, 500, 170 + 300, 200, &anchor2, -1, ksize2-1);

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

            // 效果图copy到右边
            // 注意:rang从位置1到位置2,不是位置1+宽度
            cv::Mat rightMat = windowMat(cv::Range(srcMat.rows * 1, srcMat.rows * 2),
                                         cv::Range(srcMat.cols * 2, srcMat.cols * 3));
            cv::addWeighted(rightMat, 0.0f, dstMat, 1.0f, 0.0f, rightMat);
        }
        {
            // 原图先copy到左边
            cv::Mat leftMat = windowMat(cv::Range(srcMat.rows * 2, srcMat.rows * 3),
                                        cv::Range(0, srcMat.cols));
            cv::addWeighted(leftMat, 0.0f, srcMat, 1.0f, 0.0f, leftMat);
            // 中间为调整滤波参数的相关设置
            cvui::printf(windowMat, 500, 60 + 600, "ksize = size *  2 + 1");
            cvui::trackbar(windowMat, 500, 70 + 600, 200, &ksize3, 0, 10);

            cvui::printf(windowMat, 500, 120 + 600, "sigmaX");
            cvui::trackbar(windowMat, 500, 130 + 600, 200, &sigmaX, 0, 100);

            cvui::printf(windowMat, 500, 180 + 600, "sigmaY");
            cvui::trackbar(windowMat, 500, 190 + 600, 200, &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(srcMat.rows * 2, srcMat.rows * 3),
                                         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.12.0

      The corresponding version number v1.12.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 / 104514873

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

Guess you like

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