Simple understanding of common filtering (Gaussian filtering, mean filtering, etc.)

This article has participated in the "Newcomer Creation Ceremony" event to start the road of gold creation together.

3 kinds of linear filter: box filter, mean filter, Gaussian filter

Gaussian filter:

1. Definition

Image filtering, that is, suppressing noise under the condition of preserving the details of the image as much as possible, reducing noise by suppressing high frequency bands, and causing image blur to a certain extent , which is also called smoothing or low-pass filter; when smoothing, Pixels at different positions in the neighborhood are given different weights, which can preserve the overall gray level distribution characteristics of the image while smoothing the image. insert image description here insert image description hereThere are two main parameters for the Gaussian kernel: the size of the Gaussian kernel and the degree of dispersion σ. It can be known that: in order to be as smooth as possible, the Gaussian kernel should be as large as possible, but for values ​​other than 3σ, its influence is not very large, so generally the value of its edge is less than 3σ, and its size is generally 2*k+ 1, is an odd number. At this time, the center point is (k+1, k+1), and then substituted into the above formula to obtain the value of each position. It can be seen that because it is not infinite, the sum should be slightly less than 1 , so sometimes, when calculating, it is not divided by 2xpixσ, but obtained separately, and then added as the denominator, so that the sum is 1, the specific situation is analyzed in detail, the effect is similar! Note: k+1 is the default subscript starting from 1.

insert image description here

2. Gaussian filter manual implementation code:

# -*- coding: utf-8 -*-
"""
Created on Sat Jul 11 14:53:28 2020

@author: 陨星落云
"""
import imageio
import numpy as np

def GaussianFilter(img):
    
    h,w,c = img.shape
    # 高斯滤波 
    K_size = 3
    sigma = 1
    
    # 零填充
    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)
    
    # 定义滤波核
    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()
    
    # 卷积的过程
    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__":
    
    # 读取图像
    img = imageio.imread("lena.jpg")
    # 高斯滤波
    imageio.imsave("GaussianFilter.jpg",GaussianFilter(img))
复制代码

3. The effect of window size and standard deviation

blog.shinelee.me/2018/09-19-…

Mean filter:

​Mean filtering is a spatial domain linear filter in which each pixel in the resulting image has a value equal to the average in the input image of its neighbors. It is a form of low-pass ("blur") filter.insert image description here

Box filtering:

The kernel used in box filtering: insert image description hereWhen normalize is true, box filtering becomes mean filtering. That is to say, the mean filter is a special case of the normalized block filter.

Normalization is to scale the amount to be processed to a certain range, such as (0, 1).

(Function analysis is provided uniformly in the following text) Note: Generally, it is mean filtering! ! ! I feel

2 kinds of nonlinear filters: median filter, bilateral filter

median filter

The basic idea of ​​median filtering is to replace the gray value of the pixel with the median value of the gray value of the neighborhood of the pixel. This method can retain the details of the image while removing impulse noise and salt and pepper noise. The median filter takes longer than the mean filter, but its ability to remove noise is stronger . Note: Just look at the picture to understand.insert image description here

bilateral filtering

Bilateral filtering is a compromise processing that combines the spatial proximity of the image and the similarity of pixel values, and considers the spatial information and grayscale similarity to achieve the purpose of preserving edges and removing noise. insert image description hereNote: Tentative! ! ! ! ! ! ! ! ! ! !

Sobel operator

The idea of ​​the sobel operator, the Sobel operator believes that the influence of the pixels in the neighborhood on the current pixel is not equivalent, so the pixels with different distances have different weights and have different effects on the operator results. In general, the greater the distance, the smaller the impact. The principle of the sobel operator is to convolve the incoming image pixels. The essence of the convolution is to calculate the gradient value, or to give a weighted average, where the weight is the so-called convolution kernel; The gray value is subjected to a threshold operation to determine the edge information. If Gx is the convolution on the x-direction of the original image, Gy is the convolution on the y-direction of the insert image description hereoriginal image; the pixel value of the action point in the original image after convolution is: insert image description hereIt can be simplified to: insert image description hereIn addition, the convolution kernel can also be Rotate, use and to find edges in directions that are not parallel or perpendicular to the x,y axis.insert image description here insert image description here

Reference link: blog.csdn.net/qq_37534947…

Guess you like

Origin juejin.im/post/7078585077768126471