The principle of image convolution filtering and the implementation of Python programs for mean filtering, box filtering, Gaussian filtering, median filtering, and bilateral filtering

The basic principle of image convolution filtering:

A two-dimensional filter matrix (that is, a convolution kernel) and a two-dimensional image to be processed; for each pixel of the image, calculate the product of its neighboring pixels and the corresponding elements of the filter matrix, and then add them up , as the value of the pixel position, thus completing the filtering process.

Enter a picture description

It is not difficult to find that a 6×6 image is convolved with a 3×3 kernel, and a 4×4 image is obtained, and the image is shrunk! then what should we do? We can expand the original image by a circle and then convolve it. This operation is called padding.

In fact, the original image is n×n, the convolution kernel is f×f, and the final result image size is (n-f+1) × (n-f+1)

Enter a picture description

cv2.copyMakeBorder()Used to add a border to the picture, it has the following parameters:

  • src: the original image to be processed
  • top, bottom, left, right: the number of pixels to be expanded up, down, left, and right
  • borderType: border type, this is the filling method that needs attention, please refer to: BorderTypes for details

Commonly used filling types include: fixed value filling, pixel filling with mirror symmetry, etc.

Common Image Smoothing and Filtering Algorithms

  • Mean filter, box filter
  • Gaussian filter
  • median filter
  • bilateral filtering

1. Mean filter, box filter

Mean filtering is a typical linear filtering algorithm, which mainly uses the pixel value of the pixel neighborhood to calculate the value of the pixel. The specific method is to first give a filter template kernel, which will cover other neighboring pixels around the pixel, remove the pixel itself, add its neighboring pixels and then take the average value to get the new value of the pixel. Pixel value, this is the essence of mean filtering.

function:
cv2.blur(InputArray src, OutputArray dst, Size ksize, Point anchor=Point(-1,-1), int borderType=BORDER_DEFAULT )

Parameter explanation:

  • InputArray src: Input image, which can be of Mat type, and the image depth is one of CV_8U, CV_16U, CV_16S, CV_32F, and CV_64F.
  • OutputArray dst: output image, the same depth and type as the input image
  • Size ksize: The size of the filter template kernel, generally specified by Size(w, h), such as Size(3,3)
  • Point anchor=Point(-1, -1): Literally means the anchor point, that is, where the processed pixel is located in the kernel. The default value is (-1, -1), which is located in the center of the kernel. If there is no special need, then no change required
  • int borderType=BORDER_DEFAULT: A border mode used to infer pixels outside the image, with a default value of BORDER_DEFAULT

Advantages: easy to use, easy to calculate

Features: The contribution rate of the core area is the same

2. Gaussian filter

Gaussian filtering is a weighted average process of the entire image, and the value of each pixel is obtained by weighted average of itself and other pixel values ​​in the neighborhood.

function:
cv2.GaussianBlur(InputArray src, OutputArray dst, Size ksize, double sigmaX, double sigmaY=0, int borderType=BORDER_DEFAULT )

Parameter explanation:

  • InputArray src: input image, which can be of Mat type, and the image depth is CV_8U, CV_16U, CV_16S, CV_32F, CV_64F.
  • OutputArray dst: The output image, which has the same type and size as the input image.
  • Size ksize: Gaussian kernel size, this size is different from the previous two filter kernel sizes, ksize.width and ksize.height can be different but these two values ​​must be positive odd numbers, if these two values ​​are 0, their values ​​will be determined by sigma calculation.
  • double sigmaX: The standard deviation of the Gaussian kernel function in the X direction
  • double sigmaY: The standard deviation of the Gaussian kernel function in the Y direction. If sigmaY is 0, the function will automatically set the value of sigmaY to the same value as sigmaX. If both sigmaX and sigmaY are 0, these two values ​​will be determined by ksize. Width and ksize.height are calculated. For details, please refer to the getGaussianKernel() function for details. It is recommended to specify size, sigmaX and sigmaY.
  • int borderType=BORDER_DEFAULT: A convenient mode for inferring the pixels outside the image. There is a default value of BORDER_DEFAULT. If there is no special need, do not change it. For details, please refer to the borderInterpolate() function.

Advantages: Very effective for noise that obeys a normal distribution

Features: The contribution rate of the area in the nucleus is proportional to the distance from the center, and the weight is related to the Gaussian distribution

3. Median filtering

The gray value of the pixel is replaced by the median value of the gray value of the neighborhood of the pixel, so that the surrounding pixel values ​​​​are close to the real value, thereby eliminating isolated noise points. This method can preserve the edge details of the image while removing the impulse noise and salt and pepper noise.

function:
cv2.medianBlur(InputArray src, OutputArray dst, int ksize)

Parameter explanation:

  • InputArray src: input image, the image is an image of 1, 3, or 4 channels. When the template size is 3 or 5, the image depth can only be one of CV_8U, CV_16U, and CV_32F. For example, for images with larger aperture sizes, Image depth can only be CV_8U.
  • OutputArray dst: The output image, the size and type are consistent with the input image, you can use Mat::Clone to initialize the output image dst with the original image as a template
  • int ksize: The size of the filter template, which must be an odd number greater than 1, such as 3, 5, 7...

Advantages: effective for salt and pepper noise

Features: The pixel value of the center point is replaced by the median pixel in the kernel

4. Bilateral filtering

Combining the spatial proximity of the image and the similarity of pixel values, it is a compromise processing, while considering the similarity of space, information and gray level, to achieve the purpose of edge preservation and denoising. It has the characteristics of simple, non-iterative, and local processing.

function:

cv2.bilateralFilter(InputArray src, OutputArray dst, int d, double sigmaColor, double sigmaSpace, int borderType=BORDER_DEFAULT)

Parameter explanation:

  • InputArray src: Input image, which can be of Mat type, and the image must be an 8-bit or floating-point single-channel or three-channel image.
  • OutputArray dst: The output image, which has the same size and type as the original image.
  • int d: Indicates the diameter range of each pixel neighborhood during filtering. If this value is non-positive, the function calculates the value from the fifth parameter sigmaSpace.
  • double sigmaColor: The sigma value of the color space filter. The larger the value of this parameter, the larger the value, indicating that the wider colors in the pixel neighborhood will be mixed together, resulting in a larger semi-equal color area.
  • double sigmaSpace: The sigma value of the filter in the coordinate space, if the value is larger, it means that distant pixels with similar colors will affect each other, so that sufficiently similar colors in a larger area get the same color. When d>0, d specifies the size of the neighborhood and is related to sigmaSpace, otherwise d is proportional to sigmaSpace.
  • int borderType=BORDER_DEFAULT: A border mode used to infer pixels outside the image, with a default value of BORDER_DEFAULT.

Function: It can well preserve the edge details of the image and filter out the noise of low frequency components

Features: The efficiency of the bilateral filter is not too high, and the time spent is relatively long compared with other filters

The program implementation of the above algorithm is as follows:

# -*- coding:utf-8 -*-
#本程序用于实现各种滤波算法
import cv2  #导入opencv模块
import numpy as np

print("Hellow word!")     #打印“Hello word!”,验证模块导入成功

img = cv2.imread("imag2.jpg")  #导入图片,图片放在程序所在目录
cv2.namedWindow("imagshow", 2)   #创建一个窗口
cv2.imshow('imagshow', img)    #显示原始图片

# 均值滤波
img_mean = cv2.blur(img, (3,3)) #参数1输入图像,参数2核大小
cv2.namedWindow("mean", 2)   #创建一个窗口
cv2.imshow('mean', img_mean)    #显示原始图片

# 高斯滤波
img_Guassian = cv2.GaussianBlur(img,(3,3),0)
cv2.namedWindow("Guassian", 2)   #创建一个窗口
cv2.imshow('Guassian', img_Guassian)    #显示原始图片

# 中值滤波
img_median = cv2.medianBlur(img, 5)
cv2.namedWindow("median", 2)   #创建一个窗口
cv2.imshow('median', img_median)    #显示原始图片

# 双边滤波
img_bilater = cv2.bilateralFilter(img,9,75,75)
cv2.namedWindow("bilater", 2)   #创建一个窗口
cv2.imshow('bilater', img_bilater)    #显示原始图片

cv2.waitKey()

Effect picture:
Enter a picture description
download the complete code, please go to: opencv common filter algorithm python language implementation

From left to right are: original image, median filter, bilateral filter, mean filter, Gaussian filter.

Guess you like

Origin blog.csdn.net/u014005758/article/details/88238941