TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

OpenCV Image Filters

OpenCV Image Filters with What is OpenCV, History, Installation, Reading Images, Writing Images, Resize Image, Image Rotation, Gaussian Blur, Blob Detection, Face Detection and Face Recognition etc.

<< Back to OPENCV

OpenCV Image Filters

Image filtering is the process of modifying an image by changing its shades or color of the pixel. It is also used to increase brightness and contrast. In this tutorial, we will learn about several types of filters.

Bilateral Filter

OpenCV provides the bilateralFilter() function to apply the bilateral filter on the image. The bilateral filter can reduce unwanted noise very well while keeping edges sharp. The syntax of the function is given below:

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

Parameters:

  • src- It denotes the source of the image. It can be an 8-bit or floating-point, 1-channel image.
  • dst- It denotes the destination image of the same size. Its type will be the same as the src image.
  • d - It denotes the diameter of the pixel neighborhood (integer type) that is used during filtering. If its value is negative, then it is computed from sigmaSpace.
  • sigmaColor - It denotes the filter sigma in the color space.
  • sigmaSpace - It denotes the filter sigma in the coordinate space.

Consider the following example:

import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread(r'C:\Users\DEVANSH SHARMA\baloon.jpg',1)

kernel = np.ones((5,5),np.float32)/25
blur = cv2.bilateralFilter(img,9,75,75)
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(blur),plt.title('Bilateral Filter')
plt.xticks([]), plt.yticks([])
cv2.imshow("Image",blur)

Output

OpenCV Image Filters

Box Filter

We can perform this filter using the boxfilter() function. It is similar to the averaging blur operation. The syntax of the function is given below:

cv2. boxfilter(src, dst, ddepth, ksize, anchor, normalize, bordertype) 

Parameters:

  • src - It denotes the source of the image. It can be an 8-bit or floating-point, 1-channel image.
  • dst- It denotes the destination image of the same size. Its type will be the same as the src image.
  • ddepth - It denotes the output image depth.
  • ksize - It blurs the kernel size.
  • anchor - It denotes the anchor points. By default, its value Point to coordinates (-1,1), which means that the anchor is at kernel center.
  • normalize - It is the flag, specifying whether the kernel should be normalized or not.
  • borderType - An integer object represents the type of the border used.

Consider the following example:

import cv2
import numpy as np  
# using imread('path') and 0 denotes read as  grayscale image  
img = cv2.imread(r'C:\Users\DEVANSH SHARMA\baloon.jpg',1)  
img_1 = cv2.boxFilter(img, 0, (7,7), img, (-1,-1), False, cv2.BORDER_DEFAULT)
#This is using for display the image 
cv2.imshow('Image',img_1)
cv2.waitKey(3) # This is necessary to be required so that the image doesn't close immediately.  
#It will run continuously until the key press.  
cv2.destroyAllWindows()

Output

OpenCV Image Filters

Filter2D

It combines an image with the kernel. We can perform this operation on an image using the Filter2D() method. The syntax of the function is given below:

cv2.Filter2D(src, dst, kernel, anchor = (-1,-1))

Parameters:

  • src - It represents the input image.
  • dst- It denotes the destination image of the same size. Its type will be the same as the src image.
  • kernel - It is a convolution kernel, a single-channel floating-point matrix. If you want to apply different kernels to different channels, split the image into a separate color plane using the split () process them individually.
  • anchor - It denotes the anchor points, by default its value Point(-1,1), which means that the anchor is at kernel center.
  • borderType - An integer object represents the type of the border used.

Consider the following example:

import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread(r'C:\Users\DEVANSH SHARMA\baloon.jpg',1)

kernel = np.ones((5,5),np.float32)/25
dst = cv2.filter2D(img,-1,kernel)
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(dst),plt.title('Filter2D')
plt.xticks([]), plt.yticks([])
plt.show()

Output

OpenCV Image Filters




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf