{"id":217,"date":"2008-06-25T14:35:49","date_gmt":"2008-06-25T18:35:49","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2008\/06\/25\/false-color-visualization-of-binary-image-object-sets\/"},"modified":"2019-10-28T09:34:46","modified_gmt":"2019-10-28T13:34:46","slug":"false-color-vis","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2008\/06\/25\/false-color-vis\/","title":{"rendered":"False-color visualization of binary image object sets"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>Today I want to demonstrate a useful technique to produce a false-color visualization of different sets of binary image objects.\r\n      Here's the sample image that we'll use:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">url = <span style=\"color: #A020F0\">'https:\/\/blogs.mathworks.com\/images\/steve\/2008\/segmented_rice.png'<\/span>;\r\nbw = imread(url);\r\nimshow(bw)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2008\/binary_indexed_vis_01.png\"> <p>Let's look at two sets of objects: Those that touch the image border, and those that do not.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">notouch = imclearborder(bw);\r\nimshow(notouch)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2008\/binary_indexed_vis_02.png\"> <p>The objects that do touch the border can be computed using logical operators:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">touch = bw &amp; ~notouch;\r\nimshow(touch)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2008\/binary_indexed_vis_03.png\"> <p>Here's one way to turn these two sets of objects into a single, false-color, indexed image. First, initialize the index matrix:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">X = zeros(size(bw), <span style=\"color: #A020F0\">'uint8'<\/span>); <span style=\"color: #228B22\">% Did you know about<\/span>\r\n                              <span style=\"color: #228B22\">% this way to call zeros?<\/span><\/pre><p>Now assign 1 to the elements of X corresponding to the border-touching set, and assign 2 to the elements corresponding to\r\n      the interior set.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">X(touch) = 1;  <span style=\"color: #228B22\">% Logical indexing!<\/span>\r\nX(notouch) = 2;<\/pre><p>Now we just need to pick some colors for the color map.  I'll make the background white:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">map(1,:) = [1 1 1];<\/pre><p>Make the touching objects be purple-ish.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">map(2,:) = [0.7 0.3 0.8];<\/pre><p>And use a green shade for the removed objects.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">map(3,:) = [0.4 0.8 0.7];<\/pre><p>Now we can display the resulting indexed image.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">imshow(X,map)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2008\/binary_indexed_vis_04.png\"> <script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_0d0e3a0014514493ad69388c5666f8c0() {\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='0d0e3a0014514493ad69388c5666f8c0 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 0d0e3a0014514493ad69388c5666f8c0';\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_0d0e3a0014514493ad69388c5666f8c0()\"><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\n0d0e3a0014514493ad69388c5666f8c0 ##### SOURCE BEGIN #####\r\n%%\r\n% Today I want to demonstrate a useful technique to produce a\r\n% false-color visualization of different sets of binary image\r\n% objects. Here's the sample image that we'll use:\r\n\r\nurl = 'https:\/\/blogs.mathworks.com\/images\/steve\/2008\/segmented_rice.png';\r\nbw = imread(url);\r\nimshow(bw)\r\n\r\n%%\r\n% Let's look at two sets of objects: Those that touch the image\r\n% border, and those that do not.\r\n\r\nnotouch = imclearborder(bw);\r\nimshow(notouch)\r\n\r\n%%\r\n% The objects that do touch the border can be computed using logical\r\n% operators:\r\n\r\ntouch = bw & ~notouch;\r\nimshow(touch)\r\n\r\n%%\r\n% Here's one way to turn these two sets of objects into a single,\r\n% false-color, indexed image. First, initialize the index matrix:\r\n\r\nX = zeros(size(bw), 'uint8'); % Did you know about \r\n                              % this way to call zeros?\r\n                              \r\n%%\r\n% Now assign 1 to the elements of X corresponding to the\r\n% border-touching set, and assign 2 to the elements corresponding to\r\n% the interior set. \r\n\r\nX(touch) = 1;  % Logical indexing!\r\nX(notouch) = 2;\r\n\r\n%%\r\n% Now we just need to pick some colors for the color map.  I'll make the \r\n% background white:\r\n\r\nmap(1,:) = [1 1 1];\r\n\r\n%%\r\n% Make the touching objects be purple-ish.\r\n\r\nmap(2,:) = [0.7 0.3 0.8];\r\n\r\n%%\r\n% And use a green shade for the removed objects.\r\n\r\nmap(3,:) = [0.4 0.8 0.7];\r\n\r\n%%\r\n% Now we can display the resulting indexed image.\r\n\r\nimshow(X,map)\r\n##### SOURCE END ##### 0d0e3a0014514493ad69388c5666f8c0\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Today I want to demonstrate a useful technique to produce a false-color visualization of different sets of binary image objects.\r\n      Here's the sample image that we'll use:\r\n   url =... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2008\/06\/25\/false-color-vis\/\">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":[404,76,36,190,130],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/217"}],"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=217"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/217\/revisions"}],"predecessor-version":[{"id":3606,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/217\/revisions\/3606"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}