OpenCV study notes 5: image processing -- image blur

 

1. 2D convolution (image filtering)

The image, as a one-dimensional signal, can also be filtered through a variety of low-pass filters (LPF) and high-pass filters (HPF).

A low-pass filter removes noise, or blurs the image. A high pass filter can help find boundaries in an image.

OpenCV provides a function, cv2.filter2D(), to perform kernel convolution on an image.

 

For example, using a 5x5 filter kernel, which means that for each pixel, its color value is: the color value of all pixels in the surrounding 5x5 area is accumulated, and then divided by 25 (equal to calculating the average of the pixel color values ​​in this area) .

After doing the above processing for all pixels in an image, wait until a filtered image.

cv2.filter2D(src, ddepth, kernel)

a) src: source image

b) ddepth: target image depth (image depth: the number of bits used to store each pixel)

c) kernel: convolution kernel, a single-channel floating-point matrix

Example result:

a 5x5 core, and a 50x50 core


 

2. Image blur (image smoothing)

Image mode is a convolution operation on an image using a low-pass filter kernel. Noise can be removed.

OpenCV provides main 4 blurring techniques:

1) Mean blur

Convolve an image through a standard box filter. Take the average of all pixel color values ​​within the kernel and assign it to the center pixel.

cv2.blur(img, (5,5))

 

2) Gaussian blur

To use a Gaussian kernel, the size of the kernel needs to be specified as a positive odd integer. Standard deviations in the X and Y directions need to be specified.

Very efficient for removing Gaussian noise in images.

(Gaussian noise refers to a class of noise whose probability density function follows a Gaussian distribution (ie, normal distribution))

 

cv2.GaussianBlur(src, ksize, sigmaX)

a) src: source image

b) ksize: convolution kernel size

c) Orientation deviation in X direction

 

3) Median blur

Find the median of the pixels in the convolution kernel and assign it to the center pixel. Very effective for removing salt and pepper noise.

In contrast to Gaussian kernels and standard box kernels, it is possible to assign pixels to color values ​​that are not present in the source image. Median smoothing definitely sets the center pixel a pixel color value that exists in the source image. This is also very effective in reducing noise.

cv2.medianBlur(src, ksize)

a) src: source image

b) ksize: convolution kernel size

 

4) Bilateral blur

Compared with the above three fuzzy methods, the boundary is blurred, and the bilateral smoothing preserves the boundary, but the processing speed is slower than the three methods.

cv2.bilateralFilter (src, d, sigmaColor, sigmaSpace)

a) src: source image

b) d: the number of neighbor pixels participating in blurring

c) sigmaColor: The larger the value, the more distant colors in the pixel neighborhood will be mixed together, resulting in a larger area of ​​nearly equal colors

d) sigmaSpace: larger values ​​indicate that farther pixels with sufficiently similar colors influence each other

 

Example result:



 

-- End --

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326176197&siteId=291194637