Smoothing, sometimes called fuzzy processing, is the most common operation in the field of image processing. The main purpose of smoothing is to remove noise. Smoothing is usually done by convolution operations (note: these are actually related operations, and the following description does not distinguish between the two operations). The following describes several common smoothing filtering operations.

Average filtering

  • Mean filtering is the simplest filtering operation. Each filtered pixel is calculated from the mean value of its neighboring pixels.

  • Kernel of mean filtering is as follows:

     

     

Gaussian filter

  • The kernel of Gaussian filter is constructed by Gaussian function. The closer the pixel is to the center, the larger the value is, which also reflects the relationship between image space. The closer the pixel is, the stronger the correlation is.

  • The two-dimensional Gaussian function is as follows:

     

     

Median filtering

  • The median value of kernel pixel is taken as the value of filtered pixel.
  • Median filtering is a nonlinear smoothing technique.
  • Median filtering method is very effective in eliminating salt and pepper noise.

Bilateral filtering

  • The above filtering method will not only smooth the noise but also blur the edges of the image. This is because they only use spatial location information when constructing the kernel.
  • The kernel constructed by bilateral filtering uses not only spatial location information but also color information.

In OpencV provides a variety of filter call interface, the code is as follows:

#include<opencv2/opencv.hpp> using namespace cv; using namespace std; int main() { namedWindow("Smoothing Demo", WINDOW_AUTOSIZE); Mat src = imread("G:\\opencvDemo\\lena.jpg"); Mat blurImg; Mat gaussianImg; Mat medianImg; Mat bilteralImg; Mat boxImg; Mat filter2DImg; blur(src, blurImg, Size(5, 5)); boxFilter(src, boxImg, -1, Size(5, 5), Point(-1, -1), true); GaussianBlur (SRC, gaussianImg, Size (5, 5), 0, 0). medianBlur(src, medianImg, 5); BilateralFilter (SRC, bilteralImg, 5, 10, 2.5); Mat kernal = Mat::ones(Size(5, 5), CV_8UC1); filter2D(src, filter2DImg, -1, kernal); //imshow("Smoothing Demo", bilteralImg); imwrite("blurImg55.jpg", blurImg); imwrite("boxImg55.jpg", boxImg); imwrite("gaussianImg55.jpg", gaussianImg); imwrite("medianImg55.jpg", medianImg); imwrite("bilteralImg55.jpg", bilteralImg); imwrite("filter2DImg55.jpg", filter2DImg); //waitKey(0); return 0; }Copy the code

Experimental results:

The original image Lena.jpg is as follows:

 

Mean filtering:

Filter function: blur()

void cv::blur ( InputArray src, OutputArray dst, Size ksize, Point anchor = Point(-1,-1), Int borderType = BORDER_DEFAULT) SRC: input image DST: output image ksize: kernel size anchor: The position of the filtered pixel in the kernel, the default Point(-1,-1) represents the borderType in the center of the kernel: image boundary filling methodCopy the code

The following figure shows the experimental results when kernel size is 3×3 and 5×5 respectively:

 

 

Gaussian filtering:

Filter function: GaussianBlur()

void cv::GaussianBlur ( InputArray src, OutputArray dst, Size ksize, double sigmaX, double sigmaY = 0, Int borderType = BORDER_DEFAULT) SRC: input image DST: output image ksize: kernel size sigmaX: Gaussian kernel standard deviation in the X direction sigmaY: Standard deviation of the Gaussian kernel in the Y direction. If sigmaY=0, sigmaY is set to be equal to sigmaX. If sigmaX and sigmaY are both 0, they are calculated from ksie.width and ksie.height. BorderType: How image boundaries are filledCopy the code

The following figure shows the experimental results when kernel size is 3×3 and 5×5 respectively:

 

 

Median filtering:

Filter: medianBlur()

Void CV ::medianBlur (InputArray SRC, OutputArray DST, int ksize) SRC: input image DST: output image ksize: kernel sizeCopy the code

The following figure shows the experimental results when kernel size is 3×3 and 5×5 respectively:

 

 

Bilateral filtering:

Function: bilateralFilter ()

void cv::bilateralFilter ( InputArray src, OutputArray dst, int d, double sigmaColor, double sigmaSpace, Int borderType = BORDER_DEFAULT) SRC: input image DST: output image d: Calculate the diameter of the neighborhood pixel during filtering. If it is not positive, it will be calculated by sigmaSpace. SigmaColor: The influence factor of color space. SigmaSpace: Airspace influence factor. BorderType: How image boundaries are filledCopy the code

The following figure shows the experimental results when kernel size is 3×3 and 5×5 respectively:

 

 

Supplement:

Two filter functions are added below:

BoxFilter () and filter2D ()

1, boxFilter ()

void cv::boxFilter ( InputArray src, OutputArray dst, int ddepth, Size ksize, Point anchor = Point(-1,-1), Bool normalize = true, int borderType = BORDER_DEFAULT) Normalize = true, int borderType = BORDER_DEFAULT Pixel values after filtering are the sum of pixel values in kernelCopy the code

The kernel is as follows:

 

Normalize = false and ksize = 3×3

 

2, filter2D ()

void cv::filter2D ( InputArray src, OutputArray dst, int ddepth, InputArray kernel, Point anchor = Point(-1,-1), Double delta = 0, int borderType = BORDER_DEFAULT) kernel is the filter kernel to use and can be built on its own as neededCopy the code

The following figure constructs a 3×3 all-1 filtering kernel, which can verify that the result is the same as the figure above:

 

summary

The above is the basic content of smoothing filtering, the key lies in the selection of filtering core.

Welcome to leave a message.