Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Open CV
  • scikit-image
  • pycairo
  • Pyglet
  • Python
  • Numpy
  • Pandas
  • Python Database
  • Data Analysis
  • ML Math
  • Machine Learning
  • NLP
  • Deep Learning
  • Deep Learning Interview Questions
  • ML Projects
  • ML Interview Questions
  • 100 Days of Machine Learning
Open In App
Next Article:
Python OpenCV - getWindowImageRect() Function
Next article icon

Python OpenCV – getgaussiankernel() Function

Last Updated : 03 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Python OpenCV getGaussianKernel() function is used to find the Gaussian filter coefficients. The Gaussian kernel is also used in Gaussian Blurring. Gaussian Blurring is the smoothing technique that uses a low pass filter whose weights are derived from a Gaussian function. In fact, this is the most widely used low pass filter in CV(computer vision) applications. Different properties of the gaussian filter make the algorithm more efficient.

Syntax: cv.getGaussianKernel(ksize, sigma[, ktype])

Parameters:

  • Ksize: It is the Aperture size. Ksize value should be odd and positive.
  • sigma: Sigma is the Gaussian standard deviation. It is computed from ksize as sigma = 0.3*((ksize-1)*0.5 – 1) + 0.8 if it is non-positive.
  • ktype: It is the type of filter coefficients. It can be CV_32F or CV_64F.

Returns:

matrix of Gaussian filter coefficients.

The getGaussianKernel() function computes and returns the matrix of dimension ksize×1 of Gaussian filter coefficients:

Gi=α∗e−(i−(ksize−1)/2)2/(2∗sigma2)

where i=0 to ksize−1 and α is the scale factor chosen so that ∑iGi=1.

Two of such generated kernels can be passed to sepFilter2D. Those passed functions automatically recognize smoothing kernels (symmetrical kernel with the sum of weights equal to 1) and handle them accordingly. You may also use the higher-level GaussianBlur. Here, one thing to remember is that k which is the aperture size should also be odd and positive.

So, you might have a question that what is the Gaussian kernel and why do we need it? The Gaussian kernel is linearly separable. This means we can break any 2 dimension filter into two 1 dimension filters. Because of this, the computational complexity is reduced from O(n2) to O(n). What time is what matters in computer science.

Applying multiple successive Gaussian kernels is the same as applying a single, larger Gaussian blur, whose radius is equal to the square root of the sum of the squares of the multiple kernels radius. Using this characteristic we can approximate a non-separable filter by a mixture of multiple separable filters. The Gaussian kernel weights(1-D) can be obtained quickly using Pascal’s Triangle.

Example 1:

Here, in the below example we will find the Gaussian kernel of one image. We first read the image using cv2. Then we create the Gaussian kernel of size 3×1 using getgaussiankernel() function. ksize which is the Aperture size is odd and positive.

The below image is used in this example:

Python3

# Python OpenCV - getgaussiankernel() Function
  
# import cv2
import cv2
  
# read image
img = cv2.imread('gfg2.jpg')
  
# Creates a 1-D Gaussian kernel
a = cv2.getGaussianKernel(3, 1)
  
# print Gaussian filter coefficients matrix
print(a)
                      
                       

Output:

[[0.27406862]   [0.45186276]   [0.27406862]]

Example 2:

In this example, we will find the Gaussian kernel of one image, we create the Gaussian kernel of size 7×1 using getgaussiankernel() function. 

Python3

# Python OpenCV - getgaussiankernel() Function
  
# import cv2
import cv2
  
# read image
img = cv2.imread('gfg_logo.png')
  
# Creates a 1-D Gaussian kernel
a = cv2.getGaussianKernel(7, 1)
  
# print Gaussian filter coefficients matrix
print(a)
                      
                       

Output:

[[0.00443305]   [0.05400558]   [0.24203623]   [0.39905028]   [0.24203623]   [0.05400558]   [0.00443305]]


Next Article
Python OpenCV - getWindowImageRect() Function

R

rushi_javiya
Improve
Article Tags :
  • Python
  • OpenCV
  • Python-OpenCV
Practice Tags :
  • python

Similar Reads

  • Python OpenCV - getWindowImageRect() Function
    Python OpenCV getWindowImageRect() Function returns the client screen coordinates, along with the width and height of the window containing the picture. Syntax of cv2.getWindowImageRect() Syntax: cv2.getWindowImageRect(window_name) Parameter:  window_name - Name of the window displaying image/video
    3 min read
  • Python OpenCV - imencode() Function
    Python OpenCV imencode() function converts (encodes) image formats into streaming data and stores it in-memory cache. It is mostly used to compress image data formats in order to make network transfer easier. Basic example of imencode() FunctionExample 1: We began by importing the necessary librarie
    2 min read
  • Python OpenCV - haveImageReader() function
    In this article, we are going to learn about the haveImageReader() function of the OpenCV library.  The haveImageReader() function is used to check whether specified images can be decoded or read successfully by OpenCV or not. Sometimes we need to detect if the specified image file is being correctl
    1 min read
  • Python OpenCV - haveImageWriter() function
    In this article, we are going to learn about the haveImageWriter() function of the OpenCV library. haveImageWriter() function Sometimes we need to detect if the specified image file is being correctly written or not before continuing further, In such a case we can use OpenCV which helps us to proces
    2 min read
  • Python OpenCV - getRotationMatrix2D() Function
    cv2.getRotationMatrix2D() function is used to make the transformation matrix M which will be used for rotating a image. Syntax:  cv2.getRotationMatrix2D(center, angle, scale) Parameters:  center: Center of rotationangle(θ): The angle of rotation in degrees. A positive value rotates the image anti-cl
    3 min read
  • Python OpenCV - namedWindow() Function
    Python OpenCV namedWindow() method is used to create a window with a suitable name and size to display images and videos on the screen. The image by default is displayed in its original size, so we may need to resize the image for it to fit our screen. Created windows are referred by their names and
    3 min read
  • OpenCV - imdecode() Function in Python
    cv2.imdecode() function is used to read image data from a memory cache and convert it into image format. This is generally used for loading the image efficiently from the internet.  Example: Decoding and Saving an Image from URL in ColorThis example demonstrates how to download an image from a URL,
    2 min read
  • Python OpenCV - Canny() Function
    Canny edge detection algorithm is used in computer vision for identifying edges within an image. It helps in highlighting boundaries which are important for tasks like object detection and image segmentation. In this article, we will see how OpenCV's built-in Canny() function detects edges in an ima
    3 min read
  • Python OpenCV - Drowsiness Detection
    The number of fatalities on the road due to drowsiness is very high. Thus python allows the model of deep learning algorithm via including the use of OpenCV. Drowsiness Detection is the detection of a person to check whether the person is feeling sleepy while performing a significant task. This dete
    9 min read
  • Python OpenCV - waitKey() Function
    waitkey() function of Python OpenCV allows users to display a window for given milliseconds or until any key is pressed. It takes time in milliseconds as a parameter and waits for the given time to destroy the window, if 0 is passed in the argument it waits till any key is pressed.  Examples 1: Disp
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences