{"id":119,"date":"2007-03-06T07:00:45","date_gmt":"2007-03-06T12:00:45","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2007\/03\/06\/connected-component-labeling-part-1\/"},"modified":"2019-10-23T08:43:58","modified_gmt":"2019-10-23T12:43:58","slug":"connected-component-labeling-part-1","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2007\/03\/06\/connected-component-labeling-part-1\/","title":{"rendered":"Connected component labeling &#8211; Part 1"},"content":{"rendered":"<div class=\"content\">\n<p>Several people have asked recently about how <tt>bwlabel<\/tt> works, so I plan to write about it here during the next several weeks.<\/p>\n<p><tt>bwlabel<\/tt> implements an operation known as <i>connected component labeling<\/i>, or sometimes <i>blob labeling<\/i>. So, what is a connected component, and what does it mean to label it?<\/p>\n<p>Let's start by looking at a simple binary image:<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">bw = imread(<span style=\"color: #a020f0;\">'https:\/\/blogs.mathworks.com\/images\/steve\/119\/binary.png'<\/span>);\r\nimshow(bw)<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/119\/bwlabel_part1_01.jpg\" hspace=\"5\" vspace=\"5\" \/><\/p>\n<p>What do you see? Most people would probably say that they see four round objects (or blobs). (Actually, if you look closely<br \/>\nat the lower right object, you can see a picture of Cleve Moler from his grad school days. No, just kidding.)<\/p>\n<p>In MATLAB, this image is represented as a simple matrix. It has 74,536 elements. 17,871 of the elements have the value 1;<br \/>\nthe rest have the value 0. Here are a few of these matrix values:<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">showPixelValues(bw, [173, 162])<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/119\/bwlabel_part1_02.jpg\" hspace=\"5\" vspace=\"5\" \/><\/p>\n<p>How do we get from 17,871 foreground pixels to four objects? Well, some of the pixels are obviously \"connected\" to each other.<br \/>\nWe want to identify all the groups of interconnected foreground pixels. This process is called connected component labeling.<br \/>\n(If you are interested in a more precise definition of a connected component, see section 9.4 of <i>Digital Image Processing Using MATLAB<\/i>.)<\/p>\n<p>In subsequent posts I'll write about several methods you might use to find connected components, including the particular<br \/>\nmethods used by <tt>bwlabel<\/tt> and its multidimensional cousin <tt>bwlabeln<\/tt>. I'll also explain how the exact definition of a pixel's neighborhood affects the results.<\/p>\n<p><script>\/\/ <![CDATA[\nfunction grabCode_885e1a14f5254c2ca582b624fa9d8f4a() {\n        \/\/ Remember the title so we can use it in the new page\n        title = document.title;\n\n        \/\/ Break up these strings so that their presence\n        \/\/ in the Javascript doesn't mess up the search for\n        \/\/ the MATLAB code.\n        t1='885e1a14f5254c2ca582b624fa9d8f4a ' + '##### ' + 'SOURCE BEGIN' + ' #####';\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 885e1a14f5254c2ca582b624fa9d8f4a';\n    \n        b=document.getElementsByTagName('body')[0];\n        i1=b.innerHTML.indexOf(t1)+t1.length;\n        i2=b.innerHTML.indexOf(t2);\n \n        code_string = b.innerHTML.substring(i1, i2);\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\n\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \n        \/\/ in the XML parser.\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\n        \/\/ doesn't go ahead and substitute the less-than character. \n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\n\n        author = 'Steve Eddins';\n        copyright = 'Copyright 2007 The MathWorks, Inc.';\n\n        w = window.open();\n        d = w.document;\n        d.write('\n\n\n\n<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\n\n\n\n\\n');\n      \n      d.title = title + ' (MATLAB code)';\n      d.close();\n      }\n\/\/ ]]><\/script><\/p>\n<p style=\"text-align: right; font-size: xx-small; font-weight: lighter; font-style: italic; color: gray;\">\n<a><span style=\"font-size: x-small; font-style: italic;\">Get<br \/>\nthe MATLAB code<br \/>\n<noscript>(requires JavaScript)<\/noscript><\/span><\/a><\/p>\n<p>Published with MATLAB\u00ae 7.4<\/p>\n<\/div>\n<p><!--\n885e1a14f5254c2ca582b624fa9d8f4a ##### SOURCE BEGIN #####\n%% Connected Component Labeling - Part 1\n% Several people have asked recently about how <https:\/\/www.mathworks.com\/help\/images\/index.htmlbwlabel.html % |bwlabel|> works, so I plan\n% to write about it here during the next several weeks.\n%\n% |bwlabel| implements an operation known as _connected component\n% labeling_, or sometimes _blob labeling_.  So, what is a connected\n% component, and what does it mean to label it?\n%\n% Let's start by looking at a simple binary image:\n\nbw = imread('http&#8203:\/\/blogs.mathworks.com\/images\/steve\/119\/binary.png');\nimshow(bw)\n\n%%\n% What do you see? Most people would probably say that they see four round\n% objects (or blobs).  (Actually, if you look closely at the lower right\n% object, you can see a picture of Cleve Moler from his grad school days.\n% No, just kidding.)\n%\n% In MATLAB, this image is represented as a simple matrix.  It has 74,536\n% elements.  17,871 of the elements have the value 1; the rest have the\n% value 0.  Here are a few of these matrix values:\n\nshowPixelValues(bw, [173, 162])\n\n%%\n% How do we get from 17,871 foreground pixels to four objects?  Well, some\n% of the pixels are obviously \"connected\" to each other.  We want to\n% identify all the groups of interconnected foreground pixels.  This\n% process is called connected component labeling.  (If you are interested\n% in a more precise definition of a connected component, see section 9.4 of\n% _Digital Image Processing Using MATLAB_.)\n%\n% In subsequent posts I'll write about several methods you might use to\n% find connected components, including the particular methods used by\n% |bwlabel| and its multidimensional cousin |bwlabeln|.  I'll also explain\n% how the exact definition of a pixel's neighborhood affects the results.\n##### SOURCE END ##### 885e1a14f5254c2ca582b624fa9d8f4a\n--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\nSeveral people have asked recently about how bwlabel works, so I plan to write about it here during the next several weeks.<br \/>\nbwlabel implements an operation known as connected component labeling, or... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2007\/03\/06\/connected-component-labeling-part-1\/\">read more >><\/a><\/p>\n","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[76,36],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/119"}],"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=119"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/119\/revisions"}],"predecessor-version":[{"id":2232,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/119\/revisions\/2232"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}