{"id":208,"date":"2008-04-24T07:00:14","date_gmt":"2008-04-24T11:00:14","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2008\/04\/24\/visualizing-the-output-of-bwlabel\/"},"modified":"2019-10-28T09:30:13","modified_gmt":"2019-10-28T13:30:13","slug":"visualizing-the-output-of-bwlabel","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2008\/04\/24\/visualizing-the-output-of-bwlabel\/","title":{"rendered":"Visualizing the output of bwlabel"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>I often find myself writing small functions that help visualize certain image processing algorithms.  For example, my last\r\n      three posts on <tt>bwlabel<\/tt> included identical snippets of code that performed the following steps on a label matrix:\r\n   <\/p>\r\n   <p>1. Display a binary image using two light shades of gray.<\/p>\r\n   <p>2. Use <tt>regionprops<\/tt> to find the location of each labeled object.\r\n   <\/p>\r\n   <p>3. Superimpose object text labels over each labeled object.<\/p>\r\n   <p>The visualization code, although not complicated, was longer than the algorithm code I was trying to explain.  That can obscure\r\n      the key points of the discussion.  It would be nice if there were a single visualization function to call.\r\n   <\/p>\r\n   <p>In the past, we haven't put algorithm visualization functions into the Image Processing Toolbox.  They don't really increase\r\n      what you can do with the toolbox, and there are many different visualization techniques that might be useful for any given\r\n      algorithm.  Even within a single basic technique, such as my <tt>bwlabel<\/tt> visualization, there are many possible variations.\r\n   <\/p>\r\n   <p>Still, algorithm visualizations can be very useful.  We've been talking recently about including \"example\" visualizations\r\n      in the toolbox.   My idea is that each example visualization should have simple code and a simple syntax.  We shouldn't succumb\r\n      to the temptation to overdesign these examples by supporting lots of options and variations, or by overoptimizing them.  The\r\n      primary purpose of the examples would be to provide inspiration and a starting point for our users to create their own visualizations.\r\n   <\/p>\r\n   <p>This week I captured the visualization code from my recent posts into a function called <a title=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadFile.do?objectId=19665&objectType=FILE (link no longer works)\">VISLABELS<\/a>, which I uploaded to the\r\n      MATLAB Central File Exchange.  Here's how to use it:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">help <span style=\"color: #A020F0\">vislabels<\/span><\/pre><pre style=\"font-style:oblique\"> VISLABELS Visualize labels of connected components\r\n    VISLABELS is used to visualize the output of BWLABEL.\r\n \r\n    VISLABELS(L), where L is a label matrix returned by BWLABEL,\r\n    displays each object's label number on top of the object itself.\r\n \r\n    Note: VISLABELS requires the Image Processing Toolbox.\r\n \r\n    Example\r\n    -------\r\n        bw = imread('text.png');\r\n        L = bwlabel(bw);\r\n        vislabels(L)\r\n        axis([1 70 1 70])\r\n\r\n<\/pre><p>And here's what the example does:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">bw = imread(<span style=\"color: #A020F0\">'text.png'<\/span>);\r\nL = bwlabel(bw);\r\nvislabels(L)\r\naxis([1 70 1 70])<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2008\/bwlabel_visualization_01.png\"> <p>Do you have your own favorite ways of visualizing algorithms?  I encourage you to share them on the File Exchange.<\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_37b35677f5fc4c508a81f6d713b2b6a2() {\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='37b35677f5fc4c508a81f6d713b2b6a2 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 37b35677f5fc4c508a81f6d713b2b6a2';\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_37b35677f5fc4c508a81f6d713b2b6a2()\"><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\n37b35677f5fc4c508a81f6d713b2b6a2 ##### SOURCE BEGIN #####\r\n%%\r\n% I often find myself writing small functions that help visualize\r\n% certain image processing algorithms.  For example, my last\r\n% three posts on |bwlabel| included identical snippets of code that\r\n% performed the following steps on a label matrix:\r\n%\r\n% 1. Display a binary image using two light shades of gray.\r\n%\r\n% 2. Use |regionprops| to find the location of each labeled object.\r\n%\r\n% 3. Superimpose object text labels over each labeled object.\r\n%\r\n% The visualization code, although not complicated, was longer than\r\n% the algorithm code I was trying to explain.  That can obscure the\r\n% key points of the discussion.  It would be nice if there were a\r\n% single visualization function to call.\r\n%\r\n% In the past, we haven't put algorithm visualization functions into\r\n% the Image Processing Toolbox.  They don't really increase what you\r\n% can do with the toolbox, and there are many different\r\n% visualization techniques that might be useful for any given\r\n% algorithm.  Even within a single basic technique, such as my\r\n% |bwlabel| visualization, there are many possible variations.\r\n%\r\n% Still, algorithm visualizations can be very useful.  We've been\r\n% talking recently about including \"example\" visualizations in the\r\n% toolbox.   My idea is that each example visualization should have\r\n% simple code and a simple syntax.  We shouldn't succumb to the\r\n% temptation to overdesign these examples by supporting lots of\r\n% options and variations, or by overoptimizing them.  The primary\r\n% purpose of the examples would be to provide inspiration and a\r\n% starting point for our users to create their own visualizations.\r\n%\r\n% This week I captured the visualization code from my recent posts\r\n% into a function called VISLABELS, which I uploaded to the MATLAB\r\n% Central File Exchange.  Here's how to use it:\r\n\r\nhelp vislabels\r\n\r\n%%\r\n% And here's what the example does:\r\n\r\nbw = imread('text.png');\r\nL = bwlabel(bw);\r\nvislabels(L)\r\naxis([1 70 1 70])\r\n\r\n%%\r\n% Do you have your own favorite ways of visualizing algorithms?  I\r\n% encourage you to share them on the File Exchange.\r\n\r\n##### SOURCE END ##### 37b35677f5fc4c508a81f6d713b2b6a2\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   I often find myself writing small functions that help visualize certain image processing algorithms.  For example, my last\r\n      three posts on bwlabel included identical snippets of code that... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2008\/04\/24\/visualizing-the-output-of-bwlabel\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[50,166,76],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/208"}],"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=208"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/208\/revisions"}],"predecessor-version":[{"id":3598,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/208\/revisions\/3598"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}