First entry to SLAM (5) - Gaussian blur in SIFT

1. Go Gaussian Blur

Gaussian blur is an image filter that uses a normal distribution (Gaussian function) to calculate the template value, and uses the template to perform convolution operations with the original image to achieve the purpose of blurring the image.

The normal distribution of N-dimensional space is shown in formula 1-1 G ( r ) = 1 2 π σ 2 e − r 2 ( 2 σ 2 ) ( 1 − 1 ) G(r)=\frac{1}{\ sqrt{2 \pi \sigma^{2}} e^{-r^{2}\left(2 \sigma^{2}\right)}} (1-1)G(r)=2 p.s _2 er2 (2p2)111 )
Among them,σ \sigmaσ is the standard deviation of the normal distribution,σ \sigmaThe larger the σ value, the blurrier (smoother) the image after Gaussian blurring. r is the blur radius, and the blur radius refers to the distance from the template element to the center of the template.

The following uses a two-dimensional template as an example, assuming that the size of the two-dimensional template is m ∗ nm*nmn , then the element ( x , y )on the template element(x,y ) corresponding Gaussian calculation formula is:
G ( x , y ) = 1 2 π σ 2 e − ( x − m / 2 ) 2 + ( y − n / 2 ) 2 2 σ 2 ( 1 − 2 ) G( x, y)=\frac{1}{2 \pi \sigma^{2}} e^{-\frac{(xm / 2)^{2}+(yn / 2)^{2}}{2 \sigma^{2}}} (1-2)G(x,y)=2 p.s _21e2 p2(xm/2)2 +(yn/2)212 )
In 2D space, the contours of the surface generated by this formula are concentric circles distributed normally from the center. The convolution matrix composed of pixels whose distribution is not zero is transformed with the original image. The value of each pixel is a weighted average of the values ​​of surrounding neighboring pixels. The value of the original pixel has the largest Gaussian distribution value, so it has the largest weight, and the weight of adjacent pixels becomes smaller and smaller as the distance from the original pixel is farther away. Blurring this way preserves edge effects much better than other equalized blur filters.

Theoretically speaking, the distribution of each point in the image is not zero, which means that the calculation of each pixel needs to include the entire image. In practical applications, when calculating the discrete approximation of the Gaussian function, the pixels outside the approximate 3σ distance can be regarded as ineffective, and the calculation of these pixels can be ignored. Usually, the image processing program only needs to calculate the matrix of (6σ+1)*(6σ+1) to ensure the relative pixel influence.

2. Python learning code

import cv2
import numpy as np


def GaussianSmooth2D(image: np.ndarray, sigma: float):
	'''
	此程序用于高斯平滑灰度图像,图像不能是3维灰度图,只能是2维的。此外,本程序实现自动全零填充,输出维度跟输入维度一样。
	image:二维图像,cv2的灰度读入
	sigma:高斯核的标准差
	'''
    # 确保sigma为正数
    sigma = sigma if sigma>0 else 0
    # 高斯核矩阵大小为(6*sigma+1)*(6*sigma+1)
    ksize = round(sigma * 3) * 2 + 1

    if (ksize == 1):
        return image

    kernel = np.zeros((ksize, ksize))

    scale = -0.5/(sigma * sigma)
    cons = -scale / np.pi
    sum = 0
    for i in range(ksize):
        for j in range(ksize):
            x = i - (ksize - 1) / 2
            y = j - (ksize - 1) / 2
            temp = cons * np.exp(scale * (x*x + y*y))
            kernel[i, j] = temp
            sum += temp

    # 归一化
    kernel = kernel / sum


    p = [ksize//2, ksize//2]


    dst = np.pad(image, p, 'constant')

    guassian_img = np.empty((image.shape))

    for i in range(p[0], image.shape[0] + p[0]):
        for j in range(p[1], image.shape[1] + p[1]):
            guassian_img[i-p[0], j-p[1]] = np.sum(kernel * dst[i-p[0]:i+p[0]+1,j-p[1]:j+p[1]+1])

    return guassian_img.astype(np.uint8)

3. Summary

The code written by yourself is definitely slower than adjusting the official library, but sometimes you still need to understand the principles, and it is not impossible to reinvent the wheel.

Guess you like

Origin blog.csdn.net/REstrat/article/details/127047803