{"id":2449,"date":"2017-01-09T07:00:43","date_gmt":"2017-01-09T12:00:43","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=2449"},"modified":"2019-11-01T16:51:25","modified_gmt":"2019-11-01T20:51:25","slug":"aliasing-and-image-resizing-part-2","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2017\/01\/09\/aliasing-and-image-resizing-part-2\/","title":{"rendered":"Aliasing and image resizing &#8211; part 2"},"content":{"rendered":"<div class=\"content\"><p>In my <a href=\"https:\/\/blogs.mathworks.com\/steve\/2017\/01\/03\/aliasing-and-image-resizing-part-1\/\">03-Jan-2017 post<\/a>, I introduced my new topic: <i>aliasing<\/i>, especially as it relates to resizing images. Today I will get more specific about what aliasing actually is.<\/p><p>I'll use a 1-D sequence to illustrate. Here is part of a 1-D sequence with a period of 8.<\/p><pre class=\"codeinput\">a = repmat([1 1 1 1 0 0 0 0],1,9)\r\n<\/pre><pre class=\"codeoutput\">\r\na =\r\n\r\n  Columns 1 through 13\r\n\r\n     1     1     1     1     0     0     0     0     1     1     1     1     0\r\n\r\n  Columns 14 through 26\r\n\r\n     0     0     0     1     1     1     1     0     0     0     0     1     1\r\n\r\n  Columns 27 through 39\r\n\r\n     1     1     0     0     0     0     1     1     1     1     0     0     0\r\n\r\n  Columns 40 through 52\r\n\r\n     0     1     1     1     1     0     0     0     0     1     1     1     1\r\n\r\n  Columns 53 through 65\r\n\r\n     0     0     0     0     1     1     1     1     0     0     0     0     1\r\n\r\n  Columns 66 through 72\r\n\r\n     1     1     1     0     0     0     0\r\n\r\n<\/pre><p>Now let's \"shrink\" the sequence by sampling it with a spacing that is greater than 1, using nearest-neighbor interpolation. First, let's try a spacing of 4\/3.<\/p><pre class=\"codeinput\">f = 4\/3;\r\na_1 = a(round(1:f:end))\r\n<\/pre><pre class=\"codeoutput\">\r\na_1 =\r\n\r\n  Columns 1 through 13\r\n\r\n     1     1     1     0     0     0     1     1     1     0     0     0     1\r\n\r\n  Columns 14 through 26\r\n\r\n     1     1     0     0     0     1     1     1     0     0     0     1     1\r\n\r\n  Columns 27 through 39\r\n\r\n     1     0     0     0     1     1     1     0     0     0     1     1     1\r\n\r\n  Columns 40 through 52\r\n\r\n     0     0     0     1     1     1     0     0     0     1     1     1     0\r\n\r\n  Columns 53 through 54\r\n\r\n     0     0\r\n\r\n<\/pre><p>The shrunken sequence has a period of 6. That's consistent with the original sequence period and the shrink factor: $8\/(4\/3) = 6$.<\/p><p>Now let's increase the shrink factor to 1.6.<\/p><pre class=\"codeinput\">f = 1.6;\r\na_2 = a(round(1:f:end))\r\n<\/pre><pre class=\"codeoutput\">\r\na_2 =\r\n\r\n  Columns 1 through 13\r\n\r\n     1     1     1     0     0     1     1     1     0     0     1     1     1\r\n\r\n  Columns 14 through 26\r\n\r\n     0     0     1     1     1     0     0     1     1     1     0     0     1\r\n\r\n  Columns 27 through 39\r\n\r\n     1     1     0     0     1     1     1     0     0     1     1     1     0\r\n\r\n  Columns 40 through 45\r\n\r\n     0     1     1     1     0     0\r\n\r\n<\/pre><p>The sequence <tt>a_2<\/tt> has a period of 5. Again, that is consistent with the original sequence period and shrink factor: $8\/1.6 = 5$.<\/p><p>Let's bump the shrink factor up to 4.<\/p><pre class=\"codeinput\">f = 4;\r\na_3 = a(round(1:f:end))\r\n<\/pre><pre class=\"codeoutput\">\r\na_3 =\r\n\r\n  Columns 1 through 13\r\n\r\n     1     0     1     0     1     0     1     0     1     0     1     0     1\r\n\r\n  Columns 14 through 18\r\n\r\n     0     1     0     1     0\r\n\r\n<\/pre><p>The sequence <tt>a_4<\/tt> has a period of 2, which is still understandably straightforward: $8\/4 = 2$. Note that 2 is the shortest period possible for a nonconstant periodic discrete sequence.<\/p><p>It gets more fun (and less straightforward) when the shrink factor goes above 4.<\/p><pre class=\"codeinput\">f = 6;\r\na_4 = a(round(1:f:end))\r\n<\/pre><pre class=\"codeoutput\">\r\na_4 =\r\n\r\n     1     0     0     1     1     0     0     1     1     0     0     1\r\n\r\n<\/pre><p>Now the shrunken sequence's periodicity appears to be 5, and that's harder to understand because $8\/6 \\neq 5$.<\/p><p>To recap what we've seen so far: the period of the shrunken sequence decreased steadily as we increased the shrink factor, but only up to a point. Once the shrink factor increased above 4 (half the original period), the period of the shrunken sequence stopped decreasing and starting changing in a different fashion.<\/p><p>This is an example of what signal processing folk call <i>aliasing<\/i>. The term is used in the sense of one frequency masquerading as another.<\/p><p>This happens when the shrink factor becomes too high to preserve the period of the original sequence.<\/p><p>Now let me illustrate aliasing using a zone plate image. (You can get imzoneplate from the <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/35961-zone-plate-test-image\/\">MATLAB Central File Exchange<\/a>.)<\/p><pre class=\"codeinput\">Z = imzoneplate(501);\r\nimshow(Z)\r\ntitle(<span class=\"string\">'Z'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/antialiasing_2_01.png\" alt=\"\"> <pre class=\"codeinput\">Z4 = Z(1:4:end,1:4:end);\r\nimshow(Z4)\r\ntitle(<span class=\"string\">'Z4'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/antialiasing_2_02.png\" alt=\"\"> <p>That result hardly looks anything like the original image. The visual artifacts are caused by aliasing. The original image has high spatial frequencies near the boundaries that can't be represented when we shrink the image. Those frequencies, instead of just disappearing, get \"aliased\" into a different set of spatial frequencies.<\/p><p>In general, aliasing distortion can't be removed once it has happened. Instead, antialiasing techniques must be applied as part of the image resizing process. Next time, I'll discuss the particular antialiasing procedure used by <tt>imresize<\/tt>.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_fa20e6797a7f4ffd94ac06a02c720d60() {\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='fa20e6797a7f4ffd94ac06a02c720d60 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' fa20e6797a7f4ffd94ac06a02c720d60';\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_fa20e6797a7f4ffd94ac06a02c720d60()\"><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\nfa20e6797a7f4ffd94ac06a02c720d60 ##### SOURCE BEGIN #####\r\n%%\r\n% In my <https:\/\/blogs.mathworks.com\/steve\/2017\/01\/03\/aliasing-and-image-resizing-part-1\/ \r\n% 03-Jan-2017 post>, I introduced my new topic: _aliasing_, especially as\r\n% it relates to resizing images. Today I will get more specific about what\r\n% aliasing actually is.\r\n%\r\n% I'll use a 1-D sequence to illustrate. Here is part of a 1-D sequence\r\n% with a period of 8.\r\n\r\na = repmat([1 1 1 1 0 0 0 0],1,9)\r\n\r\n%%\r\n% Now let's \"shrink\" the sequence by sampling it with a spacing that is\r\n% greater than 1, using nearest-neighbor interpolation. First, let's try a\r\n% spacing of 4\/3.\r\n\r\nf = 4\/3;\r\na_1 = a(round(1:f:end))\r\n\r\n%%\r\n% The shrunken sequence has a period of 6. That's consistent with the\r\n% original sequence period and the shrink factor: $8\/(4\/3) = 6$.\r\n%\r\n% Now let's increase the shrink factor to 1.6.\r\n\r\nf = 1.6;\r\na_2 = a(round(1:f:end))\r\n\r\n%%\r\n% The sequence |a_2| has a period of 5. Again, that is consistent with the\r\n% original sequence period and shrink factor: $8\/1.6 = 5$.\r\n%\r\n% Let's bump the shrink factor up to 4.\r\n\r\nf = 4;\r\na_3 = a(round(1:f:end))\r\n\r\n%%\r\n% The sequence |a_4| has a period of 2, which is still understandably\r\n% straightforward: $8\/4 = 2$. Note that 2 is the shortest period possible\r\n% for a nonconstant periodic discrete sequence.\r\n%\r\n% It gets more fun (and less straightforward) when the shrink factor goes\r\n% above 4.\r\n\r\nf = 6;\r\na_4 = a(round(1:f:end))\r\n\r\n%%\r\n% Now the shrunken sequence's periodicity appears to be 5, and that's\r\n% harder to understand because $8\/6 \\neq 5$.\r\n%\r\n% To recap what we've seen so far: the period of the shrunken sequence\r\n% decreased steadily as we increased the shrink factor, but only up to a\r\n% point. Once the shrink factor increased above 4 (half the original\r\n% period), the period of the shrunken sequence stopped decreasing and\r\n% starting changing in a different fashion.\r\n%\r\n% This is an example of what signal processing folk call _aliasing_. The\r\n% term is used in the sense of one frequency masquerading as another.\r\n%\r\n% This happens when the shrink factor becomes too high to preserve the\r\n% period of the original sequence.\r\n%\r\n% Now let me illustrate aliasing using a zone plate image. (You can get\r\n% imzoneplate from the <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/35961-zone-plate-test-image\/ \r\n% MATLAB Central File Exchange>.)\r\n\r\nZ = imzoneplate(501);\r\nimshow(Z)\r\ntitle('Z')\r\n\r\n%%\r\nZ4 = Z(1:4:end,1:4:end);\r\nimshow(Z4)\r\ntitle('Z4')\r\n\r\n%%\r\n% That result hardly looks anything like the original image. The visual\r\n% artifacts are caused by aliasing. The original image has high spatial\r\n% frequencies near the boundaries that can't be represented when we shrink\r\n% the image. Those frequencies, instead of just disappearing, get \"aliased\"\r\n% into a different set of spatial frequencies.\r\n%\r\n% In general, aliasing distortion can't be removed once it has happened.\r\n% Instead, antialiasing techniques must be applied as part of the image\r\n% resizing process. Next time, I'll discuss the particular antialiasing\r\n% procedure used by |imresize|.\r\n##### SOURCE END ##### fa20e6797a7f4ffd94ac06a02c720d60\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/antialiasing_2_02.png\" onError=\"this.style.display ='none';\" \/><\/div><p>In my 03-Jan-2017 post, I introduced my new topic: aliasing, especially as it relates to resizing images. Today I will get more specific about what aliasing actually is.I'll use a 1-D sequence to... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2017\/01\/09\/aliasing-and-image-resizing-part-2\/\">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":[156,36,116,188,52],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/2449"}],"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=2449"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/2449\/revisions"}],"predecessor-version":[{"id":2450,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/2449\/revisions\/2450"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=2449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=2449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=2449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}