[python + opencv image processing 2] image filtering (denoising, smoothing, blurring, etc.)

Smoothing, also known as blurring, is a simple and frequently used image processing method. Smoothing can be used in many ways, but in this tutorial we will only focus on its ability to reduce noise. From the perspective of filtering, the general main purpose is to achieve the elimination of image noise and enhance the effect of the image.

mean filtering

The simplest filter, the output pixel value is the mean value of the pixel values ​​​​in the kernel window

Mean blur function blur()
blur(src, ksize, dst=None, anchor=None, borderType=None).
The definition is that there are 5 parameters, but the last three are none
src: the original image to be processed, ksize: the range of surrounding associated pixels.

code:

def blur_demo(image):   # 均值模糊
    dst = cv.blur(image,(3,3))  # ksize是卷积核大小3行3列,列数越大模糊越大
    cv.imshow("blur_demo",dst)

median filter

The median filtering template is to replace the central value with the median value of the pixels in the convolution frame to achieve the purpose of denoising. This template is generally used to remove salt and pepper noise. Median filtering is very good for the removal of these white point noises.

The previous filters all replace the value of the center pixel with a new value calculated, while the median filter replaces it with the value around the center pixel (or itself), and the size of the convolution kernel is also an odd number .

Median blur function medianBlur():
medianBlur(src, ksize, dst=None).
ksize is different from the blur() function, not a matrix, but a number, for example, 5, which means a 5*5 square matrix

code:

def median_blur_demo(image):   # 中值模糊,对于椒盐噪声的去噪效果好,去掉图片中的一些黑点等
    dst = cv.medianBlur(image,5)  # ksize是卷积核大小1行3列,列数越大模糊越大
    cv.imshow("median_blur_demo",dst)

Gaussian filter

insert image description hereinsert image description hereinsert image description hereinsert image description hereinsert image description here
code:

def Gaussian_demo(image):
    dst = cv.GaussianBlur(image, (5, 5), 15)
    cv.imshow("GaussianBlur", dst)

box filtering

insert image description here
insert image description here
insert image description here
insert image description here

def box_demo(image):
    dst = cv.boxFilter(image,-1,(5,5),normalize=1)
    cv.imshow("box",dst)

bilateral filtering

Definition: bilateralFilter(src, d, sigmaColor, sigmaSpace, dst=None, borderType=None)
d: neighborhood diameter
sigmaColor: color standard deviation
sigmaSpace: spatial standard deviation

def shuangbian(image):
    src2 = cv.bilateralFilter(image, 25, 50, 25 / 2)
    cv.imshow("shuangbian", src2)

Salt and pepper noise result 1:

Original image (after adding salt and pepper noise)
(For noise addition, please refer to https://blog.csdn.net/poppyty/article/details/118410338?spm=1001.2014.3001.5501 )
insert image description here
After mean filtering:
insert image description here
After median filtering:
insert image description here
After Gaussian filtering :
insert image description here

After box filtering:
insert image description here
bilateral filtering:
insert image description here

Gaussian noise result 2:

Original image (after adding Gaussian noise):
(For noise addition, please refer to https://blog.csdn.net/poppyty/article/details/118410338?spm=1001.2014.3001.5501
insert image description here

After mean filtering:
insert image description here

After median filtering:
insert image description here

After Gaussian filtering:
insert image description here

After box filtering:
insert image description here

After bilateral filtering:
insert image description here

Guess you like

Origin blog.csdn.net/poppyty/article/details/118414503