{"id":22026,"date":"2021-09-30T19:07:00","date_gmt":"2021-09-30T19:07:00","guid":{"rendered":"https:\/\/www.askpython.com\/?p=22026"},"modified":"2021-10-01T19:07:52","modified_gmt":"2021-10-01T19:07:52","slug":"denoising-images-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/denoising-images-in-python","title":{"rendered":"Denoising Images in Python &#8211; A Step-By-Step Guide"},"content":{"rendered":"\n<p>In this tutorial, we have used a machine-learning algorithm to denoise a noisy image by making use of Python as the programming language.<\/p>\n\n\n\n<p>Let&#8217;s get straight to what image denoising is and how to implement the same in the coming sections.<\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python\/visualizing-colors-in-images\" data-type=\"post\" data-id=\"21882\">Visualizing Colors In Images Using Histograms \u2013 Python OpenCV<\/a><\/em><\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to Image Denoising<\/h2>\n\n\n\n<p>Demand for more precise and aesthetically attractive photographs is rising as digital photography explodes. Modern cameras, on the other hand, produce images that are tainted by noise, resulting in poor visual quality. <\/p>\n\n\n\n<p>As a result, efforts must be made to minimize noise without sacrificing image quality (edges, corners, and other sharp structures).<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"270\" height=\"150\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/image_denoise_sample.png\" alt=\"Image Denoise Sample\" class=\"wp-image-22029\"\/><figcaption>Image Denoise Sample<\/figcaption><\/figure><\/div>\n\n\n\n<p><strong><em><span style=\"text-decoration: underline\">Image denoising<\/span><\/em><\/strong> refers to the process of removing noise from a noisy image in order to recover the original image. <\/p>\n\n\n\n<p>However, because of some components like noise, edges, and texture which is difficult to differentiate them throughout the denoising process and the denoised pictures may unavoidably lose some features. <\/p>\n\n\n\n<p>The recovery of useful information from noisy pictures during noise reduction to create high-quality photographs has become a significant issue in recent years.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Denoising Images in Python &#8211; Implementation<\/h2>\n\n\n\n<p>Now that we have got an introduction to Image Denoising, let us move to the implementation step by step. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Importing Modules<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nimport cv2\nimport numpy as np\nfrom matplotlib import pyplot as plt\nplt.style.use(&#039;seaborn&#039;)\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. Loading the Image <\/h3>\n\n\n\n<p>In order to load the image into the program, we are going to use <code>imread<\/code> function. The code for the same is shown below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimage = cv2.imread(&#039;sample_denoise_input.jpg&#039;)\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. Applying Denoising functions of OpenCV<\/h3>\n\n\n\n<p>There are multiple denoising functions present in the OpenCV library which are listed below:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>S.no.<\/strong><\/td><td><strong>Function Name<\/strong><\/td><td><strong>Description<\/strong><\/td><\/tr><tr><td><strong>1<\/strong><\/td><td>cv2.fastNlMeansDenoising()<\/td><td>Works for single Grayscale Image<\/td><\/tr><tr><td><strong>2<\/strong><\/td><td>cv2.fastNlMeansDenoisingColored()<\/td><td>Works for Colored Image<\/td><\/tr><tr><td><strong>3<\/strong><\/td><td>cv2.fastNlMeansDenoisingMulti()&nbsp;<\/td><td>Works for a sequence of Grayscale Image<\/td><\/tr><tr><td><strong>4<\/strong><\/td><td>cv2.fastNlMeansDenoisingColoredMulti()<\/td><td>Works for a sequence of Colored Image<\/td><\/tr><\/tbody><\/table><figcaption>De-noising Techniques &#8211; OpenCV<\/figcaption><\/figure>\n\n\n\n<p>Here in this tutorial, we will be loading a single colored image so we would the second function. The code for the same is shown below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndst = cv2.fastNlMeansDenoisingColored(image, None, 11, 6, 7, 21)\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4.  Plotting the Original and Denoised Image<\/h3>\n\n\n\n<p>Now that the image is denoised, its time to plot the original and denoised image using <code>subplots<\/code> which can be achieved through the code mentioned below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nrow, col = 1, 2\nfig, axs = plt.subplots(row, col, figsize=(15, 10))\nfig.tight_layout()\naxs&#x5B;0].imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))\naxs&#x5B;0].set_title(&#039;Elephant&#039;)\naxs&#x5B;1].imshow(cv2.cvtColor(dst, cv2.COLOR_BGR2RGB))\naxs&#x5B;1].set_title(&#039;Fast Means Denoising&#039;)\nplt.show()\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Complete Code for Denoising Images<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nimport cv2\nimport numpy as np\nfrom matplotlib import pyplot as plt\nplt.style.use(&#039;seaborn&#039;)\n\nimage = cv2.imread(&#039;sample_denoise_input.jpg&#039;)\ndst = cv2.fastNlMeansDenoisingColored(image, None, 11, 6, 7, 21)\n\nrow, col = 1, 2\nfig, axs = plt.subplots(row, col, figsize=(15, 10))\nfig.tight_layout()\naxs&#x5B;0].imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))\naxs&#x5B;0].set_title(&#039;Elephant&#039;)\naxs&#x5B;1].imshow(cv2.cvtColor(dst, cv2.COLOR_BGR2RGB))\naxs&#x5B;1].set_title(&#039;Fast Means Denoising&#039;)\nplt.show()\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Some Sample Outputs<\/h2>\n\n\n\n<p>Now, let&#8217;s look at some sample outputs for the code just mentioned above.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1068\" height=\"540\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/final_output_denoising_image.png\" alt=\"Final Output Denoising Image\" class=\"wp-image-22054\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/final_output_denoising_image.png 1068w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/final_output_denoising_image-300x152.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/final_output_denoising_image-1024x518.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/final_output_denoising_image-768x388.png 768w\" sizes=\"auto, (max-width: 1068px) 100vw, 1068px\" \/><figcaption>Final Output Denoising Image<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1068\" height=\"649\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/final_output_2_denoising_image.png\" alt=\"Final Output 2 Denoising Image\" class=\"wp-image-22056\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/final_output_2_denoising_image.png 1068w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/final_output_2_denoising_image-300x182.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/final_output_2_denoising_image-1024x622.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/final_output_2_denoising_image-768x467.png 768w\" sizes=\"auto, (max-width: 1068px) 100vw, 1068px\" \/><figcaption>Final Output 2 Denoising Image<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1068\" height=\"540\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/final_output_3_denoising_image.png\" alt=\"Final Output 3 Denoising Image\" class=\"wp-image-22058\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/final_output_3_denoising_image.png 1068w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/final_output_3_denoising_image-300x152.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/final_output_3_denoising_image-1024x518.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/final_output_3_denoising_image-768x388.png 768w\" sizes=\"auto, (max-width: 1068px) 100vw, 1068px\" \/><figcaption>Final Output 3 Denoising Image<\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>I hope you understood the concept and loved the outputs. Try out the same with more images and watch the magic happening on your screen!<\/p>\n\n\n\n<p>Happy Coding! &#x1f607;<\/p>\n\n\n\n<p>Want to learn more? Check out the tutorials mentioned below:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><a href=\"https:\/\/www.askpython.com\/python\/examples\/filters-to-images\">Python and OpenCV: Apply Filters to Images<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/examples\/orb-feature-detection\">ORB Feature Detection in Python<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/examples\/color-detection\">Color Detection using Python \u2013 Beginner\u2019s Reference<\/a><\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we have used a machine-learning algorithm to denoise a noisy image by making use of Python as the programming language. Let&#8217;s get straight to what image denoising is and how to implement the same in the coming sections. Also read: Visualizing Colors In Images Using Histograms \u2013 Python OpenCV Introduction to Image [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":22063,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-22026","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/22026","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\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=22026"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/22026\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/22063"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=22026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=22026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=22026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}