Q:

How to apply filters to images using Python and OpenCV?

0

Python OpenCV Image Filtering

How to apply filters to images using Python and OpenCV

In this exercise, you will learn different image filtering techniques using Python OpenCV. Python OpenCV has several filtering techniques to perform smoothing operations on images. These smoothing techniques are generally used to reduce noise, reduce detail, and so on. These techniques can also be applied to reduce the pixelated effect in low resolution images.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Averaging Filter

The Averaging Filter technique takes the average of all the pixels under the kernel area and replaces the central element. The function cv2.blur() and cv2.boxFilter() can be used to perform the averaging filter. Both functions smooth an image using the kernel.

Syntax of cv2.blur()
cv2.blur(image, ksize)

Here, the image is the image source and ksize is the size of blurring kernel.

Syntax of cv2.boxFilter()
cv2.boxFilter(src, dst, depth, ksize, anchor, normalize, bordertype)

Here, the src is the image source, dst is the destination image of the same size, depth denotes the output image depth. The anchor denotes the anchor points. By default, it is at kernel point (cordinate (-1,1)). The ksize is the size of blurring kernel and normalize is the flag which specifies whether the kernel should be normalized or not. The bordertype is an integer object represents the type of the border used.

Example of Averaging Filter
import cv2
import numpy as np
  
# image path 
path = r'cat.jpg'

# using imread()  
img = cv2.imread(path)

im1 = cv2.blur(img,(5,5))
im2 = cv2.boxFilter(img, -1, (10, 10), normalize=True)  

cv2.imshow('image', np.hstack((im1, im2)))
cv2.waitKey(0);
cv2.destroyAllWindows();
cv2.waitKey(1)

OpenCV Averaging Filter

Gaussian Filter

The OpenCV Gaussian filtering provides cv2.GaussianBlur() method to blur an image by using Gaussian Kernel. Each pixel in an image gets multiplied by Gaussian Kernel. It means, a Gaussian Kernel is a square array of pixels.

Syntax of Gaussian Filter
cv2.GaussianBlur(src, ksize, sigma_x, dst, sigma_y, border_type)

src - the input image,
ksize - Gaussian kernel size (width and height), the width and height can have different values and must be positive and odd,
sigma_x - Gaussian kernel standard deviation along X-axis,
dst - output image,
sigma_y - Gaussian kernel standard deviation along Y-axis,
boader_type - image boundaries.

Example of Gaussian
import cv2
import numpy
  
# image path 
path = r'cat.jpg'

# using imread()  
img = cv2.imread(path)

dst = cv2.GaussianBlur(img,(5,5),cv2.BORDER_DEFAULT) 

cv2.imshow('image', numpy.hstack((img, dst)))
cv2.waitKey(0);
cv2.destroyAllWindows();
cv2.waitKey(1)

OpenCV Gaussian Filter

Median Filtering

Python OpenCV provides the cv2.medianBlur() function to blur the image with a median kernel. This is a non-linear filtering technique. It is highly effective in removing salt-and-pepper noise. This takes a median of all the pixels under the kernel area and replaces the central component with this median value. Since we are taking a middle, the output image will have no new pixel esteems other than that in the input image.

Syntax of Median Filter
cv2.medianBlur(image, ksize)

Here, the image is representing the image for operation, the ksize is a size object representing the size of the kernel.

Example of Median Filter
import cv2
import numpy
  
# image path 
path = r'cat.jpg'

# using imread()  
img = cv2.imread(path)
dst = cv2.medianBlur(img,7)

cv2.imshow('image', numpy.hstack((img, dst)))
cv2.waitKey(0);
cv2.destroyAllWindows();
cv2.waitKey(1)

OpenCV Gaussian Filter

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Exercises, Practice Questions and Solutions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a python program to input week number and pr... >>
<< Write a python program to sum all the numbers in a...