This article has participated in the activity of “New person creation Ceremony”, and started the road of digging gold creation together.

Three linear filters: box filter, mean filter, gaussian filter

Gaussian filtering:

Definition 1.

Image filtering, that is, the noise is suppressed under the condition of preserving the details of the image as far as possible, and the noise is reduced by suppressing the high frequency band, which will cause the image to a certain extentThe fuzzyThis is also called a smooth or low-pass filter; In the process of smoothing, pixels at different positions in the neighborhood are given different weights. While smoothing the image, the overall gray distribution characteristics of the image can be retained more. There are two main parameters for gaussian kernel:The size of gaussian kernel and the degree of dispersion σ.Can know: In order to be as smooth as possible, the Gaussian kernel should be as large as possible, but for values beyond σ 3, the effect is not very great. Therefore, for values less than σ 3, the size of the gaussian kernel is usually 2*k+1, which is odd. In this case, the center point is (k+1, k+1). As you can see, since it is not infinitely large, the sum should be slightly less than 1. So sometimes, instead of dividing by 2xpix sigma, we calculate the sum separately and add them together as the denominator, which gives us a sum of 1. Note: k+1 is the default subscript starting with 1.

2. Gaussian filtering manual implementation code:

# -*- coding: utf-8 -*-
""" Created on Sat Jul 11 14:53:28 2020 @author: Meteorite Fall ""
import imageio
import numpy as np

def GaussianFilter(img) :
    
    h,w,c = img.shape
    # Gaussian filtering
    K_size = 3
    sigma = 1
    
    # zero padding
    pad = K_size//2
    out = np.zeros((h + 2*pad,w + 2*pad,c),dtype=np.float)
    out[pad:pad+h,pad:pad+w] = img.copy().astype(np.float)
    
    # define the filter kernel
    K = np.zeros((K_size,K_size),dtype=np.float)
    
    for x in range(-pad,-pad+K_size):
        for y in range(-pad,-pad+K_size):
            K[y+pad,x+pad] = np.exp(-(x**2+y**2)/(2*(sigma**2)))
    K /= (sigma*np.sqrt(2*np.pi))
    K /=  K.sum(a)# Process of convolution
    tmp = out.copy()
    for y in range(h):
        for x in range(w):
            for ci in range(c):
                out[pad+y,pad+x,ci] = np.sum(K*tmp[y:y+K_size,x:x+K_size,ci])
    
    out = out[pad:pad+h,pad:pad+w].astype(np.uint8)
    
    return out

if __name__ == "__main__":
    
    # fetch image
    img = imageio.imread("lena.jpg")
    # Gaussian filtering
    imageio.imsave("GaussianFilter.jpg",GaussianFilter(img))
Copy the code

3. Impact of window size and standard deviation

Blog. Shinelee. Me / 2018/09-19 -…

Mean filtering:

The mean filter is a linear filter in the spatial domain, in which each pixel in the resulting image has a value equal to the average of its adjacent pixels in the input image. It is a form of a low-pass (” fuzzy “) filter.

Box filtering:

Kernel used for box filtering:When normalize is true, the box filter becomes the mean filter. In other words, mean filtering is a special case after the normalization of box filtering.

Normalization is scaling the amount you’re dealing with to a range, such as (0,1).

Note: It is usually mean filtering!! I feel

Two kinds of nonlinear filtering: median filtering and bilateral filtering

Median filtering

The basic idea of median filtering is to use pixel neighborhood gray valueThe medianTo replace the gray value of the pixel, this method can remove the pulse noise, salt and pepper noise and retain the details of the image. Median filtering takes longer time than mean filtering, but it is in the ability to eliminate noisestronger. Note: Just look at the picture.

Bilateral filtering

Bilateral filtering is a kind of compromise processing combining spatial proximity and pixel value similarity of image, considering spatial information and gray level similarity at the same time, so as to preserve edge and remove noise.Note: tentative !!!!!!!!!!!

Sobel operator

The idea of Sobel operator. Sobel operator believes that the influence of pixels in the neighborhood on the current pixel is not equivalent, so pixels with different distances have different weights and have different influences on the operator results. In general, the greater the distance, the smaller the impact. The principle of Sobel operator is to convolve the incoming image pixels, and the essence of convolution is to find the gradient value, or to give a weighted average, where the weight is the so-called convolution kernel; Then the edge information is determined by threshold operation on the gray value of the generated new pixel. If Gx is the convolution of the original graph in the x direction, and Gy is the convolution of the original graph in the y direction;After convolution, the pixel values of the active points in the original figure are:It can be simplified as:In addition, the convolution kernel can also be rotated, using and to find edges in directions that are not parallel or perpendicular to the x and y axes.

Reference link: blog.csdn.net/qq_37534947…