Image Filtering— using OpenCV

Soumya Patil
4 min readNov 17, 2019

--

Image Filtering is a step during image preprocessing. When it comes to detecting edges and contours, noise gives a great impact on the accuracy of detection. Therefore removing noises and controlling the intensity of the pixel is necessary. Image filtering is done to remove noise and any undesired features from an image, creating a better and enhanced version of that image.

There are two types of filters that exist: linear and non-linear.

Linear Filter: Mean, Laplacian

Non-Linear Filter: Median, GaussianBlur

Why do Image Filtering?

Image filters can be used to reduce the amount of noise in an image and to enhance the edges in an image. It is to remove low-intensity edges. It can also be used to hide the details of an image. An image pre-processing is done to increase the accuracy of the models. On the contrary, if we blur the images too much, we’ll lose the data. Therefore we need to find an adequate amount of blurring we’re going to apply without losing desirable edges.

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

Blur

The blur() function of OpenCV takes two parameters first is the image, second kernel (a matrix) A kernel is an n x n square matrix where n is an odd number. The kernel depends on the digital filter. This is used to blur the complete image. The kernel specifies the intensity to which it should be blurred.

cv2.medianBlur(img, 21)

medianBlur

This is a non-linear type of filter. Median Blur is used in Digital Image Processing, the edges of the image are preserved in medianBlur() This filtering technique is used best to remove salt and pepper type of noise. This uses the median of the matrix for blurring.

cv2.GaussianBlur(img, (21,21),0)

Applying a Gaussian blur to an image is the same as convolving the image with a Gaussian function. It is mostly used in graphics software. The specified width and height of the kernel should be positive and odd.

GaussianBlur

cv2.bilateralFilter(img, 21,51,51)

This is the advanced version of Gaussian Filter it also has some functionality of the median blur which is it preserves the edges while removing the noise from the image. The original image and his bilaterFilter image may look a bit similar but they are not. The unwanted noise is removed once we perform this filtering technique.

bilaterFilter

Canny Edge Detection

Canny(image, edges, threshold1, threshold2)

The first argument is our input image. Second and third arguments are our minVal and maxVal respectively.

The canny edge detector is a 4-step detection process. The steps are:

  1. Noise Reduction — 5x5 Gaussian filter
  2. Calculating gradients — Finding Intensity Gradient of the Image
  3. No maximum suppression — upper threshold
  4. Thresholding with hysteresis — upper/lower threshold
Original Image noise — Bilateral Image noise

Edge detection helps in to maintain the structural aspect of the image and reduce the amount of data needed to process.

Summary:

These are few of the image filtering techniques which can be performed by OpenCV Python. This will reduce the noise from the image and smoothen it.

--

--