One of image processing techniques: & median filter mean filter

Transfer: & among openCV median filter mean filter (and code implementation): https://blog.csdn.net/weixin_37720172/article/details/72627543

 Before we begin today's blog, we need to understand what is filtering:

Among openCV median filter & mean filter (and code implementation) First we look at the concept of image filtering. Image filtering, i.e. noise suppression target image under conditions to preserve image detail features, are indispensable image preprocessing operation, the treatment effect is good or bad will directly affect the effectiveness of subsequent image processing and analysis, and reliability.

Figure on the left is a picture at the right is the noise figure:

Eliminating noise components in the image filtering operation is called smoothing or image. Most of the energy is concentrated in the image signal or the amplitude spectrum and low frequency bands are common, and in the higher frequency band, the information of interest is often overwhelmed by the noise. Thus a high-frequency component can be reduced the amplitude of the filter can attenuate the effects of noise.
There are two purposes of image filtering: extracting features of the object as one image recognition characteristic pattern; another is to meet the requirements of image processing, the image to eliminate noise mixed into digitized.
The requirements of the filtering process also has two: one can not damage the important information and the contour of the edge image and the like; second image clarity is good visual effect.

Smoothing filter is a low-frequency enhanced spatial domain filtering techniques. Its purpose has two types: one is blurred; the other is to eliminate noise.
Smoothing spatial domain generally use simple average method, it is to compute the average luminance value of the pixel adjacent to the point. Is directly related to the size of the neighborhood and the effect of smoothing, the better the larger the effect of smoothing the neighborhood, the neighborhood is too large, the smoothing causes greater loss of edge information, so that the output image becomes blurred, and therefore need a reasonable choice o size of the field.
Filter on a vivid metaphor method is: we can imagine a filter weighting coefficients of the window comprises, when using the smoothing filter image, on the image put into the window, through the window look at the image we get.

 

For a filtering application in our lives: microdermabrasion beauty of function. If we face bumpy likened to the noise, then the algorithm is to filter out the noise, so we selfie skin looks very smooth.

This blog will introduce the median filter and mean filter two algorithms

 

A. Mean filter

          Within a square picture region (typically 3 * 3), the center of the average value of all pixel values ​​of the pixel points. Average filtering operation is carried out for more than the whole picture.

We can look at Matrix will be understood

                                                                      

                         

Defects: mean filter itself, there are inherent drawback that it can not well protect the image detail while also destroyed the image denoising details of the image, so that the image becomes blurred, can not properly remove the noise points. In particular, salt and pepper noise

Implementation code:

 
#include "opencv2/imgproc.hpp"

#include "opencv2/highgui.hpp"

#include<ctime>

using namespace cv;

using namespace std;


//均值滤波

void AverFiltering(const Mat &src,Mat &dst) {

if (!src.data) return;

//at访问像素点

for (int i = 1; i<src.rows; ++i)

for (int j = 1; j < src.cols; ++j) {

if ((i - 1 >= 0) && (j - 1) >= 0 && (i + 1)<src.rows && (j + 1)<src.cols) {//边缘不进行处理

dst.at<Vec3b>(i, j)[0] = (src.at<Vec3b>(i, j)[0] + src.at<Vec3b>(i - 1, j - 1)[0] + src.at<Vec3b>(i - 1, j)[0] + src.at<Vec3b>(i, j - 1)[0] +

src.at<Vec3b>(i - 1, j + 1)[0] + src.at<Vec3b>(i + 1, j - 1)[0] + src.at<Vec3b>(i + 1, j + 1)[0] + src.at<Vec3b>(i, j + 1)[0] +

src.at<Vec3b>(i + 1, j)[0]) / 9;

dst.at<Vec3b>(i, j)[1] = (src.at<Vec3b>(i, j)[1] + src.at<Vec3b>(i - 1, j - 1)[1] + src.at<Vec3b>(i - 1, j)[1] + src.at<Vec3b>(i, j - 1)[1] +

src.at<Vec3b>(i - 1, j + 1)[1] + src.at<Vec3b>(i + 1, j - 1)[1] + src.at<Vec3b>(i + 1, j + 1)[1] + src.at<Vec3b>(i, j + 1)[1] +

src.at<Vec3b>(i + 1, j)[1]) / 9;

dst.at<Vec3b>(i, j)[2] = (src.at<Vec3b>(i, j)[2] + src.at<Vec3b>(i - 1, j - 1)[2] + src.at<Vec3b>(i - 1, j)[2] + src.at<Vec3b>(i, j - 1)[2] +

src.at<Vec3b>(i - 1, j + 1)[2] + src.at<Vec3b>(i + 1, j - 1)[2] + src.at<Vec3b>(i + 1, j + 1)[2] + src.at<Vec3b>(i, j + 1)[2] +

src.at<Vec3b>(i + 1, j)[2]) / 9;

}

else {//边缘赋值

dst.at<Vec3b>(i, j)[0] = src.at<Vec3b>(i, j)[0];

dst.at<Vec3b>(i, j)[1] = src.at<Vec3b>(i, j)[1];

dst.at<Vec3b>(i, j)[2] = src.at<Vec3b>(i, j)[2];

}

}

}

//图像椒盐化

void salt(Mat &image, int num) {

if (!image.data) return;//防止传入空图

int i, j;

srand(time(NULL));

for (int x = 0; x < num; ++x) {

i = rand() % image.rows;

j = rand() % image.cols;

image.at<Vec3b>(i, j)[0] = 255;

image.at<Vec3b>(i, j)[1] = 255;

image.at<Vec3b>(i, j)[2] = 255;

}

}

void main() {

Mat image = imread("路飞.jpg");


Mat Salt_Image;

image.copyTo(Salt_Image);

salt(Salt_Image, 3000);


Mat image1(image.size(), image.type());

Mat image2;

AverFiltering(Salt_Image, image1);

blur(Salt_Image, image2, Size(3, 3));//openCV库自带的均值滤波函数

imshow("原图", image);

imshow("自定义均值滤波", image1);

imshow("openCV自带的均值滤波", image2);

waitKey();

}


Renderings:

 

You can see the picture blur and noise is not very effective in removing the blurring algorithm is just a picture of it.

II. Median filtering

       First, we review the value. In a series of numbers in {1,4,6,8,9}, numeral 6 is simply the string of numbers. From this we can be applied to image processing. We still go in the image matrix 3 * 3, which has nine pixels, we will sort 9 pixels, and finally the central point of this assignment matrix for the nine pixel values.

                                     

Code:

//求九个数的中值

uchar Median(uchar n1, uchar n2, uchar n3, uchar n4, uchar n5,

uchar n6, uchar n7, uchar n8, uchar n9) {

uchar arr[9];

arr[0] = n1;

arr[1] = n2;

arr[2] = n3;

arr[3] = n4;

arr[4] = n5;

arr[5] = n6;

arr[6] = n7;

arr[7] = n8;

arr[8] = n9;

for (int gap = 9 / 2; gap > 0; gap /= 2)//希尔排序

for (int i = gap; i < 9; ++i)

for (int j = i - gap; j >= 0 && arr[j] > arr[j + gap]; j -= gap)

swap(arr[j], arr[j + gap]);

return arr[4];//返回中值

}


//图像椒盐化

void salt(Mat &image, int num) {

if (!image.data) return;//防止传入空图

int i, j;

srand(time(NULL));

for (int x = 0; x < num; ++x) {

i = rand() % image.rows;

j = rand() % image.cols;

image.at<Vec3b>(i, j)[0] = 255;

image.at<Vec3b>(i, j)[1] = 255;

image.at<Vec3b>(i, j)[2] = 255;

}

}


//中值滤波函数

void MedianFlitering(const Mat &src, Mat &dst) {

if (!src.data)return;

Mat _dst(src.size(), src.type());

for(int i=0;i<src.rows;++i)

for (int j=0; j < src.cols; ++j) {

if ((i - 1) > 0 && (i + 1) < src.rows && (j - 1) > 0 && (j + 1) < src.cols) {

_dst.at<Vec3b>(i, j)[0] = Median(src.at<Vec3b>(i, j)[0], src.at<Vec3b>(i + 1, j + 1)[0],

src.at<Vec3b>(i + 1, j)[0], src.at<Vec3b>(i, j + 1)[0], src.at<Vec3b>(i + 1, j - 1)[0],

src.at<Vec3b>(i - 1, j + 1)[0], src.at<Vec3b>(i - 1, j)[0], src.at<Vec3b>(i, j - 1)[0],

src.at<Vec3b>(i - 1, j - 1)[0]);

_dst.at<Vec3b>(i, j)[1] = Median(src.at<Vec3b>(i, j)[1], src.at<Vec3b>(i + 1, j + 1)[1],

src.at<Vec3b>(i + 1, j)[1], src.at<Vec3b>(i, j + 1)[1], src.at<Vec3b>(i + 1, j - 1)[1],

src.at<Vec3b>(i - 1, j + 1)[1], src.at<Vec3b>(i - 1, j)[1], src.at<Vec3b>(i, j - 1)[1],

src.at<Vec3b>(i - 1, j - 1)[1]);

_dst.at<Vec3b>(i, j)[2] = Median(src.at<Vec3b>(i, j)[2], src.at<Vec3b>(i + 1, j + 1)[2],

src.at<Vec3b>(i + 1, j)[2], src.at<Vec3b>(i, j + 1)[2], src.at<Vec3b>(i + 1, j - 1)[2],

src.at<Vec3b>(i - 1, j + 1)[2], src.at<Vec3b>(i - 1, j)[2], src.at<Vec3b>(i, j - 1)[2],

src.at<Vec3b>(i - 1, j - 1)[2]);

}

else

_dst.at<Vec3b>(i, j) = src.at<Vec3b>(i, j);

}

_dst.copyTo(dst);//拷贝

}



void main() {

Mat image = imread("路飞.jpg");


Mat Salt_Image;

image.copyTo(Salt_Image);

salt(Salt_Image, 3000);


Mat image3, image4;

MedianFlitering(Salt_Image, image3);

medianBlur(Salt_Image, image4, 3);

imshow("自定义中值滤波处理后", image3);

imshow("openCV自带的中值滤波", image4);

waitKey();

}

Renderings:

We can see, good salt and pepper noise is smoothed, and it did not mean that blur too serious.

Three padding issue

When an image filter is applied to filter, a boundary problem is to process. In general, there are three kinds of treatment methods.

1. not to deal with border

Boundary of the image is not any treatment, while the image is filtered, the filter is not applied to the image periphery, and therefore no change in the image periphery.

 

2. filling 0

Extension of the boundary of the image do, zero fill the extended boundary, in square filter for 2k + 1, the extended boundary size is k, if the original image is a [m, n], then the image becomes extended [m + 2k, n + 2k]. After filtering, the image of a black border appears.

 

3. Fill the nearest pixel values

0 expansion and filling expansion similar, but the filling is in the extended extension 0 0 partially filled, and this is the closest value to fill pixels.

Four summary:

Mean and median filtering and can play a smooth image filtering, noise is considered to function.

Mean filter using the linear method, the average pixel value of the entire window range, mean filter itself, there are inherent drawback that it can not well protect the image detail while also destroyed the image denoising details of the image, whereby the image becomes blurred, can not properly remove the noise points. Mean filter performed better Gaussian noise, poor performance of the salt and pepper noise.

Nonlinear median filter method, it is very effective in terms of noise pulses smooth, and it can protect the image sharp edges, instead of selecting an appropriate point value of contaminated sites, so the good effect, better performance of salt and pepper noise, poor performance of the Gaussian noise.

reference:

1. The median filter and mean filter

https://blog.csdn.net/cjsh_123456/article/details/79261271

Published 377 original articles · won praise 145 · views 210 000 +

Guess you like

Origin blog.csdn.net/Windgs_YF/article/details/104353147