Algorithm optimization learning: (2) The introduction of two-dimensional Gaussian filtering

1. Gaussian distribution

1.1 One-dimensional Gaussian distribution

Gaussian distribution, also known as normal distribution, is a widely used probability distribution. One-dimensional Gaussian distribution is relatively common. The relevant mathematical definitions are as follows.

For different means and standard deviations, the one-dimensional Gaussian distribution curve is as follows. It can be seen that the larger the standard deviation, the flatter the curve and the more even the distribution; the smaller the standard deviation, the steeper the curve and the more uneven the distribution.

1.2 Two-dimensional Gaussian distribution

Images are generally processed as two-dimensional data, and two-dimensional Gaussian distributions are used accordingly. The mathematical definition and distribution curve of the two-dimensional Gaussian distribution are shown in the figure below.

                   

The two-dimensional Gaussian distribution has a very important property, that is, G(x,y) =G(x)*G(y), which is very important for the optimization of the two-dimensional Gaussian filter later.

2. Gaussian filtering in the image

Due to various reasons, there is always noise in the image. If the image is described in the form of a grayscale image, the grayscale value of the noise is often different from the grayscale value of its neighborhood (but the edge pixels also have this feature). Noise provides a simple idea: since the noise gray value is relatively "prominent", use the gray information of the neighborhood to balance. Therefore, common denoising algorithms include median filtering (taking the median value of a pixel neighborhood as the gray value of the pixel), mean filtering (corresponding to the average value of the pixel neighborhood), and Gaussian filtering. Gaussian filtering simply uses the Gaussian distribution. Intuitively, the closer the distance to the pixel point in the neighborhood, the higher the weighted value, and vice versa.

For two-dimensional Gaussian filtering, an important parameter is the standard deviation, and the influence of the standard deviation on the Gaussian distribution has been mentioned above. Taking the two-dimensional Gaussian filter of 3X3 size as an example, for a certain pixel point, the 3X3 neighborhood centered on it is shown in the figure below, and the values ​​in the x and y directions are as follows, which can be brought into G(x,y) Get the weight corresponding to the Gaussian distribution. However, the sum of these weights is not equal to 1 (less than 1), and they are generally normalized.

If the radius of the Gaussian filter is r, the size of the neighborhood is generally 2*r+1. The sample code for generating the weighting coefficient of the Gaussian filter is as follows.

void generateGaussianTemplate(double window[][11], int r, double sigma)
{
    static const double pi = 3.1415926;
    int center = r; // 模板的中心位置,也就是坐标的原点
    int ksize = 2*r + 1;// 邻域边长
    double x2, y2;
    double sum = 0;
    for (int i = 0; i < ksize; i++)
    {
        x2 = pow(i - center, 2);
        for (int j = 0; j < ksize; j++)
        {
            y2 = pow(j - center, 2);
            double g = exp(-(x2 + y2) / (2 * sigma * sigma));
            g /= 2 * pi * sigma;
            sum += g;
            window[i][j] = g;
        }
    }
    
    for (int i = 0; i < ksize; i++)
    {
        for (int j = 0; j < ksize; j++)
        {
            window[i][j] /= sum;
        }
    }
}

After that, the weight template is used to perform weighted summation of the pixel neighborhood. For a grayscale image of M*N size, if the radius of the two-dimensional Gaussian filter template is r, then (2*r+1)*(2*r+1)*M*N multiplications and ((2* r+1)*(2*r+1)-1)*M*N additions.

3. Simplification of two-dimensional Gaussian filtering

Using the mathematical properties of the two-dimensional Gaussian filter can reduce the amount of calculation of the algorithm, and the related derivation process is as follows.

In doing so, the computational complexity is reduced from the algorithm level.

For Gaussian filtering with a fixed template size, you can 1) calculate the template value in advance and save it, eliminating the need to recalculate the template value every time 2) convert the floating-point multiplication between the template value and the neighboring pixels into fixed-point multiplication and division, and try as much as possible Use shifts instead of divisions (with some loss of precision)

In this way, some optimizations have been completed at the algorithm level, and later we will try to optimize the code using multi-threading and NEON instruction set

Guess you like

Origin blog.csdn.net/lwx309025167/article/details/82761474