The common image processing filter opencv

Image Smoothing

Smoothing, also called blurring, is a simple and frequently used image processing operation.

Smooth, also known as fuzzy.

Essentially the conversion of the pixel values ​​of a point and its superposition of different weights .h (k, l) is the convolution kernel, or call filter pixel values ​​around the filter.

There are several common filter

  • Normalized Box Filter
  • Gaussian Filter
  • Median Filter
  • Bilateral Filter

Mean Filter


Weight matrix as described above.

img2= cv2.blur(img,(5,5))

The call blur(src, dst, ksize, anchor, borderType) is equivalent to boxFilter(src, dst, src.type(), anchor, true, borderType).
https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#ga8c45db9afe636703801b0b2e440fce37

Results are as follows:

Gaussian filter

That is assumed that the pixel to a location and its neighboring pixels Gaussian distribution. Specifically, it is the weight of the pixel at each position of the weight Gaussian distribution. In this case, given a Gaussian distribution, and the size of the Gaussian kernel, can be calculated weight values of the surrounding n pixels.

FIG formula shows on a Gaussian distribution and dimensions of the two-dimensional Gaussian distribution. the image dimension is 2. (i.e., a row dimension and a column dimension to determine the position of the pixel).

Details of the calculation of the Gaussian filter may reference https://blog.csdn.net/zr459927180/article/details/52400641

Get opencv Gaussian core function in getGaussianKernel, but the acquisition of a one-dimensional Gaussian kernel of images, in terms of 3 * 3 neighborhood, we should get the right to a 3x3 weight matrix can be obtained as follows:

    kernal_x = cv2.getGaussianKernel(3,-1)
    kernal_y = cv2.getGaussianKernel(3,-1)
    kernal_filter = np.dot(kernal_x,kernal_y.T)
    print(kernal_filter)

Output is as follows:

[[0.0625 0.125  0.0625]
 [0.125  0.25   0.125 ]
 [0.0625 0.125  0.0625]]

The luminance value of the intermediate element after converted into Gauss 0.0625 xp (0,0) + 0.125 xp (0,1) + .... + 0.0625 xp (2,2), the weight matrix can be seen that the sum is equal to 1.

Here, we use the example 3 X 3 Gaussian kernel, in fact, must be a Gaussian kernel is not limited to a square.

Back to the cv of GaussianBlur (),

Its parameters sigmaX, sigmaY i.e. x, Gauss distribution on the difference in the y-direction. This can be obtained in different directions Gaussian matrix, to do matrix multiplication, i.e., to obtain a weight weight mxn matrix then obtain the Gaussian image conversion .

We know Gaussian distribution (normal distribution also known) characteristics are, the larger the standard deviation, the more dispersed distribution, the smaller the standard deviation, the distribution of the concentration. Therefore, the Gaussian Blur transfer large () in sigmaX, sigmaY the image such that each of more pixels surrounding a reference pixel, i.e., a smoother or blurred.

:( selected this picture is not good See Figure, the Gaussian blur effect is not obvious, but you can still see some of the more obscure Figure 3)

import cv2
import numpy as np
def test():
    imgname = "/home/sc/opencv-3.4.3/samples/data/lena.jpg"
    img = cv2.imread(imgname)

    img2 = img.copy()
    #img2 = cv2.blur(img,(5,5))
    img2 = cv2.GaussianBlur(img,(5,7),1)
    img3 = cv2.GaussianBlur(img,(5,7),100)

    kernal_x = cv2.getGaussianKernel(3,0)
    kernal_y = cv2.getGaussianKernel(3,0)
    kernal_filter = np.dot(kernal_x,kernal_y.T)
    print(kernal_filter)
    
    kernal_x = cv2.getGaussianKernel(3,5)
    kernal_y = cv2.getGaussianKernel(3,5)
    kernal_filter = np.dot(kernal_x,kernal_y.T)
    print(kernal_filter)
    
    #return

    cv2.imshow("origin",img)
    cv2.imshow("dst",img2)
    cv2.imshow("dst3",img3)

    k=cv2.waitKey()
    if k == 27:
        cv2.destroyAllWindows()
        
test()

Output getGaussianKernel () can be clearly seen, large standard deviation adjustment, transformation weighting matrix.

Median filter

I.e., the median value of pixel values of neighboring pixels becomes.

Note that, kernal size must be odd.

import cv2
def test():
    imgname = "/home/sc/opencv-3.4.3/samples/data/lena.jpg"
    img = cv2.imread(imgname)

    dst = cv2.medianBlur(img, 1)
    dst2 = cv2.medianBlur(img, 11)
    
    cv2.imshow("origin",img)
    cv2.imshow("dst",dst)
    cv2.imshow("dst2",dst2)

    k=cv2.waitKey()
    if k == 27:
        cv2.destroyAllWindows()
test()

Bilateral Filtering

Bilateral filtering function is cv2.bilateralFilter (), bilateral filtering can be in case of holding a clear boundary effectively remove the noise. However, this operation would be slower compared to other filters . We have seen that the Gaussian filter is a Gaussian weighted average seek the center point of the adjacent area of the pixel. This Gaussian filter only considers the spatial relationship between pixels, and does not consider the relationship between the pixel values (pixel similarity). Therefore, this method does not consider whether a pixel located at the boundary. Therefore, the boundary will be obscured, and this is not what we want.

Bilateral filtering while using Gaussian weights and spatial gray value similarity Gaussian weight . Gaussian spatial region adjacent to the pixel to ensure that only the central point of impact, the gradation value of the Gaussian function to ensure that only the similarity with the center pixel grayscale values will be used for similar fuzzy operation . So this method will ensure that the border will not be obscured, because gray value at the border is relatively large.

That is to simply drop, when generating the peripheral pixels weight matrix, if it is found next to the large pixel values and the current pixel value differences, it is only to a small right great differences that element weight distribution, such a "big difference mutation . it was retained "
principle of bilateral filtering can refer to: https://blog.csdn.net/shenziheng1/article/details/50838970

dst = cv2.bilateralFilter(img, 9, 75, 75)

Renderings:

you can see the texture are blurred out, but the border is still well preserved.

参考:参考:<https://docs.opencv.org/master/dc/dd3/tutorial_gausian_median_blur_bilateral_filter.html >
https://blog.csdn.net/shenziheng1/article/details/50838970
https://blog.csdn.net/GiffordY/article/details/91891920

Guess you like

Origin www.cnblogs.com/sdu20112013/p/11600436.html