The introduction

First of all, I admit that I have lost my title. I just want to improve my reading. Is it easy for me?

After all, the amount of reading is too low to be motivated to write further, so I can only improve my reading volume occasionally with small measures.

I’m going to switch to this article, and I’m going to put the results first, and then I’m going to scroll down, and then I’m going to go to the top left corner, and I’m going to pretend that I tricked you in.

The results of

Then put the source code directly:

import cv2 as cv

source = cv.imread("zhaopian.jpg")
dst = cv.bilateralFilter(src=source, d=0, sigmaColor=30, sigmaSpace=15)

cv.imshow("source", source)
cv.imshow("dst", dst)

cv.waitKey()
cv.destroyAllWindows()
Copy the code

Ok, the end of the results section of this paper, those who want to know more about the principle can continue to read, not interested in the upper left corner.

Principle of bilateral filtering

The above picture beauty effect is actually using the bilateral filter OpenCV provided for us, is a picture noise reduction algorithm.

Those interested in other image denoising or image filters can refer to the previous section “Python Image Processing OpenCV (7) : Image Smoothing (Filtering) Processing”.

If you haven’t seen it, you are highly advised to read it first, at least read the last part of gaussian filtering, otherwise the following bilateral filtering will be difficult to understand.

Bilateral filter is a nonlinear filtering method, essentially based on Gaussian filtering.

As mentioned in the previous article, gaussian filtering will cause edge blurriness, which can not be helped. This is caused by gaussian filtering.

On the basis of Gaussian filtering, bilateral filtering improves the way of Gaussian filtering, combines the spatial proximity and pixel value similarity of image, and considers the domain and range information at the same time, so as to achieve the purpose of edge protection and noise reduction.

In the process of filtering, it is necessary to consider not only the difference between the size of the surrounding pixel value and the pixel value of the middle point, but also the space distance, and then determine the influence factor of the point on the middle point.

For example, in an image, the color of adjacent pixels will be very similar, but in the edge region, the color change of adjacent elements will be very large.

In the filtering process of Gaussian filter, the edge of the filtered image is blurred because the edge region is not taken into account. In the filtering process of bilateral filter, the difference between the value of the surrounding pixel and the pixel value of the midpoint is considered, so as to determine an influence factor, so as to achieve the image edge preservation and noise reduction.

The specific implementation principle is as follows:

I hope you didn’t give all your high school math back to your math teacher

The whole bilateral filtering algorithm is divided into two parts, one is the similarity of color value (range kernel), the formula is as follows:


The other is to calculate the similarity of spatial distance (spatial kernel), that is, the closer the distance is, the higher the similarity is, and the formula is as follows:


The logic is still clear, so don’t get confused by a bunch of symbols.

Here (I, j) represents the coordinate points of the pixels to be processed, while (k, L) is the coordinate points of the pixels in the range to be processed that may affect their values.

The final weight coefficient W (I,j, K, L) depends on the product of the spatial kernel and the range kernel, and the formula is as follows:


The following is a classical schematic diagram of the principle of bilateral filtering:

Can be seen from the diagram, in figure (b) the airspace cores, the weight of each pixel is in conformity with the gaussian distribution, and in figure (c) the range of values on nuclear, due to the large pixel values differ, the weight coefficient of different color difference is big, after the completion of the bilateral filter to filter, on either side of the edge pixels to retain the original color values.

Next, let’s look at the original function of bilateral filtering:

def bilateralFilter(src, d, sigmaColor, sigmaSpace, dst=None, borderType=None)
Copy the code
  • SRC:
  • D: diameter of pixel neighborhood. If the value is set to non-positive, OpenCV evaluates it from sigmaSpace, the fifth argument.
  • SigmaColor: color space filterValue. A larger value of this parameter indicates that a wider range of colors in the pixel’s neighborhood will be mixed together, resulting in a larger semi-equal color region.
  • SigmaSpace: filter in coordinate spaceValue, the annotation variance of the coordinate space. The larger his value, the more distant the pixels will affect each other, so that the larger the area is similar enough to get the same color. When d > 0, d specifies the neighborhood size and is independent of sigmaSpace. Otherwise, d is proportional to sigma ace. I found that the larger the value, the better the transition effect of the image.

I’m not going to show you the source code, it’s really just that one line of code, but hopefully those of you reading this article will understand a little bit more than just how bilateral filtering can beautifying images.