Filtering in OpenCV

FREE Online Courses: Dive into Knowledge for Free. Learn More!

In this article, we’ll be covering the various filtering methods provided by the OpenCV library. But firstly, let us see what is filtering in OpenCV.

Filtering in OpenCV

Filtering is the most fundamental operation in computer vision and image processing. It is the manipulation of pixel values based on the filtering function applied at that location. The value for the pixel is calculated by applying a kernel and then the central value is replaced by the calculated value.

Filters are used to suppress high frequencies i.e. image smoothing or low frequencies i.e. enhancement of edges in image processing.

Filtering Methods in OpenCV

1. Filter2d in OpenCV

The cv2.filter2D function provided by the OpenCV library convolves an image with a kernel. An arbitrary linear filter is applied to the image using this function. Furthermore, the function interpolates outlier pixels according to the border mode specified when the aperture is partially outside the image.

Image Processing, cv2.filter2D() function is used to change the pixel intensity value of an image based on the surrounding pixel intensity values. This function is used to enhance or remove certain features of an image and create new images.

Syntax

filter2D (src, dst, ddepth, kernel)

Parameters

  • src: Source image or input image
  • dst: Output image
  • ddepth: Depth of output image (-1 is used to specify the same depth as input image)
  • kernel: Defines the kernel

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

# Defining the kernel     
kernel = np.ones((5,5),np.float32)/25  

# Applying  Filter2D function to the image
img = cv2.filter2D(img,-1,kernel)
    
# Displaying the output image
plt.imshow(img)

filter2D in opencv

2. Bilateral Filter in OpenCV

In OpenCV, cv2.bilateralFilter() is useful for effectively removing noise from an image without disturbing the edges of the image.

Bilateral filtering takes a Gaussian filter in space and one Gaussian filter which is a function of the difference in pixel values. Only nearby pixels are considered for blurring purposes using the Gaussian function and only pixels with similar intensity values to the central pixel are considered using the Gaussian function of intensity. The edges are preserved since the pixels at the edges will have large intensity variations.

Syntax

cv2.bilateralFilter(src, dst, d, sigmaSpace, borderType)

Parameters

  • src: Source image or input image
  • dst: Output image
  • d: Pixel neighborhood diameter used during filtering. If the value is negative, sigmaSpace is for computing the value
  • sigmaColor: Color space of the filter
  • sigmaSpace: Coordinate space of the filter
  • borderType: 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

# Defining the kernel     
kernel = np.ones((5,5),np.float32)/25  

# Applying bilateral filter to the image
img = cv2.bilateralFilter(img,10,75,75) 

# Displaying the output image
plt.imshow(img)

bilateral filter in opencv

3. BoxFilter in OpenCV

In OpenCV, cv2.boxFilter() is useful for filtering an image using the box filter. The Box Filter operation is similar to the averaging method in blurring, it applies a bilateral image to a filter. The box can be normalized or not.

Syntax

boxFilter(src, dst, ddepth, ksize, anchor, normalize, borderType)

Parameters

  • src: Source image or input image
  • dst: Output image
  • ddepth: Depth of output image (-1 is used to specify the same depth as input image)
  • kernel: Defines the kernel
  • anchor: Represents the anchor point
  • normalize: Specifies whether kernel is normalized
  • borderType: 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

# Defining the kernel     
kernel = np.ones((5,5),np.float32)/25  

# Applying box filter to the image
img = cv2.boxFilter(img, 0, (3,3), img, (-1,-1), False, cv2.BORDER_DEFAULT)  

# Displaying the output image
plt.imshow(img)

box filter in opencv

4. SQRbox filter

It calculates the normalized sum of the squares value of the pixels. The SQRbox filter first squares the intensity values for the neighboring pixels of the pixel under the filter. Then the sum of the squares of the intensity values is computed.

The unnormalized square box filter is used in computing the statistics for local image, such as variance and standard deviation for pixel neighborhood.

Syntax

cv2.sqrBoxFilter(src, ddepth, ksize, dst, anchor, normalize, borderType)

Parameters

  • src: Source image or input image
  • dst: Output image of the same size and type as the source image
  • ddepth: The depth of the output image
  • ksize: The size of the kernel
  • anchor: Represents the anchor point
  • normalize: Specifies whether kernel should be normalized
  • borderType: Type of border

Conclusion

Through this article, we understood the various filtering methods available in the OpenCV library and the need for them. We learned about the filter2D function, bilateral filtering function, and box filter and observed the implementation of these functions.

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google | Facebook


Leave a Reply

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