Averaging, Gaussian Blur and Median Blur in OpenCV

FREE Online Courses: Elevate Skills, Zero Cost. Enroll Now!

In this article, we’ll be understanding the various blurring operations provided by the OpenCV library. The blurring or smoothening techniques applied on an image remove outlier pixels that may constitute noise in an image. Smoothing and blurring operations are important steps in computer vision and image processing.

What is blurring in OpenCV?

Blurring is the process of applying a low-pass filter to an image. In the field of computer vision, a low pass filter refers to the removal of noise from an image while not disrupting the majority of its regions.

OpenCV blurs an image by applying a Kernel. A kernel describes the change in the value of pixels depending on the combination of neighboring pixels selected. The kernel is applied to each pixel of the image to give the final result and this process is termed convolution.

Why is blurring important in image processing?

1. Removal of noise: A high pass signal is considered as noise in an image and by application of a low pass filter to an image, the noise is restricted.

2. Removal of high-frequency content: It removes high-frequency content which might not be useful for us such as noise and edges. It reduces the details of an image and aids in the application of other algorithms to the image. By reducing the details, we can recognize other features more easily.

3. Removal of low-intensity edges: The value of image intensity change is not significant from one side of the abruption encountered to another and hence it is discarded.

Averaging in OpenCV

Averaging is the blurring technique where the image is normalized. It replaces the central elements with the calculated average of pixel values under the kernel area.

OpenCV provides the cv2.blur() function for averaging applications. The kernel height and width need to be defined in this function.

Syntax

cv2.blur(src, dst, ksize, anchor, borderType)

Parameters

  • src: Source image or input image
  • dst: Output image
  • ksize: Kernel size
  • anchor: Anchor points
  • borderType: The type of border

Implementation

# Importing OpenCV
import cv2

# Importing numpy
import numpy as np

# Importing matplotlib.pyplot
import matplotlib.pyplot as plt

# Reading the image
img = cv2.imread(r'C:\Users\tushi\Downloads\PythonGeeks\flower.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# Displaying the original image
plt.imshow(img)

original

# Averaging the image
img = cv2.blur(img, (3,3))

# Displaying the blurred image
plt.imshow(img)

averaging in opencv

Gaussian Blur in OpenCV

In the Gaussian blurring method, a Gaussian kernel is used.

It is done with the function, cv2.GaussianBlur(). Gaussian blur replaces the central elements with the calculated weighted mean of pixel values under the kernel area. In Gaussian blurring, pixels closer to the central element, contribute more to the weight. Gaussian blurring is used to remove noise following a gaussian distribution.

The kernel height and width need to be defined for this function which should be positive and odd. The standard deviation in X and Y directions also needs to be specified. If the standard deviation in one direction is specified, then the other is taken as the same. If both deviations are zero, then the standard deviation is calculated using the kernel size.

Syntax

cv2.GaussianBlur(src, ksize, dst, sigmaX,sigmaY, borderType)

Parameters

  • src: Source image or input image
  • dst: Output image
  • ksize: Kernel size. Height and width must be positive and odd.
  • sigmaX: Standard deviation in the X direction
  • sigmaY: Standard deviation in the Y direction
  • borderType: The type of border

Implementation

# Importing OpenCV
import cv2

# Importing numpy
import numpy as np
# Importing matplotlib.pyplot
import matplotlib.pyplot as plt

# Reading the image
img = cv2.imread(r'C:\Users\tushi\Downloads\PythonGeeks\flower.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# Displaying the original image
plt.imshow(img)

original

# Averaging the image
img = cv2.GaussianBlur(img, (5,5), cv2.BORDER_DEFAULT)

# Displaying the blurred image
plt.imshow(img)

gaussian blur

Median Blur in OpenCV

OpenCV provides the cv2.medianBlur() function to perform the median blur operation. Median blur replaces the central elements with the calculated median of pixel values under the kernel area. The kernel size for the median blur operation should be positive and odd. The kernel size of the median blur should be a square.

Syntax

cv2.medianBlur(src, dst, ksize)

Parameters

  • src: Source image or input image
  • dst: Output image
  • ksize: Kernel size. Height and width must be positive and odd.

Implementation

# Importing OpenCV
import cv2

# Importing numpy
import numpy as np

# Importing matplotlib.pyplot
import matplotlib.pyplot as plt

# Reading the image
img = cv2.imread(r'C:\Users\tushi\Downloads\PythonGeeks\flower.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# Displaying the original image
plt.imshow(img)

original

# Averaging the image
img = cv2.medianBlur(img, 5)

# Displaying the blurred image
plt.imshow(img)

median blur in opencv

Conclusion

Through this article, we gained insights into the various blurring and smoothing methods available in the OpenCV library. We learned how to apply Averaging, Gaussian, and Median blur functions on an image in OpenCV by defining a kernel and also observed the implementation of these functions.

Did we exceed your expectations?
If Yes, share your valuable feedback on Google | Facebook


Leave a Reply

Your email address will not be published. Required fields are marked *