Smoothing Images

摘自https://docs.opencv.org/4.2.0/d4/d13/tutorial_py_filtering.html

摘自https://docs.opencv.org/4.2.0/dc/dd3/tutorial_gausian_median_blur_bilateral_filter.html

2D Convolution ( Image Filtering )

As in one-dimensional signals, images also can be filtered with various low-pass filters (LPF), high-pass filters (HPF), etc. LPF helps in removing noise, blurring images, etc. HPF filters help in finding edges in images.

OpenCV provides a function cv.filter2D() to convolve a kernel with an image. 

As an example, we will try an averaging filter on an image. 

img = cv.imread('opencv_logo.png')

kernel = np.ones((5,5),np.float32)/25
dst = cv.filter2D(img,-1,kernel)

Image Blurring (Image Smoothing)

Image blurring is achieved by convolving the image with a low-pass filter kernel. It actually removes high frequency content (eg: noise, edges) from the image. OpenCV provides four main types of blurring techniques.

1. Averaging

This is done by convolving an image with a normalized box filter. It simply takes the average of all the pixels under the kernel area and replaces the central element. This is done by the function cv.blur() or cv.boxFilter(). We should specify the width and height of the kernel. The kernel is below:

blur = cv.blur(img,(5,5))

2. Gaussian Blurring

In this method, instead of a box filter, a Gaussian kernel is used. It is done with the function, cv.GaussianBlur(). We should specify the width and height of the kernel which should be positive and odd. We also should specify the standard deviation in the X and Y directions, sigmaX and sigmaY respectively. If only sigmaX is specified, sigmaY is taken as the same as sigmaX. If both are given as zeros, they are calculated from the kernel size. Gaussian blurring is highly effective in removing Gaussian noise from an image.

If you want, you can create a Gaussian kernel with the function, cv.getGaussianKernel().

blur = cv.GaussianBlur(img,(5,5),0)

3. Median Blurring

cv.medianBlur() takes the median of all the pixels under the kernel area and the central element is replaced with this median value. This is highly effective against salt-and-pepper noise in an image. Its kernel size should be a positive odd integer.

median = cv.medianBlur(img,5)

4. Bilateral Filtering

cv.bilateralFilter() is highly effective in noise removal while keeping edges sharp. But the operation is slower compared to other filters. 

In an analogous way as the Gaussian filter, the bilateral filter also considers the neighboring pixels with weights assigned to each of them. These weights have two components, the first of which is the same weighting used by the Gaussian filter. The second component takes into account the difference in intensity between the neighboring pixels and the evaluated one.

blur = cv.bilateralFilter(img,9,75,75)

The texture on the surface is gone, but the edges are still preserved.

Additional Resources

bilateral filtering

bilateral filtering

猜你喜欢

转载自blog.csdn.net/Airfrozen/article/details/104430730