Image processing: Gaussian filter algorithm

Table of contents

foreword

concept introduction

Fundamental

The size of the convolution kernel

The shape and weight ratio of the convolution kernel

Normalization of convolution kernel

in conclusion

Opencv implements Gaussian filtering

Python handwriting to achieve Gaussian filtering

reference article


foreword

Before that, I have deduced image processing in this article: Deriving five filtering algorithms (mean, median, Gaussian, bilateral, bootstrap) . Based on this, I would like to study and derive these algorithms more deeply in order to lay the foundation for future projects on image processing.

concept introduction

Gaussian filtering is a commonly used image processing technique, often used in applications such as denoising, smoothing, and edge detection. It is based on the concept of the Gaussian function. Due to the nature of the Gaussian function, the farther away from the center pixel, the smaller the contribution to the new value. The weighted average processing is performed on the pixels in the image, so that the influence of the surrounding pixels is relatively large, and far away from the center. Pixels have less of an impact. Therefore, Gaussian filtering can smooth the image and remove part of the noise, while preserving the edges and details in the image.

Specifically, Gaussian filtering is implemented through a matrix (convolution kernel). The value of this matrix is ​​calculated by the Gaussian function, which has a maximum value at the center point and gradually decreases as the distance increases. The size of the matrix and the choice of parameters depend on the image to be processed and the effect to be achieved.

Fundamental

Here we take a 3x3 convolution kernel as an example. The value of the convolution kernel is calculated by the Gaussian function. The Gaussian function obtains the maximum value at the center point and gradually decreases as the distance increases. By applying a convolution kernel to each pixel, Gaussian filtering can smooth the image and remove noise while preserving the details and edges of the image.

As shown in the figure above, the Gaussian function has the largest value at the center point, and the value becomes smaller the farther away from the center point. This means that for each pixel, pixels farther from the center pixel have less influence on the new value. This mode of influence allows Gaussian filtering to remove image noise while preserving image details and edges.

Through the calculation process of Gaussian filtering, the result that the center point pixel value 226 is replaced by the new value 164 can be obtained. Specifically, the convolution kernel is multiplied by the pixel matrix in the image through a simple matrix dot product calculation to obtain a new value for each pixel.

Its calculation process is as follows:

a=40*1+107*2+5*1+198*2+226*8+223*2+37*1+68*2+193*1
b=a*(1/(1*4+2*4+8))
print(b)

The size of the convolution kernel

The previous example is a 3x3 convolution kernel as an example. Here, some knowledge about the convolution kernel will be supplemented in detail.

The shape and weight ratio of the convolution kernel

The shape of the convolution kernel is usually a matrix, and the length and width can vary, but must be odd. In Gaussian filtering, the size of the convolution kernel is usually 3x3, 5x5, 7x7, etc. Among them, the 3x3 convolution kernel is the most commonly used, and its central point has the largest weight, and the surrounding point weights gradually decrease.

\frac{1}{256}\begin{bmatrix} 1 & 4 & 6 & 4 &1 \\ 4 & 16 & 24 & 16 & 4\\ 6 & 24& 36 & 24 &6 \\ 4 & 16 & 24 &16 &4 \\ 1& 4 & 6 &4 &1 \end{bmatrix} \frac{1}{159}\begin{bmatrix} 2& 9& 5& 9 &2 \\ 4& 4& 12& 4 &4 \\ 5& 12& 15& 12 &5 \\ 4& 9& 12& 9& 4\\ 2& 4 & 5 & 4 &2 \end{bmatrix}

Convolution kernels of the same size can have different weight ratios, and these weight ratios will determine how much the convolution kernel pays attention to different areas of the image. In Gaussian filtering, the weight ratio of the convolution kernel is calculated according to the Gaussian function. A Gaussian function is a bell-shaped curve with the greatest weight at its center and gradually decreasing weights on either side.

Normalization of convolution kernel

In actual use, it is often necessary to normalize the convolution kernel to ensure the correctness of the filtering effect. In Gaussian filtering, all weight values ​​​​of the convolution kernel need to be scaled to the same range, and the sum is 1, so as to ensure that the influence of the convolution operation on the image is balanced, and avoid some weights that are too large or too small to cause filtering The results are biased.

in conclusion

Convolution kernel is a very important concept in image processing. It can realize filtering, edge detection and other functions through convolution operation. In Gaussian filtering, factors such as the size of the convolution kernel, weight ratio, and normalization all affect the filtering effect. Therefore, it is necessary to select an appropriate convolution kernel according to the actual situation, and make appropriate adjustments to the convolution kernel to meet different image processing requirements.

Opencv implements Gaussian filtering

def GaussianBlur(src, ksize, sigmaX, dst=None, sigmaY=None, borderType=None):

In Opencv, we can use the cv2.GaussianBlur() function to implement Gaussian filtering. When using this function, you need to input parameters such as the original image, the size of the convolution kernel, the standard deviation of the Gaussian kernel along the x-axis and y-axis, and the boundary style. Among them, sigmaY and borderType are optional parameters, and sigmaX is a mandatory parameter, but you can set this parameter to 0 and let the function calculate the specific value of sigmaX by itself.

import cv2
import numpy as np
import pyps.pyzjr.utility as zjr
path = 'Images/Colnoiselena.jpg'

img = cv2.imread(path)
imgGaussianBlur_1=cv2.GaussianBlur(img,(1,1),0,0)
imgGaussianBlur_3=cv2.GaussianBlur(img,(3,3),0,0)
imgGaussianBlur_5=cv2.GaussianBlur(img,(5,5),0,0)
imgGaussianBlur_7=cv2.GaussianBlur(img,(7,7),0,0)

imgStack = zjr.stackImages(0.6, ([imgGaussianBlur_1, imgGaussianBlur_3], [imgGaussianBlur_5, imgGaussianBlur_7]))
cv2.imshow("imges",imgStack)
cv2.waitKey(0)
cv2.destroyAllWindows()

Realize the effect: 

In general, the convolution kernel size and Gaussian kernel standard deviation need to be adjusted according to the actual scene. If a stronger blurring of the image is required, the sigma value and the size of the convolution kernel can be increased; if a weaker blurring of the image is required, the sigma value and the size of the convolution kernel can be decreased.

Python handwriting to achieve Gaussian filtering

import cv2
import numpy as np
import pyps.pyzjr.utility as zjr
path = 'Images/Colnoiselena.jpg'
img = cv2.imread(path)

def gaussian_kernel(size, sigma):
    kernel = np.zeros((size, size), dtype=np.float32)
    center = size // 2

    for i in range(size):
        for j in range(size):
            x, y = i - center, j - center
            kernel[i, j] = np.exp(-(x**2 + y**2)/(2*sigma**2))
    kernel /= 2 * np.pi * sigma**2
    kernel /= np.sum(kernel)
    return kernel

def Gaussian_Filtering(img, kernel_size, sigma):
    kernel = gaussian_kernel(kernel_size, sigma)
    height, width, channels = img.shape
    result = np.zeros_like(img, dtype=np.float32)

    pad_size = kernel_size // 2
    img_pad = np.pad(img, [(pad_size, pad_size), (pad_size, pad_size), (0, 0)], mode='constant')
    for c in range(channels):
        for i in range(pad_size, height + pad_size):
            for j in range(pad_size, width + pad_size):
                result[i - pad_size, j - pad_size, c] = np.sum(kernel * img_pad[i - pad_size:i + pad_size + 1, j - pad_size:j + pad_size + 1, c])
    return np.uint8(result)

imgGaussianBlur_1 = Gaussian_Filtering(img, 1, 1.5)
imgGaussianBlur_3 = Gaussian_Filtering(img, 3, 1.5)
imgGaussianBlur_5 = Gaussian_Filtering(img, 5, 1.5)
imgGaussianBlur_7 = Gaussian_Filtering(img, 7, 1.5)
imgStack = zjr.stackImages(0.6, ([imgGaussianBlur_1, imgGaussianBlur_3], [imgGaussianBlur_5, imgGaussianBlur_7]))
cv2.imshow("imges",imgStack)
cv2.waitKey(0)
cv2.destroyAllWindows()

It is roughly the same as the case of implementing mean filtering by handwriting, and the calculation speed is relatively slow, but here, from the effect point of view, there is not much difference.

The following is the specific implementation process of this function: 

  • Define a Gaussian kernel: first generate a Gaussian kernel through gaussian_kernel()the function , which generates a Gaussian kernel according to the input sizeand sigmaparameters , where sizerepresents the size of the Gaussian kernel and sigmathe standard deviation of the Gaussian kernel.

G(x,y)=\frac{1}{2\pi \sigma^{2}}e^{-\frac{x^{2}+y^{2}}{2\sigma ^{2}}}

  • Zero padding to the image: Since Gaussian filtering is to apply a Gaussian kernel around the pixels of the image, it is necessary to pad the image with zeros so that there will be no out-of-bounds when performing Gaussian convolution on the edge area of ​​the image. np.pad()The function is used here to realize the zero padding operation on the image, in which only the two dimensions of height and width are filled, and the channel dimension is not filled.

  • Realize Gaussian convolution: For each pixel, convolve the area centered on it with the Gaussian kernel. Here, a nested triple loop is used to traverse the image, where crepresents .ij

  • Store convolution result in result matrix: Save the result of convolution in a result matrix, which has the same size as the original image. Finally convert that resulting matrix to uint8 format and return it.

It should be noted that when performing Gaussian filtering on an image, each channel should be processed separately, so a channel-specific loop needs to be added to the Gaussian convolution loop.

reference article

(13 messages) Image processing: Derivation of five filtering algorithms (mean, median, Gaussian, bilateral, guided)_Gaussian filter, mean filter, median filter_Summer is the blog of ice tea-CSDN blog

(19 messages) Gauss filtering (Gauss filtering)_Half Hao Chunshui's Blog-CSDN Blog

(6 messages) Gaussian filtering and its principle_Shengxi Censh's Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/m0_62919535/article/details/130352118