Image filtering of Opencv: 3. Box filtering (cv2.boxFilter)

        

        OpenCV also provides a box filtering method, which is different from the mean filter in that the box filter does not calculate the pixel mean. In mean filtering, the pixel value of the filtering result is the average value of the neighborhood of any point, which is equal to the sum of the pixel values ​​of each neighborhood divided by the area of ​​the neighborhood. In box filtering, you can freely choose whether to normalize the mean filtering result, that is, you can freely choose whether the filtering result is the average of the sum of neighboring pixel values ​​or the sum of neighboring pixel values.

3.1 Principle introduction

        Taking a 5×5 neighborhood as an example, when performing box filtering, if the mean value of the neighborhood pixel values ​​is calculated, the convolution kernel used is:

        

        If the calculation is the sum of neighboring pixel values, the convolution kernel used is:

3.2 Function Syntax

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

        dst=cv2.boxFilter(src,ddepth,ksize,anchor,normalize,borderType)

        In the formula:

        ● dst is the return value, indicating the processing result obtained after box 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.

         ● ddepth is the image depth of the processed image, generally use -1 to indicate the same image depth as the original image.

         ● ksize is the size of the filter kernel. The filter kernel size refers to the height and width of the neighborhood image selected during the filtering process.

        ● 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.

        ● normalize indicates whether to perform normalization (here refers to normalizing the calculation result to a value within the current pixel value range) during filtering. This parameter is a logical value, which may be true (value 1) or false (value 0) ):

                1. When the parameter normalize=1, it means that the normalization process is performed, and the sum of the neighboring pixel values ​​​​is divided by the area. At this time, the box filter has the same effect as the mean filter.

                2. When the parameter normalize=0, it means that normalization processing is not required, and the sum of neighboring pixel values ​​is directly used. When normalize=0, because no normalization processing is performed, the value obtained by filtering is likely to exceed the maximum value of the current pixel value range, thus being truncated to the maximum value. In this way, a pure white image will be obtained.

        ● borderType is the border style, this value determines how to deal with the border.

        Usually, when using the box filter function, for the parameters anchor, normalize and borderType, you can directly use their default values. Therefore, the usual form of the function cv2.boxFilter() is:

        dst=cv2.boxFilter(src,ddepth,ksize)

3.3 Program Example

        

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
 
img = cv.imread('D:\\dlam.jpg')
if img is None:
    print('Failed to read the image')
 
img1 = add_gauss_noise(img)
cv_show('img', img1)
 
# 默认为规定尺寸的1/n的全1矩阵
img2 = cv.blur(img1, (3, 3))
cv_show('img2', img2)
 

# boxfilter方框滤波效果和均值滤波效果一致,默认情况下,对方框滤波后的像素值进行归一化
img3 = cv.boxFilter(img1, -1, (3, 3))
cv_show('img3', img3)

img4 = cv.boxFilter(img1, -1, (3, 3), normalize=0)
cv_show('img4', img4)

 The original picture is as follows:

After adding salt and pepper noise: 

 Mean filtering: 3*3 convolution kernel 

Box filtering: 3*3 convolution kernel 

 

Box filtering: 3*3 convolution kernel, no normalization operation

Guess you like

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