{"id":382,"date":"2011-07-26T13:25:58","date_gmt":"2011-07-26T17:25:58","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2011\/07\/26\/checkerboard-fun\/"},"modified":"2019-10-29T16:49:52","modified_gmt":"2019-10-29T20:49:52","slug":"checkerboard-fun","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2011\/07\/26\/checkerboard-fun\/","title":{"rendered":"Checkerboard fun"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>Thinking about test images (<a href=\"https:\/\/blogs.mathworks.com\/steve\/2006\/01\/13\/synthesizing-images-using-simple-equations\/\">13-Jan-2006<\/a>, <a href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/07\/16\/jahne-test-pattern\/\">16-Jul-2011<\/a>, <a href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/07\/19\/jahne-test-pattern-take-2\/\">19-Jul-2011<\/a>, <a href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/07\/19\/jahne-test-pattern-take-3\/\">19-Jul-2011<\/a>) recently prompted me to take a look at the Image Processing Toolbox function <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/images\/ref\/checkerboard.html\"><tt>checkerboard<\/tt><\/a>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">I = checkerboard;\r\nimshow(I)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/checkerboard_ideas_01.png\"> <p>The implementation of <tt>checkerboard<\/tt> creates a four-by-four set of squares and then uses <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/repmat.html\"><tt>repmat<\/tt><\/a>, something like this:\r\n   <\/p><pre>   black = zeros(m);\r\n   white = ones(m);\r\n   tile = [black white; white black];\r\n   I = repmat(tile,p,q);<\/pre><p>(Then there's a postprocessing step to turn the white squares into gray squares on the right half of the image.)<\/p>\r\n   <p>This works just fine, but it occurred to me that it's a little inconvenient if you want an image with a size that's not a\r\n      multiple of the square size. I had an idea for a different implementation of the checkerboard test pattern, one that is based\r\n      on the function <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/checkerboard_ideas_eq70329.png\">  where <i>n<\/i> is an integer. This function bounces back and forth between -1 and 1.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">n = 0:10;\r\n(-1).^n<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n     1    -1     1    -1     1    -1     1    -1     1    -1     1\r\n\r\n<\/pre><p>You can do this in two dimensions, also, with a function like <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/checkerboard_ideas_eq05830.png\"> .\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[n1, n2] = ndgrid(0:4);\r\n(-1).^(n1 + n2)<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n     1    -1     1    -1     1\r\n    -1     1    -1     1    -1\r\n     1    -1     1    -1     1\r\n    -1     1    -1     1    -1\r\n     1    -1     1    -1     1\r\n\r\n<\/pre><p>To hurt your eyes a little, make a bigger version of this and display it as an image.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[n1, n2] = ndgrid(0:199);\r\nI = (-1).^(n1 + n2);\r\nimshow(I, [-1 1])<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/checkerboard_ideas_02.png\"> <p>This is like a checkerboard but with extra tiny squares. We can make the squares bigger by using some scaling and rounding\r\n      of the <tt>n1<\/tt> and <tt>n2<\/tt> values.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">I = (-1).^(round(n1\/20) + round(n2\/20));\r\nimshow(I, [-1 1])<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/checkerboard_ideas_03.png\"> <p>Hmm, that resulted in half-tiles around the edges. Let's try <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/floor.html\"><tt>floor<\/tt><\/a> instead of <tt>round<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">I = (-1).^(floor(n1\/20) + floor(n2\/20));\r\nimshow(I, [-1 1])<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/checkerboard_ideas_04.png\"> <p>Now back to my observation above about the relationship between the size of the image and the number of squares. The implementation\r\n      based on <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/checkerboard_ideas_eq05830.png\">  makes it very easy to specify independently the image height, image width, and square size. You can even turn the squares\r\n      into rectangles.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">image_height = 206;\r\nimage_width = 97;\r\n\r\nsquare_height = 25;\r\nsquare_width = 11;  <span style=\"color: #228B22\">% I know, I know; it's not a \"square\". It's a joke, OK?<\/span>\r\n\r\n[n1, n2] = ndgrid(1:image_height, 1:image_width);\r\nI = (-1).^(floor(n1\/square_height) + floor(n2\/square_width));\r\n<span style=\"color: #228B22\">% Make it a binary image of 0s and 1s instead of -1s and 1s.<\/span>\r\nBW = I == 1;\r\nimshow(BW)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/checkerboard_ideas_05.png\"> <p>Last week's fun with the Jahne test pattern gave me the notion of making a checkboard image with spatially-varying frequency.\r\n      The technique is the same - square and scale the exponent terms so that the frequency rises as you move away from the center\r\n      of the image and reaches a maximum at the desired location. (And I'm going to switch back to using <tt>round<\/tt>.)\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">square_size = 20;\r\n[n1, n2] = ndgrid(-100:100);\r\nI = (-1).^(round(n1.^2\/1000) + round(n2.^2\/1000));\r\nBW = I == 1;\r\nimshow(BW)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/checkerboard_ideas_06.png\"> <p>I love MATLAB!<\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_fe6202bb167a45829f04af5537759819() {\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='fe6202bb167a45829f04af5537759819 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' fe6202bb167a45829f04af5537759819';\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 2011 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_fe6202bb167a45829f04af5537759819()\"><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.12<br><\/p>\r\n<\/div>\r\n<!--\r\nfe6202bb167a45829f04af5537759819 ##### SOURCE BEGIN #####\r\n%%\r\n% Thinking about test images\r\n% (<https:\/\/blogs.mathworks.com\/steve\/2006\/01\/13\/synthesizing-images-using-simple-equations\/\r\n% 13-Jan-2006>,\r\n% <https:\/\/blogs.mathworks.com\/steve\/2011\/07\/16\/jahne-test-pattern\/\r\n% 16-Jul-2011>,\r\n% <https:\/\/blogs.mathworks.com\/steve\/2011\/07\/19\/jahne-test-pattern-take-2\/\r\n% 19-Jul-2011>,\r\n% <https:\/\/blogs.mathworks.com\/steve\/2011\/07\/19\/jahne-test-pattern-take-3\/\r\n% 19-Jul-2011>) recently prompted me to take a look at the Image Processing\r\n% Toolbox function\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/images\/ref\/checkerboard.html\r\n% |checkerboard|>.\r\n\r\nI = checkerboard;\r\nimshow(I)\r\n\r\n%%\r\n% The implementation of |checkerboard| creates a four-by-four set of\r\n% squares and then uses\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/repmat.html |repmat|>,\r\n% something like this:\r\n% \r\n%     black = zeros(m);\r\n%     white = ones(m);\r\n%     tile = [black white; white black];\r\n%     I = repmat(tile,p,q);\r\n%\r\n% (Then there's a postprocessing step to turn the white squares into gray\r\n% squares on the right half of the image.)\r\n%\r\n% This works just fine, but it occurred to me that it's a little\r\n% inconvenient if you want an image with a size that's not a multiple of\r\n% the square size. I had an idea for a different implementation of the\r\n% checkerboard test pattern, one that is based on the function $(-1)^n$\r\n% where _n_ is an integer. This function bounces back and forth between -1\r\n% and 1.\r\n\r\nn = 0:10;\r\n(-1).^n\r\n\r\n%%\r\n% You can do this in two dimensions, also, with a function like $(-1)^{(n_1\r\n% + n_2)}$.\r\n\r\n[n1, n2] = ndgrid(0:4);\r\n(-1).^(n1 + n2)\r\n\r\n%%\r\n% To hurt your eyes a little, make a bigger version of this and display it\r\n% as an image.\r\n\r\n[n1, n2] = ndgrid(0:199);\r\nI = (-1).^(n1 + n2);\r\nimshow(I, [-1 1])\r\n\r\n%%\r\n% This is like a checkerboard but with extra tiny squares. We can make the\r\n% squares bigger by using some scaling and rounding of the |n1| and |n2|\r\n% values.\r\n\r\nI = (-1).^(round(n1\/20) + round(n2\/20));\r\nimshow(I, [-1 1])\r\n\r\n%%\r\n% Hmm, that resulted in half-tiles around the edges. Let's try\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/floor.html |floor|> instead of\r\n% |round|.\r\n\r\nI = (-1).^(floor(n1\/20) + floor(n2\/20));\r\nimshow(I, [-1 1])\r\n\r\n%%\r\n% Now back to my observation above about the relationship between the size\r\n% of the image and the number of squares. The implementation based on\r\n% $(-1)^{(n_1 + n_2)}$ makes it very easy to specify independently the\r\n% image height, image width, and square size. You can even turn the squares\r\n% into rectangles.\r\n\r\nimage_height = 206;\r\nimage_width = 97;\r\n\r\nsquare_height = 25;\r\nsquare_width = 11;  % I know, I know; it's not a \"square\". It's a joke, OK?\r\n\r\n[n1, n2] = ndgrid(1:image_height, 1:image_width);\r\nI = (-1).^(floor(n1\/square_height) + floor(n2\/square_width));\r\n% Make it a binary image of 0s and 1s instead of -1s and 1s.\r\nBW = I == 1;\r\nimshow(BW)\r\n\r\n%%\r\n% Last week's fun with the Jahne test pattern gave me the notion of making\r\n% a checkboard image with spatially-varying frequency. The technique is the\r\n% same - square and scale the exponent terms so that the frequency rises as\r\n% you move away from the center of the image and reaches a maximum at the\r\n% desired location. (And I'm going to switch back to using |round|.)\r\n\r\nsquare_size = 20;\r\n[n1, n2] = ndgrid(-100:100);\r\nI = (-1).^(round(n1.^2\/1000) + round(n2.^2\/1000));\r\nBW = I == 1;\r\nimshow(BW)\r\n\r\n%%\r\n% I love MATLAB!\r\n\r\n##### SOURCE END ##### fe6202bb167a45829f04af5537759819\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Thinking about test images (13-Jan-2006, 16-Jul-2011, 19-Jul-2011, 19-Jul-2011) recently prompted me to take a look at the Image Processing Toolbox function checkerboard.\r\n   I =... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/07\/26\/checkerboard-fun\/\">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":[182,252,36,206,288,116,188,130],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/382"}],"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=382"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/382\/revisions"}],"predecessor-version":[{"id":3745,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/382\/revisions\/3745"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}