{"id":22177,"date":"2021-09-25T13:52:48","date_gmt":"2021-09-25T13:52:48","guid":{"rendered":"https:\/\/www.askpython.com\/?p=22177"},"modified":"2021-10-01T20:38:36","modified_gmt":"2021-10-01T20:38:36","slug":"opencv-filter2d","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/opencv-filter2d","title":{"rendered":"Python OpenCV filter2D() function &#8211; A Complete Guide"},"content":{"rendered":"\n<p>Hello everyone! In this tutorial, we will learn how to use OpenCV <code>filter2D()<\/code> method to apply filters on images such as sharpening, bluring and finding edges in the images. So lets get started.<\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python-modules\/read-images-in-python-opencv\" data-type=\"post\" data-id=\"8838\">Read Images in Python using OpenCV<\/a><\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the OpenCV filter2D() function<\/h2>\n\n\n\n<p>While dealing with images in Image Processing, <code>filter2D()<\/code> function is used to change the pixel intensity value of an image based on the surrounding pixel intensity values. This method can enhance or remove certain features of an image to create a new image.<\/p>\n\n\n\n<p>Syntax to define <code>filter2D()<\/code> function in python is as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nresulting_image = cv2.filter2D(src, ddepth, kernel)\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\"><li><strong>src<\/strong>: The source image on which to apply the fitler. It is a matrix that represents the image in pixel intensity values.<\/li><li><strong>ddepth<\/strong>: It is the <strong>desirable depth<\/strong> of destination image. Value -1 represents that the resulting image will have same depth as the source image.<\/li><li><strong>kernel<\/strong>: kernel is the filter matrix applied on the image.<\/li><\/ul>\n\n\n\n<p>More formally, <code>filter2D()<\/code> function convolves an image with the kernel which results in an image becoming blur or sharpen and enhances the image features.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is a kernel?<\/h3>\n\n\n\n<p><span style=\"font-size: 1rem;background-color: var(--ast-global-color-5)\">Also known as <a href=\"https:\/\/www.askpython.com\/python-modules\/pytorch\" data-type=\"post\" data-id=\"11297\">convolution matrix<\/a> or mask, kernel is a small 2-dimensional matrix containing values that represent how much part of surrounding pixel values it should take to calculate intensity value of the current pixel. Usually, kernels are square matrices of odd length like <\/span>3&#215;3, 5&#215;5, 7&#215;7 matrices.<\/p>\n\n\n\n<p>Thus, the kernel act as a weighted matrix and is used for the blurring of images, sharpening of images, detection of edges in the images, etc. in image processing. This is done by convolution between image and kernel.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is Convolution?<\/h3>\n\n\n\n<p>In <a href=\"https:\/\/www.askpython.com\/python\/examples\/image-processing-in-python\" data-type=\"post\" data-id=\"9154\">Image Processing<\/a>, Convolution is simply an element wise multiplication of kernel and some part of source image to produce a new single data point representing a pixel, doing it on every possible part of image to create a new image. <\/p>\n\n\n\n<p>In Convolution, we take a submatrix from source image of same size as that of kernel, multiply each element of source image with corresponding element of kernel, perform addition on the previous computation and normalize the data so as to represent the data as pixel value.<\/p>\n\n\n\n<p>Consider an example as shown in the image below:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"400\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Convolution-Image-Processing-Example-1.png\" alt=\"Convolution Image Processing Example 1\" class=\"wp-image-22277\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Convolution-Image-Processing-Example-1.png 600w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Convolution-Image-Processing-Example-1-300x200.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><figcaption>Convolution Image Processing Example 1<\/figcaption><\/figure><\/div>\n\n\n\n<p>Convolution on an image can result in an image of size less than the source image. The difference depends on the size of our kernel. However, there are ways to deal with it as discussed <a href=\"https:\/\/en.wikipedia.org\/wiki\/Kernel_(image_processing)#Edge_Handling\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using OpenCV filter2d() with different kernels<\/h2>\n\n\n\n<p>Let&#8217;s apply the <code>filter2d()<\/code> function on an image with different kernels and see what results we get. For this example, we will use the following image.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"367\" height=\"267\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/filter2d-source-image.jpg\" alt=\"Filter2d Source Image\" class=\"wp-image-22178\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/filter2d-source-image.jpg 367w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/filter2d-source-image-300x218.jpg 300w\" sizes=\"auto, (max-width: 367px) 100vw, 367px\" \/><figcaption>Filter2d Source Image<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">1. Sharpening an Image<\/h3>\n\n\n\n<p>You can learn more about <a href=\"https:\/\/www.askpython.com\/python\/examples\/denoising-images-in-python\" data-type=\"post\" data-id=\"22026\">sharpening images<\/a>. This short snippet will sharpen the image shown above.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; highlight: [6,7,8,9,10,12]; title: ; notranslate\" title=\"\">\nimport cv2\nimport numpy as np\n# Loading source image\nsrc_image = cv2.imread(&quot;pug-dog.jpg&quot;)\n# Defining the kernel of size 3x3\nkernel = np.array(&#x5B;\n  &#x5B;0, -1, 0],\n  &#x5B;-1, 5, -1],\n  &#x5B;0, -1, 0]\n])\n\nresulting_image = cv2.filter2D(src_image, -1, kernel)\n\ncv2.imshow(&quot;original image&quot;, src_image)\ncv2.imshow(&quot;filter2d image&quot;, resulting_image)\ncv2.imwrite(&quot;Filter2d Sharpened Image.jpg&quot;, resulting_image)\ncv2.waitKey()\ncv2.destroyAllWindows()\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"367\" height=\"267\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Filter2d-Sharpened-Image.jpg\" alt=\"Filter2d Sharpened Image\" class=\"wp-image-22179\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Filter2d-Sharpened-Image.jpg 367w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Filter2d-Sharpened-Image-300x218.jpg 300w\" sizes=\"auto, (max-width: 367px) 100vw, 367px\" \/><figcaption>Filter2d Sharpened Image<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">2. Bluring an image<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; highlight: [6,7,8,9,10,12]; title: ; notranslate\" title=\"\">\nimport cv2\nimport numpy as np\n# Loading source image\nsrc_image = cv2.imread(&quot;pug-dog.jpg&quot;)\n# Defining the kernel of size 3x3\nkernel = np.array(&#x5B;\n  &#x5B;1, 1, 1],\n  &#x5B;1, 1, 1],\n  &#x5B;1, 1, 1]\n]) \/ 9\n\nresulting_image = cv2.filter2D(src_image, -1, kernel)\n\ncv2.imshow(&quot;original image&quot;, src_image)\ncv2.imshow(&quot;filter2d image&quot;, resulting_image)\ncv2.imwrite(&quot;Filter2d Blur Image.jpg&quot;, resulting_image)\ncv2.waitKey()\ncv2.destroyAllWindows()\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"367\" height=\"267\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Filter2d-Blur-Image.jpg\" alt=\"Filter2d Blur Image\" class=\"wp-image-22182\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Filter2d-Blur-Image.jpg 367w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Filter2d-Blur-Image-300x218.jpg 300w\" sizes=\"auto, (max-width: 367px) 100vw, 367px\" \/><figcaption>Filter2d Blur Image<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">3. Outline Edge Detection on an image<\/h3>\n\n\n\n<p>Let&#8217;s look at <a href=\"https:\/\/www.askpython.com\/python\/examples\/edge-detection-in-images\" data-type=\"post\" data-id=\"17372\">edge detection<\/a> with the OpenCV filter2D() function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; highlight: [6,7,8,9,10,12]; title: ; notranslate\" title=\"\">\nimport cv2\nimport numpy as np\n# Loading source image\nsrc_image = cv2.imread(&quot;pug-dog.jpg&quot;)\n# Defining the kernel of size 3x3\nkernel = np.array(&#x5B;\n  &#x5B;-1, -1, -1],\n  &#x5B;-1, 8, -1],\n  &#x5B;-1, -1, -1]\n])\n\nresulting_image = cv2.filter2D(src_image, -1, kernel)\n\ncv2.imshow(&quot;original image&quot;, src_image)\ncv2.imshow(&quot;filter2d image&quot;, resulting_image)\ncv2.imwrite(&quot;Filter2d Outline Image.jpg&quot;, resulting_image)\ncv2.waitKey()\ncv2.destroyAllWindows()\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"367\" height=\"267\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Filter2d-Outline-Image.jpg\" alt=\"Filter2d Outline Image\" class=\"wp-image-22181\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Filter2d-Outline-Image.jpg 367w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Filter2d-Outline-Image-300x218.jpg 300w\" sizes=\"auto, (max-width: 367px) 100vw, 367px\" \/><figcaption>Filter2d Outline Image<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Using Emboss Filter<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; highlight: [6,7,8,9,10,12]; title: ; notranslate\" title=\"\">\nimport cv2\nimport numpy as np\n# Loading source image\nsrc_image = cv2.imread(&quot;pug-dog.jpg&quot;)\n# Defining the Emboss kernel of size 3x3\nkernel = np.array(&#x5B;\n  &#x5B;-2, -1, 0],\n  &#x5B;-1, 1, 1],\n  &#x5B;0, 1, 2]\n])\n\nresulting_image = cv2.filter2D(src_image, -1, kernel)\n\ncv2.imshow(&quot;original image&quot;, src_image)\ncv2.imshow(&quot;filter2d image&quot;, resulting_image)\ncv2.imwrite(&quot;Filter2d Emboss Image.jpg&quot;, resulting_image)\ncv2.waitKey()\ncv2.destroyAllWindows()\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"367\" height=\"267\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Filter2d-Emboss-Image.jpg\" alt=\"Filter2d Emboss Image\" class=\"wp-image-22180\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Filter2d-Emboss-Image.jpg 367w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Filter2d-Emboss-Image-300x218.jpg 300w\" sizes=\"auto, (max-width: 367px) 100vw, 367px\" \/><figcaption>Filter2d Emboss Image<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Using Sobel Filter<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; highlight: [6,7,8,9,10,12]; title: ; notranslate\" title=\"\">\nimport cv2\nimport numpy as np\n# Loading source image\nsrc_image = cv2.imread(&quot;pug-dog.jpg&quot;)\n# Defining the Sobel kernel of size 3x3\nkernel = np.array(&#x5B;\n  &#x5B;-1, 0, 1],\n  &#x5B;-2, 0, 2],\n  &#x5B;-1, 0, 1]\n])\n\nresulting_image = cv2.filter2D(src_image, -1, kernel)\n\ncv2.imshow(&quot;original image&quot;, src_image)\ncv2.imshow(&quot;filter2d image&quot;, resulting_image)\ncv2.imwrite(&quot;Filter2d Sobel Image.jpg&quot;, resulting_image)\ncv2.waitKey()\ncv2.destroyAllWindows()\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"367\" height=\"267\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Filter2d-Sobel-Image.jpg\" alt=\"Filter2d Sobel Image\" class=\"wp-image-22183\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Filter2d-Sobel-Image.jpg 367w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Filter2d-Sobel-Image-300x218.jpg 300w\" sizes=\"auto, (max-width: 367px) 100vw, 367px\" \/><figcaption>Filter2d Sobel Image<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this tutorial, you learned about Convolution and kernels in image processing and how OpenCV filter2D() function is used in python to manipulate images. Now you can play around and try different kernel filters to get different image effects.<\/p>\n\n\n\n<p>Thanks for reading!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello everyone! In this tutorial, we will learn how to use OpenCV filter2D() method to apply filters on images such as sharpening, bluring and finding edges in the images. So lets get started. Also read: Read Images in Python using OpenCV Introduction to the OpenCV filter2D() function While dealing with images in Image Processing, filter2D() [&hellip;]<\/p>\n","protected":false},"author":31,"featured_media":22273,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-22177","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-modules"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/22177","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=22177"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/22177\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/22273"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=22177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=22177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=22177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}