{"id":211,"date":"2008-05-13T07:00:08","date_gmt":"2008-05-13T11:00:08","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2008\/05\/13\/lookup-tables-makelut-applylut\/"},"modified":"2019-10-28T09:31:38","modified_gmt":"2019-10-28T13:31:38","slug":"lookup-tables-makelut-applylut","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2008\/05\/13\/lookup-tables-makelut-applylut\/","title":{"rendered":"Lookup tables for binary image processing&mdash;makelut and applylut"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>This is the second in a short series on using lookup tables for binary image neighborhood operations.  See the <a href=\"https:\/\/blogs.mathworks.com\/steve\/2008\/05\/07\/lookup-tables-for-binary-image-processing\/\">first post<\/a> for the basic idea.\r\n   <\/p>\r\n   <p>The Image Processing Toolbox has two related functions:<\/p>\r\n   <div>\r\n      <ul>\r\n         <li><tt>applylut<\/tt> - Process binary image using lookup table\r\n         <\/li>\r\n         <li><tt>makelut<\/tt> - Utility function to help you construct a lookup table.\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>Since you have to have a lookup table in order to use <tt>applylut<\/tt>, let's start with <tt>makelut<\/tt>.  It's syntax is:\r\n   <\/p><pre> lut = makelut(f, n)<\/pre><p><tt>f<\/tt> is a function handle, and <tt>n<\/tt> is either 2 or 3, depending on whether your lookup table is to be used with 2-by-2 or 3-by-3 neighborhoods.\r\n   <\/p>\r\n   <p>For 3-by-3 neighborhoods, <tt>makelut<\/tt> will call your function handle <tt>f<\/tt> 512 times.  In each call, <tt>makelut<\/tt> will pass as an argument to <tt>f<\/tt> one of the 512 possible 3-by-3 binary neighborhoods. <tt>f<\/tt> simply has to return the desired output value for a given neighborhood.\r\n   <\/p>\r\n   <p>For example, suppose we want the output pixel to be 1 if and only if the corresponding input neighborhood looks like this:<\/p><pre>  1 0 0\r\n  0 1 0\r\n  0 0 1<\/pre><p>The you would call <tt>makelut<\/tt> as follows:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">pattern = [1 0 0; 0 1 0; 0 0 1];\r\nlut = makelut(@(neighborhood) isequal(neighborhood, pattern), 3);\r\nsize(lut)<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n   512     1\r\n\r\n<\/pre><p>Let's try that on a small sample image.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">bw = eye(5) | fliplr(eye(5))<\/pre><pre style=\"font-style:oblique\">\r\nbw =\r\n\r\n     1     0     0     0     1\r\n     0     1     0     1     0\r\n     0     0     1     0     0\r\n     0     1     0     1     0\r\n     1     0     0     0     1\r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">bw2 = applylut(bw, lut)<\/pre><pre style=\"font-style:oblique\">\r\nbw2 =\r\n\r\n     0     0     0     0     0\r\n     0     1     0     0     0\r\n     0     0     0     0     0\r\n     0     0     0     1     0\r\n     0     0     0     0     0\r\n\r\n<\/pre><p>So we see that only two input pixels, (2,2) and (4,4), have neighborhoods that exactly match the pattern.<\/p>\r\n   <p>Here's another example: Find all \"endpoint\" pixels.  I'll define an endpoint pixel as a foreground pixel with exactly one\r\n      foreground neighbor.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">endpoint_fcn = @(nhood) (nhood(2,2) ~= 0) &amp;&amp; (sum(nhood(:)) == 2);\r\nendpoint_lut = makelut(endpoint_fcn, 3);<\/pre><p>Here's the result on sample image from above:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">bw3 = applylut(bw, endpoint_lut)<\/pre><pre style=\"font-style:oblique\">\r\nbw3 =\r\n\r\n     1     0     0     0     1\r\n     0     0     0     0     0\r\n     0     0     0     0     0\r\n     0     0     0     0     0\r\n     1     0     0     0     1\r\n\r\n<\/pre><p>Only the four corner pixels are endpoints.<\/p>\r\n   <p>In my next post on this topic, I'll use <tt>makelut<\/tt> and <tt>applylut<\/tt> to play Conway's Game of Life.  Look for it in a week or so.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_2d25f6724d704b90878d561d31cc41ed() {\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='2d25f6724d704b90878d561d31cc41ed ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 2d25f6724d704b90878d561d31cc41ed';\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 2008 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_2d25f6724d704b90878d561d31cc41ed()\"><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.6<br><\/p>\r\n<\/div>\r\n<!--\r\n2d25f6724d704b90878d561d31cc41ed ##### SOURCE BEGIN #####\r\n%%\r\n% This is the second in a short series on using lookup tables for\r\n% binary image neighborhood operations.  See the \r\n% <https:\/\/blogs.mathworks.com\/steve\/2008\/05\/07\/lookup-tables-for-binary-image-processing\/ \r\n% first post> for the\r\n% basic idea.\r\n%\r\n% The Image Processing Toolbox has two related functions: \r\n%\r\n% * |applylut| - Process binary image using lookup table\r\n% * |makelut| - Utility function to help you construct a lookup\r\n% table.\r\n%\r\n% Since you have to have a lookup table in order to use |applylut|,\r\n% let's start with |makelut|.  It's syntax is:\r\n%\r\n%   lut = makelut(f, n)\r\n%\r\n% |f| is a function handle, and |n| is either 2 or 3, depending on\r\n% whether your lookup table is to be used with 2-by-2 or 3-by-3\r\n% neighborhoods.\r\n%\r\n% For 3-by-3 neighborhoods, |makelut| will call your function handle\r\n% |f| 512 times.  In each call, |makelut| will pass as an argument\r\n% to |f| one of the 512 possible 3-by-3 binary neighborhoods. |f|\r\n% simply has to return the desired output value for a given\r\n% neighborhood.\r\n%\r\n% For example, suppose we want the output pixel to be 1 if and only if\r\n% the corresponding input neighborhood looks like this:\r\n%\r\n%    1 0 0\r\n%    0 1 0\r\n%    0 0 1\r\n%\r\n% The you would call |makelut| as follows:\r\n\r\npattern = [1 0 0; 0 1 0; 0 0 1];\r\nlut = makelut(@(neighborhood) isequal(neighborhood, pattern), 3);\r\nsize(lut)\r\n\r\n%%\r\n% Let's try that on a small sample image.\r\n\r\nbw = eye(5) | fliplr(eye(5))\r\n\r\n%%\r\n\r\nbw2 = applylut(bw, lut)\r\n\r\n%%\r\n% So we see that only two input pixels, (2,2) and (4,4), have\r\n% neighborhoods that exactly match the pattern.\r\n%\r\n% Here's another example: Find all \"endpoint\" pixels.  I'll define\r\n% an endpoint pixel as a foreground pixel with exactly one\r\n% foreground neighbor.\r\n\r\nendpoint_fcn = @(nhood) (nhood(2,2) ~= 0) && (sum(nhood(:)) == 2);\r\nendpoint_lut = makelut(endpoint_fcn, 3);\r\n\r\n%%\r\n% Here's the result on sample image from above:\r\n\r\nbw3 = applylut(bw, endpoint_lut)\r\n\r\n%%\r\n% Only the four corner pixels are endpoints.\r\n%\r\n% In my next post on this topic, I'll use |makelut| and |applylut|\r\n% to play Conway's Game of Life.  Look for it in a week or so.\r\n\r\n##### SOURCE END ##### 2d25f6724d704b90878d561d31cc41ed\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   This is the second in a short series on using lookup tables for binary image neighborhood operations.  See the first post for the basic idea.\r\n   \r\n   The Image Processing Toolbox has two... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2008\/05\/13\/lookup-tables-makelut-applylut\/\">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":[494,300,192,346,492],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/211"}],"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=211"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/211\/revisions"}],"predecessor-version":[{"id":3602,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/211\/revisions\/3602"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}