opencv----Filtering and sharpening

Filtering can be divided into two categories: linear filtering and non-linear filtering.OpenCV has these filter functions, which are very convenient to use. Now you can briefly introduce how to use them.

Linear Filter: Box Filter, Mean Filter, Gauss Filter

box filter

#include<opencv2\opencv.hpp>   
#include<opencv2\highgui\highgui.hpp>

using namespace std;
using namespace cv;


int main()
{
    Mat img = imread("lol4.jpg");
    imshow("Original Map", img);
    Mat out;
    boxFilter(img, out, -1, Size(5, 5));//-1 refers to the original map depth
    imshow("box filter", out);
    waitKey(0);

}

Mean filter
Mean filter is a special case of square filter.
The disadvantage of mean filter is that it can not protect the details well, destroy the details of the image while drying it, so that the image becomes blurred and the noise can not be removed well.

#include<opencv2\opencv.hpp>   
#include<opencv2\highgui\highgui.hpp>

using namespace std;
using namespace cv;


int main()
{
    Mat img = imread("lol4.jpg");
    imshow("Original Map", img);
    Mat out;
    blur(img, out,Size(5, 5));//-1 refers to the original map depth
    imshow("Mean filter", out);
    waitKey(0);

}

Gauss filter
Gauss filter, which can eliminate Gauss noise, is widely used in image processing.

#include<opencv2\opencv.hpp>   
#include<opencv2\highgui\highgui.hpp>

using namespace std;
using namespace cv;


int main()
{
    Mat img = imread("lol4.jpg");
    imshow("Original Map", img);
    Mat out;
    GaussianBlur(img, out, Size(3, 3), 0, 0);
    imshow("Gauss filter", out);
    waitKey(0);

}

The result shows that the blurring sensation is significantly reduced by Gaussian filter.

Nonlinear Filter: Median Filter, Bilateral Filter

median filtering

The basic idea is to replace the gray value of a pixel point with the domain gray value of the pixel point. This method can remove impulse noise, salt and pepper noise while preserving the details of the image (without blurring edges).

The idea of median filtering looks very similar to that of mean filtering, just an average, a median.

#include<opencv2\opencv.hpp>   
#include<opencv2\highgui\highgui.hpp>

using namespace std;
using namespace cv;

//median filtering
int main()
{
    Mat img = imread("lol4.jpg");
    imshow("Original Map", img);
    Mat out;
    medianBlur(img, out, 7);//The third parameter represents the linear size of the aperture, and its value must be an odd number greater than 1
    imshow("median filtering", out);
    waitKey(0);

}

Now let's talk about the comparison between median filter and mean filter: noise components in mean filter are added to the average calculation, so the output is affected by noise.However, in median filtering, the noise component is difficult to select, so the output is basically not affected.Of course, good performance comes at a cost, since median filtering takes more than five times as long as mean filtering.

Median filter generally uses odd convolution kernel.

Median filtering is not suitable for images with many details, especially thin, pointed images.

Bilateral filter

The biggest feature of bilateral filtering is edge preservation.

#include<opencv2\opencv.hpp>   
#include<opencv2\highgui\highgui.hpp>

using namespace std;
using namespace cv;

//Bilateral filter
int main()
{
    Mat img = imread("lol4.jpg");
    imshow("Original Map", img);
    Mat out;
    bilateralFilter(img, out, 25, 25 * 2, 25 / 2);
    imshow("Bilateral filter", out);
    waitKey(0);

}

The result image shows that the bilateral filter is the clearest of all filters.

Tags: OpenCV

Posted on Sat, 01 Feb 2020 08:16:15 -0800 by kark_1999