{"id":342,"date":"2010-10-01T14:31:37","date_gmt":"2010-10-01T18:31:37","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2010\/10\/01\/disconnected-component-labeling-part-1\/"},"modified":"2019-10-29T13:35:27","modified_gmt":"2019-10-29T17:35:27","slug":"disconnected-component-labeling-part-1","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2010\/10\/01\/disconnected-component-labeling-part-1\/","title":{"rendered":"Disconnected component labeling: part 1"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>A blog reader asked me recently for an enhancement related to connectivity. I found the request a bit surprising, and I want\r\n      to explore it with you in the this post and the next.\r\n   <\/p>\r\n   <p>Many Image Processing Toolbox functions rely on some notion of connectivity between a pixel and its neighbors. In two-dimensional\r\n      processing, the most common connectivities are usually called \"4-connected\" and \"8-connected.\"\r\n   <\/p>\r\n   <p>In the 4-connected definition, a pixel is connected to its north, east, south, and west neighbors. In the matrix below, the\r\n      1-valued elements indicate the connected neighbors of the center pixel.\r\n   <\/p><pre> 0 1 0\r\n 1 1 1\r\n 0 1 0<\/pre><p>In the 8-connected definition, a pixel is connected to all 8 of its touching neighbors:<\/p><pre> 1 1 1\r\n 1 1 1\r\n 1 1 1<\/pre><p>Most toolbox functions that take a connectivity input argument will accept other, more unusual connectivities, such as this\r\n      one:\r\n   <\/p><pre> 0 1 0\r\n 0 1 0\r\n 0 1 0<\/pre><p>The connectivity matrix above means that a pixel is connected only to its north and south neighbors.<\/p>\r\n   <p>Let's see what this does with a simple connected component labeling problem. Here's a small test matrix:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">bw = [1     1     1     0     0     0     0     0\r\n    1     1     1     0     1     1     0     0\r\n    1     1     1     0     1     1     0     0\r\n    1     1     1     0     0     0     1     0\r\n    1     1     1     0     0     0     1     0\r\n    1     1     1     0     0     0     1     0\r\n    1     1     1     0     0     1     1     0\r\n    1     1     1     0     0     0     0     0]<\/pre><pre style=\"font-style:oblique\">\r\nbw =\r\n\r\n     1     1     1     0     0     0     0     0\r\n     1     1     1     0     1     1     0     0\r\n     1     1     1     0     1     1     0     0\r\n     1     1     1     0     0     0     1     0\r\n     1     1     1     0     0     0     1     0\r\n     1     1     1     0     0     0     1     0\r\n     1     1     1     0     0     1     1     0\r\n     1     1     1     0     0     0     0     0\r\n\r\n<\/pre><p>With a connectivity of 8, here are the connected components:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">cc = bwconncomp(bw, 8);\r\nL8 = labelmatrix(cc)<\/pre><pre style=\"font-style:oblique\">\r\nL8 =\r\n\r\n    1    1    1    0    0    0    0    0\r\n    1    1    1    0    2    2    0    0\r\n    1    1    1    0    2    2    0    0\r\n    1    1    1    0    0    0    2    0\r\n    1    1    1    0    0    0    2    0\r\n    1    1    1    0    0    0    2    0\r\n    1    1    1    0    0    2    2    0\r\n    1    1    1    0    0    0    0    0\r\n\r\n<\/pre><p>You can see that there are just two connected components.<\/p>\r\n   <p>Here's what you get if you specify a north-south connectivity using a connectivity matrix:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">conn = [0 1 0; 0 1 0; 0 1 0]<\/pre><pre style=\"font-style:oblique\">\r\nconn =\r\n\r\n     0     1     0\r\n     0     1     0\r\n     0     1     0\r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">cc_ns = bwconncomp(bw, conn);\r\nL_ns = labelmatrix(cc_ns)<\/pre><pre style=\"font-style:oblique\">\r\nL_ns =\r\n\r\n    1    2    3    0    0    0    0    0\r\n    1    2    3    0    4    5    0    0\r\n    1    2    3    0    4    5    0    0\r\n    1    2    3    0    0    0    7    0\r\n    1    2    3    0    0    0    7    0\r\n    1    2    3    0    0    0    7    0\r\n    1    2    3    0    0    6    7    0\r\n    1    2    3    0    0    0    0    0\r\n\r\n<\/pre><p>Now there are seven connected components, each of which lies entirely within a single column.<\/p>\r\n   <p>A valid connectivity matrix has to be 3-by-3 (or 3-by-3-by- ... -by-3) for higher dimensions); the center pixel has to be\r\n      1; and the matrix has to be symmetric about the center pixel. (This last rule means that pixel p is connected to pixel q if\r\n      and only if pixel q is connected to pixel p.)\r\n   <\/p>\r\n   <p>Blog reader Oliver <a href=\"https:\/\/blogs.mathworks.com\/steve\/2010\/09\/07\/almost-connected-component-labeling\/#comment-23506\">recently asked me<\/a> if we could relax one of these constraints on connectivity matrices in order to allow a connectivity definition such as this:\r\n   <\/p><pre> 1 0 1 0 1<\/pre><p>With this connectivity, a pixel is not considered be connected to <b>any<\/b> of its touching neighbors. Instead, a pixel is connected only to the pixels two over to the east and west.\r\n   <\/p>\r\n   <p>Since I've been making up new terms related to connected component labeling recently (see <a href=\"https:\/\/blogs.mathworks.com\/steve\/2010\/09\/07\/almost-connected-component-labeling\/\">almost-connected-component labeling<\/a>), here's your new term for the day: <i>disconnected component labeling<\/i>.\r\n   <\/p>\r\n   <p>Toolbox functions currently do not allow this.<\/p>\r\n   <p>In part 2 I will show how to work around this restriction. In the meantime, I'm giving you homework: think about whether you\r\n      know of applications that could benefit from allowing this looser definition of connectivity.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_810ceb53ccd44bc9a35c9c7e89432eba() {\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='810ceb53ccd44bc9a35c9c7e89432eba ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 810ceb53ccd44bc9a35c9c7e89432eba';\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 2010 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_810ceb53ccd44bc9a35c9c7e89432eba()\"><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.11<br><\/p>\r\n<\/div>\r\n<!--\r\n810ceb53ccd44bc9a35c9c7e89432eba ##### SOURCE BEGIN #####\r\n%%\r\n% A blog reader asked me recently for an enhancement related to\r\n% connectivity. I found the request a bit surprising, and I want to explore\r\n% it with you in the this post and the next.\r\n%\r\n% Many Image Processing Toolbox functions rely on some notion of\r\n% connectivity between a pixel and its neighbors. In two-dimensional\r\n% processing, the most common connectivities are usually called\r\n% \"4-connected\" and \"8-connected.\"\r\n%\r\n% In the 4-connected definition, a pixel is connected to its\r\n% north, east, south, and west neighbors. In the matrix below, the 1-valued\r\n% elements indicate the connected neighbors of the center pixel.\r\n%\r\n%   0 1 0\r\n%   1 1 1\r\n%   0 1 0\r\n%\r\n% In the 8-connected definition, a pixel is connected to all 8\r\n% of its touching neighbors:\r\n%\r\n%   1 1 1\r\n%   1 1 1\r\n%   1 1 1\r\n%\r\n% Most toolbox functions that take a connectivity input argument will\r\n% accept other, more unusual connectivities, such as this one:\r\n%\r\n%   0 1 0\r\n%   0 1 0\r\n%   0 1 0\r\n%\r\n% The connectivity matrix above means that a pixel is connected only to its\r\n% north and south neighbors.\r\n%\r\n% Let's see what this does with a simple connected component labeling\r\n% problem. Here's a small test matrix:\r\n\r\nbw = [1     1     1     0     0     0     0     0\r\n    1     1     1     0     1     1     0     0\r\n    1     1     1     0     1     1     0     0\r\n    1     1     1     0     0     0     1     0\r\n    1     1     1     0     0     0     1     0\r\n    1     1     1     0     0     0     1     0\r\n    1     1     1     0     0     1     1     0\r\n    1     1     1     0     0     0     0     0]\r\n\r\n%%\r\n% With a connectivity of 8, here are the connected components:\r\n\r\ncc = bwconncomp(bw, 8);\r\nL8 = labelmatrix(cc)\r\n\r\n%%\r\n% You can see that there are just two connected components.\r\n%\r\n% Here's what you get if you specify a north-south connectivity using a\r\n% connectivity matrix:\r\n\r\nconn = [0 1 0; 0 1 0; 0 1 0]\r\n\r\n%%\r\n\r\ncc_ns = bwconncomp(bw, conn);\r\nL_ns = labelmatrix(cc_ns)\r\n\r\n%%\r\n% Now there are seven connected components, each of which lies entirely within\r\n% a single column.\r\n%\r\n% A valid connectivity matrix has to be 3-by-3 (or 3-by-3-by- ... -by-3)\r\n% for higher dimensions); the center pixel has to be 1; and the matrix has\r\n% to be symmetric about the center pixel. (This last rule means that pixel p\r\n% is connected to pixel q if and only if pixel q is connected to pixel p.)\r\n%\r\n% Blog reader Oliver \r\n% <https:\/\/blogs.mathworks.com\/steve\/2010\/09\/07\/almost-connected-component-labeling\/#comment-23506 \r\n% recently asked me> if we could relax one of these\r\n% constraints on connectivity matrices in order to allow a connectivity\r\n% definition such as this:\r\n%\r\n%   1 0 1 0 1\r\n%\r\n% With this connectivity, a pixel is not considered be connected to *any*\r\n% of its touching neighbors. Instead, a pixel is connected only to the\r\n% pixels two over to the east and west.\r\n%\r\n% Since I've been making up new terms related to connected component\r\n% labeling recently (see \r\n% <https:\/\/blogs.mathworks.com\/steve\/2010\/09\/07\/almost-connected-component-labeling\/ \r\n% almost-connected-component labeling>), here's your\r\n% new term for the day: _disconnected component labeling_.\r\n%\r\n% Toolbox functions currently do not allow this.\r\n%\r\n% In part 2 I will show how to work around this restriction. In the\r\n% meantime, I'm giving you homework: think about whether you know of\r\n% applications that could benefit from allowing this looser definition of\r\n% connectivity.\r\n\r\n##### SOURCE END ##### 810ceb53ccd44bc9a35c9c7e89432eba\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   A blog reader asked me recently for an enhancement related to connectivity. I found the request a bit surprising, and I want\r\n      to explore it with you in the this post and the next.\r\n   \r\n  ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2010\/10\/01\/disconnected-component-labeling-part-1\/\">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":[561,563],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/342"}],"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=342"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/342\/revisions"}],"predecessor-version":[{"id":3697,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/342\/revisions\/3697"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}