Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

OpenCV Image Smoothing

OpenCV Image Smoothing

In image processing, smoothing (also known as blurring) is a common operation used to reduce noise or detail in an image.

OpenCV provides a variety of smoothing methods. This article will explain in detail four commonly used smoothing techniques: mean filtering, Gaussian filtering, median filtering, and bilateral filtering.

1. Mean Filtering (cv2.blur())

Mean filtering is one of the simplest smoothing methods.

The principle of mean filtering is to replace the value of each pixel in the image with the average value of its surrounding pixels.

Mean filtering can effectively remove noise, but it may cause the image to become blurry.

Usage

Example

import

cv2

Read an image

image

=

cv2.

imread

(

'image.jpg'

)

Apply mean filtering

blurred_image

=

cv2.

blur

(

image

,

(

5

,

5

)

)

Display Results

cv2.

imshow

(

'Blurred Image'

,

blurred_image

)

cv2.

waitKey

(

0

)

cv2.

destroyAllWindows

(

)

cv2.blur(image, (5, 5)) parameter description

Applicable scenarios

Mean filtering is suitable for removing random noise in an image, but it may cause image edges to become blurry.

2. Gaussian Filtering (cv2.GaussianBlur())

Gaussian filtering is a smoothing method based on the Gaussian function. Unlike mean filtering, Gaussian filtering assigns higher weight to the center pixel and lower weight to edge pixels when calculating the pixel average.

Gaussian filtering can better preserve image edge information while removing noise.

Usage

Example

import

cv2

Read an image

image

=

cv2.

imread

(

'image.jpg'

)

Apply Gaussian filtering

blurred_image

=

cv2.

GaussianBlur

(

image

,

(

5

,

5

)

,

0

)

Display Results

cv2.

imshow

(

'Gaussian Blurred Image'

,

blurred_image

)

cv2.

waitKey

(

0

)

cv2.

destroyAllWindows

(

)

cv2.GaussianBlur(image, (5, 5), 0) parameter description

Applicable scenarios

Gaussian filtering is suitable for removing Gaussian noise in an image and performs well in preserving image edge information.

3. Median Filtering (cv2.medianBlur())

Median filtering is a nonlinear smoothing method. Its principle is to replace the value of each pixel in the image with the median value of its surrounding pixels.

Median filtering is very effective at removing salt-and-pepper noise (i.e., randomly occurring black and white dots in an image).

Usage

Example

import

cv2

Read an image

image

=

cv2.

imread

(

'image.jpg'

)

Apply median filtering

blurred_image

=

cv2.

medianBlur

(

image

,

5

)

Display Results

cv2.

imshow

(

'Median Blurred Image'

,

blurred_image

)

cv2.

waitKey

(

0

)

cv2.

destroyAllWindows

(

)

cv2.medianBlur(image, 5) parameter description

Applicable scenarios

Median filtering is suitable for removing salt-and-pepper noise in an image and performs well in preserving image edge information.

4. Bilateral Filtering (cv2.bilateralFilter())

Bilateral filtering is a nonlinear smoothing method that combines spatial proximity and pixel value similarity.

Unlike Gaussian filtering, bilateral filtering can preserve image edge information while smoothing the image. This is because bilateral filtering considers not only the spatial distance between pixels but also the difference in pixel values.

Usage

Example

import

cv2

Read an image

image

=

cv2.

imread

(

'image.jpg'

)

Apply bilateral filtering

blurred_image

=

cv2.

bilateralFilter

(

image

,

9

,

75

,

75

)

Display Results

cv2.

imshow

(

'Bilateral Filtered Image'

,

blurred_image

)

cv2.

waitKey

(

0

)

cv2.

destroyAllWindows

(

)

cv2.bilateralFilter(image, 9, 75, 75) parameter description

Applicable scenarios

Bilateral filtering is suitable for preserving image edge information while removing noise, and is commonly used for image beautification or preprocessing.

Summary

OpenCV provides a variety of image smoothing methods, each with its own unique advantages and applicable scenarios. Mean filtering is simple and easy to use, but may cause image blurring; Gaussian filtering can better preserve edge information while removing noise; median filtering is particularly suitable for removing salt-and-pepper noise; and bilateral filtering performs excellently in preserving edge information. Depending on the specific application scenario, choosing an appropriate smoothing method can significantly improve the effectiveness of image processing.