{"id":143,"date":"2007-06-15T13:11:16","date_gmt":"2007-06-15T17:11:16","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2007\/06\/15\/border-replication-option-of-imresize\/"},"modified":"2019-10-23T12:58:56","modified_gmt":"2019-10-23T16:58:56","slug":"border-replication-option-of-imresize","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2007\/06\/15\/border-replication-option-of-imresize\/","title":{"rendered":"Border replication option of imfilter"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>Loren wrote about the <a href=\"https:\/\/blogs.mathworks.com\/loren\/2007\/04\/05\/2007-matlab-world-tour\/\">2007 MATLAB World Tour<\/a> in her <a href=\"https:\/\/blogs.mathworks.com\/loren\/\">Art of MATLAB<\/a> blog recently. Several other MathWorks developers also attended some of the events.\r\n   <\/p>\r\n   <p>One message that they brought back to us is that many of the good features we put into our products simply have not yet been\r\n      discovered by many of our users. That has inspired me to think about a new kind of post on this blog - I think of it as the\r\n      \"Didja Know?\" post. (Many of the <a href=\"https:\/\/blogs.mathworks.com\/community\/\">MATLAB Desktop<\/a> blog posts are Didja Know? posts.)  In my Didja Know? posts, I'll try to highlight useful functions or function options that\r\n      you might not have discovered yet.\r\n   <\/p>\r\n   <p>First up: the border replication option of <a href=\"https:\/\/www.mathworks.com\/help\/images\/ref\/imfilter.html\"><tt>imfilter<\/tt><\/a>.\r\n   <\/p>\r\n   <p>When you perform convolution on an image, one question to consider is: How do you compute the convolution sum for pixels along\r\n      the image border? By default, <tt>imfilter<\/tt> assumes that image pixels \"outside\" the image are zero.  Mathematically this is kind of reasonable, but it tends to produce\r\n      a dark strip along the edge of the output image.  Here's an example to show what I mean:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">I = imread(<span style=\"color: #A020F0\">'cameraman.tif'<\/span>);\r\nimshow(I)\r\nimcredit(<span style=\"color: #A020F0\">'Image courtesy of MIT'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/143\/imfilter_replicate_01.jpg\"> <pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">J = imfilter(I, ones(5,5)\/25);\r\nimshow(J)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/143\/imfilter_replicate_02.jpg\"> <p>But you can tell <tt>imfilter<\/tt> to handle image borders by replicating the border pixel values.  That often produces a more desirable result:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">K = imfilter(I, ones(5,5)\/25, <span style=\"color: #A020F0\">'replicate'<\/span>);\r\nimshow(K)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/143\/imfilter_replicate_03.jpg\"> <p>There are other border options as well.  See the <a href=\"https:\/\/www.mathworks.com\/help\/images\/ref\/imfilter.html\"><tt>imfilter<\/tt><\/a> documentation for more information.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_c66e4a9ea28e402c8f49fd4c53437663() {\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='c66e4a9ea28e402c8f49fd4c53437663 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' c66e4a9ea28e402c8f49fd4c53437663';\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        author = 'Steve Eddins';\r\n        copyright = 'Copyright 2007 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 author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\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      \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_c66e4a9ea28e402c8f49fd4c53437663()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.4<br><\/p>\r\n<\/div>\r\n<!--\r\nc66e4a9ea28e402c8f49fd4c53437663 ##### SOURCE BEGIN #####\r\n%%\r\n% Loren wrote about the \r\n% <https:\/\/blogs.mathworks.com\/loren\/2007\/04\/05\/2007-matlab-world-tour\/\r\n% 2007 MATLAB World Tour> in her \r\n% <https:\/\/blogs.mathworks.com\/loren\/ Art of MATLAB> blog recently.\r\n% Several other MathWorks developers also attended some of the events.\r\n%\r\n% One message that they brought back to us is that many of the good\r\n% features we put into our products simply have not yet been discovered by\r\n% many of our users. That has inspired me to think about a new kind of post\r\n% on this blog - I think of it as the \"Didja Know?\" post. (Many of the\r\n% <https:\/\/blogs.mathworks.com\/community\/ MATLAB Desktop> blog posts are \r\n% Didja Know? posts.)  In my Didja Know? posts, I'll try to highlight\r\n% useful functions or function options that you might not have discovered\r\n% yet.\r\n%\r\n% First up: the border replication option of \r\n% <https:\/\/www.mathworks.com\/help\/images\/ref\/imfilter.html\r\n% |imfilter|>.\r\n%\r\n% When you perform convolution on an image, one question to consider is:\r\n% How do you compute the convolution sum for pixels along the image border?  \r\n% By default, |imfilter| assumes that image pixels \"outside\" the image are\r\n% zero.  Mathematically this is kind of reasonable, but it tends to produce\r\n% a dark strip along the edge of the output image.  Here's an example to\r\n% show what I mean:\r\n\r\nI = imread('cameraman.tif');\r\nimshow(I)\r\nimcredit('Image courtesy of MIT')\r\n\r\n%%\r\n\r\nJ = imfilter(I, ones(5,5)\/25);\r\nimshow(J)\r\n\r\n%%\r\n% But you can tell |imfilter| to handle image borders by replicating the\r\n% border pixel values.  That often produces a more desirable result:\r\n\r\nK = imfilter(I, ones(5,5)\/25, 'replicate');\r\nimshow(K)\r\n\r\n%%\r\n% There are other border options as well.  See the \r\n% <https:\/\/www.mathworks.com\/help\/images\/ref\/imfilter.html\r\n% |imfilter|> documentation\r\n% for more information.\r\n##### SOURCE END ##### c66e4a9ea28e402c8f49fd4c53437663\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Loren wrote about the 2007 MATLAB World Tour in her Art of MATLAB blog recently. Several other MathWorks developers also attended some of the events.\r\n   \r\n   One message that they brought back... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2007\/06\/15\/border-replication-option-of-imresize\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[370,76,36,288],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/143"}],"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=143"}],"version-history":[{"count":4,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/143\/revisions"}],"predecessor-version":[{"id":3540,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/143\/revisions\/3540"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}