1 image noise
noise generation: some pixels in the middle of the image suddenly change, resulting in disharmony with the surrounding pixels
denoising: replace disharmonious points with harmonious points. Homogenize the change of gray value.
denoising method:
(1) convolution based methods: Gaussian filtering, mean filtering
(2) operations based on morphology: open and close operations
1.1 salt and pepper noise
salt and pepper noise: also known as impulse noise, it is a common noise in image processing. It is a random white point or black point.
1.2 Gaussian noise
Gaussian noise: refers to that the (gray value) density function of noise obeys Gaussian distribution.
2 image denoising / filtering / smoothing
2.1 Gaussian filtering
Gaussian kernel:
the closer to the center, the greater the value, and the farther away from the center, the smaller the value. When calculating the Gaussian smoothing result, you only need to take the center point as the origin and assign weights to other points according to their positions on the normal distribution curve to obtain a weighted average.
function of Gaussian filter:
(1) Gaussian filter smoothes or blurs the image, and the intensity of blurring is determined by the standard variance of Gaussian σ The larger the standard variance, the greater the smoothing of the image, the smaller the variance, and the smaller the blur of the image.
(2) Gaussian filtering is an average operation, which averages the gray value of the pixel covered by the Gaussian kernel and gives more weight to the middle point. Make the middle points have more weight and the surrounding points have lower weight. It can well retain the attributes of the original image.
API: cv2.GaussianBlur(src,ksize,sigmax,sigmaY=None, borderType=None)
parameters:
src: input image
ksize: the size of Gaussian convolution kernel. Note that the width and height of convolution kernel should be odd and can be different
sigmax: standard deviation in horizontal direction
Sigma: the standard deviation in the vertical direction. The default is 0, which means it is the same as sigmax
borderType: fill boundary type
import cv2 import matplotlib.pyplot as plt import numpy as np #1. Read image img1=cv2.imread('blur_sp.png')#Salt and pepper noise image img2=cv2.imread('blur_gs.png')#Gaussian noise image #2. Gaussian filtering img_blur1=cv2.GaussianBlur(img2,(3,3),sigmaX=1) #3. Display image cv2.imshow('origin',img2) cv2.imshow('blur_jiaoyan',img_blur1) cv2.waitKey(0)
2.2 mean filtering
mean filter core:
each pixel covered by the mean filtering kernel may obtain the same weight. The mean filter template is used to remove the noise in the image - the average value of all pixels in the convolution frame coverage area is used to replace the central element. This averaging operation can not well maintain the original attributes of the image and make the image extremely blurred.
API: cv2.blur(src,ksize,anchor=None, borderType=None)
parameters:
src: input image
ksize: convolution kernel size
anchor: the default value (- 1, - 1) indicates the core center
borderType: boundary type
import cv2 import matplotlib.pyplot as plt import numpy as np #1. Read image img1=cv2.imread('blur_sp.png')#Salt and pepper noise image img2=cv2.imread('blur_gs.png')#Gaussian noise image #2. Mean filtering img_blur1=cv2.blur(img1,(5,5)) #3. Display image cv2.imshow('origin',img1) cv2.imshow('blur_jiaoyan',img_blur1) cv2.waitKey(0)
2.3 median filtering
median filter core: sort the gray values of pixels covered by the core, and take the median as the final result. Median filtering is a typical nonlinear filtering technology. The basic idea is to replace the gray value of the pixel with the median of the gray value in the neighborhood of the pixel.
emsp; features: it is particularly useful for salt and pepper noise because it does not rely on values with large differences in typical values in the neighborhood
API: cv2.medianBlur(src,ksize)
src: input image
ksize: convolution kernel size
import cv2 import matplotlib.pyplot as plt import numpy as np #1. Read image img1=cv2.imread('blur_sp.png')#Salt and pepper noise image img2=cv2.imread('blur_gs.png')#Gaussian noise image #2. Mean filtering img_blur1=cv2.medianBlur(img1,3) #3. Display image cv2.imshow('origin',img1) cv2.imshow('blur_jiaoyan',img_blur1) cv2.waitKey(0)
the effect of median filter is not as good as Gaussian filter and kernel mean filter, but the operation speed is fast.