{"id":2493,"date":"2017-01-23T08:14:29","date_gmt":"2017-01-23T13:14:29","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=2493"},"modified":"2019-11-01T17:04:39","modified_gmt":"2019-11-01T21:04:39","slug":"gaussian-filtering-with-imgaussfilt","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2017\/01\/23\/gaussian-filtering-with-imgaussfilt\/","title":{"rendered":"Gaussian filtering with imgaussfilt"},"content":{"rendered":"<div class=\"content\"><p>With the R2015a release a couple of years ago, the Image Processing Toolbox added the function <a href=\"https:\/\/www.mathworks.com\/help\/images\/ref\/imgaussfilt.html\"><tt>imgaussfilt<\/tt><\/a>. This function performs 2-D Gaussian filtering on images. It features a heuristic that automatically switches between a spatial-domain implementation and a frequency-domain implementation.<\/p><p>I wanted to check out the heuristic and see how well it works on my own computer (a 2015 MacBook Pro).<\/p><p>You can use <tt>imgaussfilt<\/tt> this way:<\/p><pre class=\"codeinput\">rgb = imread(<span class=\"string\">'peppers.png'<\/span>);\r\nsigma = 10;\r\nrgb_smoothed = imgaussfilt(rgb,sigma);\r\n\r\nimshow(rgb)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/imgaussfilt_timing_01.png\" alt=\"\"> <pre class=\"codeinput\">imshow(rgb_smoothed)\r\ntitle(<span class=\"string\">'Gaussian smoothed, \\sigma = 10'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/imgaussfilt_timing_02.png\" alt=\"\"> <p>With this call, <tt>imgaussfilt<\/tt> automatically chooses between a spatial-domain or a frequency-domain implementation. But you can also tell <tt>imgaussfilt<\/tt> which implementation to use.<\/p><pre class=\"codeinput\">rgb_smoothed_s = imgaussfilt(rgb,sigma,<span class=\"string\">'FilterDomain'<\/span>,<span class=\"string\">'spatial'<\/span>);\r\nrgb_smoothed_f = imgaussfilt(rgb,sigma,<span class=\"string\">'FilterDomain'<\/span>,<span class=\"string\">'frequency'<\/span>);\r\n<\/pre><p>I'll use this syntax, together with the <tt>timeit<\/tt> function, to get rough timing curves for the two implementation methods. The heuristic used by <tt>imgaussfilt<\/tt> uses a few different factors to decide, including image size, Gaussian kernel size, single or double precision, and the availability of processor-specific optimizations. For my computer, with a 2000-by-2000 image array, the cross-over point is at about $\\sigma = 50$. That is, <tt>imgaussfilt<\/tt> uses the spatial-domain method for $\\sigma &lt; 50$, and it uses a frequency-domain method for $\\sigma &gt; 50$.<\/p><p>Let's check that out with a set of $\\sigma$ values ranging from 5 to 200.<\/p><pre class=\"codeinput\">I = rand(2000,2000);\r\nww = 5:5:200;\r\n<span class=\"keyword\">for<\/span> k = 1:length(ww)\r\n    t_s(k) = timeit(@() imgaussfilt(I,ww(k),<span class=\"string\">'FilterDomain'<\/span>,<span class=\"string\">'spatial'<\/span>));\r\n<span class=\"keyword\">end<\/span>\r\n\r\n<span class=\"keyword\">for<\/span> k = 1:length(ww)\r\n    t_f(k) = timeit(@() imgaussfilt(I,ww(k),<span class=\"string\">'FilterDomain'<\/span>,<span class=\"string\">'frequency'<\/span>));\r\n<span class=\"keyword\">end<\/span>\r\n\r\nplot(ww,t_s)\r\nhold <span class=\"string\">on<\/span>\r\nplot(ww,t_f)\r\nhold <span class=\"string\">off<\/span>\r\nlegend(<span class=\"string\">'spatial'<\/span>,<span class=\"string\">'frequency'<\/span>)\r\nxlabel(<span class=\"string\">'\\sigma'<\/span>)\r\nylabel(<span class=\"string\">'time (s)'<\/span>)\r\ntitle(<span class=\"string\">'imgaussfilt - 2000x2000 image'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/imgaussfilt_timing_03.png\" alt=\"\"> <p>As you can see, the cross-over point of the timing curves is about the same as the threshold used by <tt>imgaussfilt<\/tt>. As computing hardware and optimized computation libraries evolve over time, though, the ideal cross-over point might change. The Image Processing Toolbox team will need to check this every few years and adjust the heuristic as needed.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_a8e0fa0a1801485fb08e2add318cf07e() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='a8e0fa0a1801485fb08e2add318cf07e ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' a8e0fa0a1801485fb08e2add318cf07e';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2017 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_a8e0fa0a1801485fb08e2add318cf07e()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2016b<br><\/p><\/div><!--\r\na8e0fa0a1801485fb08e2add318cf07e ##### SOURCE BEGIN #####\r\n%%\r\n% With the R2015a release a couple of years ago, the Image\r\n% Processing Toolbox added the function <https:\/\/www.mathworks.com\/help\/images\/ref\/imgaussfilt.html \r\n% |imgaussfilt|>. This function performs 2-D Gaussian filtering on\r\n% images. It features a heuristic that automatically switches\r\n% between a spatial-domain implementation and a frequency-domain\r\n% implementation.\r\n%\r\n% I wanted to check out the heuristic and see how well it works on\r\n% my own computer (a 2015 MacBook Pro).\r\n%\r\n% You can use |imgaussfilt| this way:\r\n\r\nrgb = imread('peppers.png');\r\nsigma = 10;\r\nrgb_smoothed = imgaussfilt(rgb,sigma);\r\n\r\nimshow(rgb)\r\n\r\n%%\r\n\r\nimshow(rgb_smoothed)\r\ntitle('Gaussian smoothed, \\sigma = 10')\r\n\r\n%%\r\n% With this call, |imgaussfilt| automatically chooses between a\r\n% spatial-domain or a frequency-domain implementation. But you can\r\n% also tell |imgaussfilt| which implementation to use.\r\n\r\nrgb_smoothed_s = imgaussfilt(rgb,sigma,'FilterDomain','spatial');\r\nrgb_smoothed_f = imgaussfilt(rgb,sigma,'FilterDomain','frequency');\r\n\r\n%%\r\n% I'll use this syntax, together with the |timeit| function, to get\r\n% rough timing curves for the two implementation methods.\r\n% The heuristic used by |imgaussfilt| uses a few different factors\r\n% to decide, including image size, Gaussian kernel size, single or\r\n% double precision, and the availability of processor-specific\r\n% optimizations. For my computer, with a 2000-by-2000 image array,\r\n% the cross-over point is at about $\\sigma = 50$. That is,\r\n% |imgaussfilt| uses the spatial-domain method for $\\sigma < 50$,\r\n% and it uses a frequency-domain method for $\\sigma > 50$.\r\n%\r\n% Let's check that out with a set of $\\sigma$ values ranging from 5\r\n% to 200.\r\n\r\nI = rand(2000,2000);\r\nww = 5:5:200;\r\nfor k = 1:length(ww)\r\n    t_s(k) = timeit(@() imgaussfilt(I,ww(k),'FilterDomain','spatial'));\r\nend\r\n\r\nfor k = 1:length(ww)\r\n    t_f(k) = timeit(@() imgaussfilt(I,ww(k),'FilterDomain','frequency'));\r\nend\r\n\r\nplot(ww,t_s)\r\nhold on\r\nplot(ww,t_f)\r\nhold off\r\nlegend('spatial','frequency')\r\nxlabel('\\sigma')\r\nylabel('time (s)')\r\ntitle('imgaussfilt - 2000x2000 image')\r\n\r\n%%\r\n% As you can see, the cross-over point of the timing curves is about\r\n% the same as the threshold used by |imgaussfilt|. As computing\r\n% hardware and optimized computation libraries evolve over time,\r\n% though, the ideal cross-over point might change. The Image\r\n% Processing Toolbox team will need to check this every few years\r\n% and adjust the heuristic as needed.\r\n\r\n##### SOURCE END ##### a8e0fa0a1801485fb08e2add318cf07e\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/steve\/files\/imgaussfilt_timing_03.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><p>With the R2015a release a couple of years ago, the Image Processing Toolbox added the function imgaussfilt. This function performs 2-D Gaussian filtering on images. It features a heuristic that... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2017\/01\/23\/gaussian-filtering-with-imgaussfilt\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":2497,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[90,1181,76,36,92,705,68,460,474,52,94,96],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/2493"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/comments?post=2493"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/2493\/revisions"}],"predecessor-version":[{"id":2494,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/2493\/revisions\/2494"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media\/2497"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=2493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=2493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=2493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}