Image filtering of Opencv: 2. Mean filtering (cv2.blur)

        After introducing image convolution, we started to learn various filtering methods, and today we will learn about mean filtering.

2.1. Principle introduction

        When performing mean filtering, the first thing to consider is how many surrounding pixels need to be averaged. Usually, we will take the current pixel as the center and average the pixel values ​​of all pixels in an area with the same number of rows and columns.

example:

For matrices:

        

For the selected 3×3 matrix, select the center pixel and perform operations on this matrix

Center point new value = (1+8+15+2+9+16+3+10+17)/9

                   = 9

For edge pixels, as shown in the figure:

        

   new value = (1+8+2+9)/4

           =  5

        In addition, the surrounding pixels of the current image can also be expanded. After the edge extension of the image is completed, different pixel values ​​can be filled in the newly added rows and columns. OpenCV provides a variety of boundary processing methods, and we can choose different boundary processing modes according to actual needs.

2.2 Function Syntax

        In OpenCV, the function that implements mean filtering is cv2.blur(), and its syntax format is:          

        dst=cv2.blur(src,ksize,anchor,borderType)

        In the formula:

         ● dst is the return value, indicating the processing result obtained after mean filtering.

         ● src is the image to be processed, that is, the original image. It can have any number of channels and can process each channel independently. Image depth should be one of CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.

        ● ksize is the size of the filter kernel. The filter kernel size refers to the height and width of its neighborhood image during mean processing.

        ● anchor is the anchor point, and its default value is (-1,-1), which means that the current mean value calculation point is located at the center point of the kernel. Use the default value for this value, and in special cases, you can specify a different point as the anchor point.

        ● borderType is the border style, this value determines how to deal with the border. In general, you do not need to consider the value of this value, and you can directly use the default value.                      

            Normally, when using the mean filter function, for the anchor point anchor and the border style borderType, you can directly use their default values. Therefore, the general form of the function cv2.blur() is:

        dst=cv2.blur(src,ksize)        

import cv2  as cv
import numpy as np

def cv_show(name, img):
    cv.imshow(name, img)
    cv.waitKey(0)
    cv.destroyAllWindows()

# 在图片上生成椒盐噪声
def add_peppersalt_noise(image, n=10000):
    result = image.copy()
    # 测量图片的长和宽
    w, h =image.shape[:2]
    # 生成n个椒盐噪声
    for i in range(n):
        x = np.random.randint(1, w)
        y=  np.random.randint(1, h)
        if np.random.randint(0, 2) == 0 :
            result[x, y] = 0
        else:
            result[x,y] = 255
    return result

# 在图片上生成高斯噪声
def add_gauss_noise(image, mean=0, val=0.01):
    size = image.shape
    image = image / 255
    gauss = np.random.normal(mean, val**0.05, size)
    image = image + gauss
    return image

# blur均值滤波,对高斯噪声有较好的去除效果,对象可以是彩色图像和灰度图像
img = cv.imread('D:\\dlam.jpg')
if img is None:
    print('Failed to read the image')

img1 = add_peppersalt_noise(img)
cv_show('img', img1)

# 默认为规定尺寸的1/n的全1矩阵
img2 = cv.blur(img1, (3, 3))
cv_show('img2', img2)

img3 = cv.blur(img1, (5, 5))
cv_show('img3', img3)

# 观察不同滤波核对图像滤波的影响

example:

        

 After adding salt and pepper noise:

        3×3 convolution kernel filtering effect

  5×5 convolution kernel filtering effect

         It is not difficult to see from the above example that the larger the convolution kernel, the more pixels will participate in the mean value operation, that is, the current point calculates the average value of the pixel values ​​of more points. Therefore, the larger the convolution kernel, the better the denoising effect, of course, the longer the calculation time will be, and the more serious the image distortion will be. In actual processing, it is necessary to strike a balance between distortion and denoising effects, and select a convolution kernel of an appropriate size.

Guess you like

Origin blog.csdn.net/qq_49478668/article/details/123217030