{"id":9804,"date":"2020-05-15T18:53:02","date_gmt":"2020-05-15T13:23:02","guid":{"rendered":"https:\/\/java2blog.com\/?p=9804"},"modified":"2021-10-28T17:22:34","modified_gmt":"2021-10-28T11:52:34","slug":"cv2-gaussianblur-python","status":"publish","type":"post","link":"https:\/\/java2blog.com\/cv2-gaussianblur-python\/","title":{"rendered":"Python | cv2 GaussianBlur() Method"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Syntax\">Syntax<\/a><ul><li><a href=\"#Parameters\">Parameters<\/a><\/li><li><a href=\"#Return_Value\">Return Value<\/a><\/li><\/ul><\/li><li><a href=\"#cv2_GaussianBlur_Method_example\">cv2 GaussianBlur() Method example<\/a><\/li><\/ul><\/div>\n<p>In this tutorial, we will see how to Blurring an image in python programming language using open-cv, which exists as a cv2 (computer vision) library in python.<\/p>\n<p>You can use <code>GaussianBlur()<\/code> method of <code>cv2<\/code> library to blur an image. In order to use cv2 library, you need to import cv2 library using <code>import statement<\/code>. You can read about more about <a href=\"https:\/\/en.wikipedia.org\/wiki\/Gaussian_function\" target=\"_blank\" rel=\"noopener\">Gaussian function<\/a><\/p>\n<p>The blurring of an image means smoothening of an image i.e., removing outlier pixels that may be noise in the image.<\/p>\n<p>Now let&#8217;s see the syntax and return value of <code>GaussianBlur()<\/code> method, then we will move on the examples.<\/p>\n<h2><span id=\"Syntax\">Syntax<\/span><\/h2>\n<pre class=\"lang:python decode:1 \" >\ncv2.GaussianBlur(src, ksize, sigmaX, sigmaY, borderType)\n<\/pre>\n<h3><span id=\"Parameters\">Parameters<\/span><\/h3>\n<ol>\n<li><code>src:<\/code> Source\/Input of n-dimensional array.<\/li>\n<li><code>ksize:<\/code> Kernal is matrix of an (no. of rows)*(no. of columns) order .Its Size is given in the form of tuple (no. of rows, no. of columns). no. of rows and no. of columns should be odd .If ksize is given as (0 0), then ksize is computed from given sigma values i.e. sigmaX and sigmaY.<\/li>\n<li><code>sigmaX:<\/code> Standard deviation value of kernal along horizontal direction.<\/li>\n<li><code>sigmaY:<\/code> Standard deviation value of kernal along vertical direction.<\/li>\n<li><code>borderType:<\/code> This specify boundaries of an image while kernel is applied on borders of an image.<\/li>\n<p>Possible values of borderType are :<\/p>\n<ol>\n<li>cv2.BORDER_CONSTANT<\/li>\n<li> cv2.BORDER_REPLICATE<\/li>\n<li> cv2.BORDER_REFLECT<\/li>\n<li> cv2.BORDER_WRAP<\/li>\n<li> cv2.BORDER_REFLECT_101<\/li>\n<li>cv2.BORDER_TRANSPARENT<\/li>\n<li> cv2.BORDER_REFLECT101<\/li>\n<li>cv2.BORDER_DEFAULT<\/li>\n<li>cv2.BORDER_ISOLATED\n<\/li>\n<\/ol>\n<\/ol>\n<h3><span id=\"Return_Value\">Return Value<\/span><\/h3>\n<p>It returns Output blurred image of n-dimensional array.<\/p>\n<blockquote><p>\na) In <code>GaussianBlur()<\/code> method, you need to pass <code>src<\/code> and <code>ksize<\/code> values everytime and either one, two, or all parameters value from remaining <code>sigmax<\/code>, <code>sigmaY<\/code> and <code>borderType<\/code> parameter should be passed.<\/p>\n<p>b) Both sigmaX and sigmaY parameters become optional if you mention the <code>ksize(kernal size)<\/code> value other than <code>(0,0)<\/code>.\n<\/p><\/blockquote>\n<h2><span id=\"cv2_GaussianBlur_Method_example\">cv2 GaussianBlur() Method example<\/span><\/h2>\n<p>Now Let&#8217;s see the Python code :<\/p>\n<p><code>Example 1:<\/code> Using GaussianBlur() method with <code>src<\/code>,<code>ksize<\/code> and <code>borderType<\/code> parameters.<\/p>\n<pre class=\"lang:python decode:1 \" >\n\n# import computer vision library(cv2) in this code\nimport cv2\n\n# main code\nif __name__ == \"__main__\" :\n\n    # mentioning absolute path of the image\n    img_path = \"C:\\\\Users\\\\user\\\\Desktop\\\\flower.jpg\"\n\n    # Load\/Read an image\n    image = cv2.imread(img_path)\n\n    # show the image on the newly created image window\n    cv2.imshow('Input image',image)\n\n    # applying gaussian blur on the image\n    blur_img = cv2.GaussianBlur(image,(5,5),cv2.BORDER_DEFAULT)\n\n    # show the image on the newly created image window\n    cv2.imshow('Blur image',blur_img)\n<\/pre>\n<p><strong>Output :<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/java2blog.com\/wp-content\/uploads\/2020\/05\/s_5.png\" alt=\"Output1\" \/><\/p>\n<p><code>Example 2:<\/code> Using <code>GaussianBlur()<\/code> method with <code>src<\/code>,<code>ksize<\/code> and <code>sigmaX<\/code> parameters.<\/p>\n<pre class=\"lang:python decode:1 \" >\n# import computer vision library(cv2) in this code\nimport cv2\n\n# main code\nif __name__ == \"__main__\" :\n\n    # mentioning absolute path of the image\n    img_path = \"C:\\\\Users\\\\user\\\\Desktop\\\\flower.jpg\"\n\n    # Load\/Read an image\n    image = cv2.imread(img_path)\n\n    # show the image on the newly created image window\n    cv2.imshow('Input image',image)\n\n    # applying gaussian blur on the image with kernal size(5,5)\n    # and sigmaX = 5\n    blur_img = cv2.GaussianBlur(image,(5,5),5)\n\n    # show the image on the newly created image window\n    cv2.imshow('Blur image',blur_img)\n<\/pre>\n<p>Output :<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/java2blog.com\/wp-content\/uploads\/2020\/05\/s_6.png\" alt=\"Output2\" \/><\/p>\n<p>You may similarly change the values of other properties and observe the outputs.<br \/>\nThat&#8217;s all about cv2 GaussianBlur() Method in Python.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsSyntaxParametersReturn Valuecv2 GaussianBlur() Method example In this tutorial, we will see how to Blurring an image in python programming language using open-cv, which exists as a cv2 (computer vision) library in python. You can use GaussianBlur() method of cv2 library to blur an image. In order to use cv2 library, you need to [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[197,145],"tags":[196,193],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/9804"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=9804"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/9804\/revisions"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=9804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=9804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=9804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}