{"id":558,"date":"2012-05-15T21:19:00","date_gmt":"2012-05-16T01:19:00","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=558"},"modified":"2019-10-31T14:47:48","modified_gmt":"2019-10-31T18:47:48","slug":"shuffling-label-colors","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2012\/05\/15\/shuffling-label-colors\/","title":{"rendered":"Shuffling label colors"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>I've written often here about various computational and visualization techniques involving labeling connected components in\r\n      binary images. Sometimes I use the function <tt>label2rgb<\/tt> to convert a label matrix into a color image with a different color assigned to each label.\r\n   <\/p>\r\n   <p>Here's an example.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">bw = imread(<span style=\"color: #A020F0\">'https:\/\/blogs.mathworks.com\/images\/steve\/2012\/rice-bw.png'<\/span>);\r\nimshow(bw)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2012\/shuffle_label_colors_01.png\"> <p>Now compute the connected components and the corresponding label matrix.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">cc = bwconncomp(bw);\r\nL = labelmatrix(cc);<\/pre><p>In the label matrix, each foreground object in the original binary image is assigned a unique positive integer. Here, for\r\n      instance, is how to display the tenth object.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">imshow(L == 10)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2012\/shuffle_label_colors_02.png\"> <p>Use the function <tt>label2rgb<\/tt> to \"colorize\" the label matrix.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">rgb = label2rgb(L);\r\nimshow(rgb)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2012\/shuffle_label_colors_03.png\"> <p>That's a nice effect, but because of the way the colors are assigned, object near each other tend to have very similar colors.\r\n      It might be better sometimes to assign the colors differently. That's what the <tt>'shuffle'<\/tt> argument to <tt>label2rgb<\/tt> is for.\r\n   <\/p>\r\n   <p>Here's the full syntax including <tt>'shuffle'<\/tt>:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">rgb = label2rgb(L,map,zerocolor,<span style=\"color: #A020F0\">'shuffle'<\/span>);<\/pre><p><tt>zerocolor<\/tt> is a three-element vector specifying what color is used for the background pixels.\r\n   <\/p>\r\n   <p>Let's try it with the jet colormap and a light gray background color.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">rgb = label2rgb(L,<span style=\"color: #A020F0\">'jet'<\/span>,[.7 .7 .7],<span style=\"color: #A020F0\">'shuffle'<\/span>);\r\nimshow(rgb)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2012\/shuffle_label_colors_04.png\"> <p>Now it's easier to see where two objects might be actually touching and so receive the same label. Let's zoom in closer to\r\n      see:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">xlim([128 185]);\r\nylim([5 62]);<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2012\/shuffle_label_colors_05.png\"> <p>I hope you find this useful!<\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_244262ec1ea44d08b9c018a05519a680() {\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='244262ec1ea44d08b9c018a05519a680 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 244262ec1ea44d08b9c018a05519a680';\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 2012 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_244262ec1ea44d08b9c018a05519a680()\"><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.14<br><\/p>\r\n<\/div>\r\n<!--\r\n244262ec1ea44d08b9c018a05519a680 ##### SOURCE BEGIN #####\r\n%%\r\n% I've written often here about various computational and visualization\r\n% techniques involving labeling connected components in binary images.\r\n% Sometimes I use the function |label2rgb| to convert a label matrix into a\r\n% color image with a different color assigned to each label.\r\n%\r\n% Here's an example.\r\n\r\nbw = imread('https:\/\/blogs.mathworks.com\/images\/steve\/2012\/rice-bw.png');\r\nimshow(bw)\r\n\r\n%%\r\n% Now compute the connected components and the corresponding label matrix.\r\n\r\ncc = bwconncomp(bw);\r\nL = labelmatrix(cc);\r\n\r\n%%\r\n% In the label matrix, each foreground object in the original binary image\r\n% is assigned a unique positive integer. Here, for instance, is how to\r\n% display the tenth object.\r\n\r\nimshow(L == 10)\r\n\r\n%%\r\n% Use the function |label2rgb| to \"colorize\" the label matrix.\r\n\r\nrgb = label2rgb(L);\r\nimshow(rgb)\r\n\r\n%%\r\n% That's a nice effect, but because of the way the colors are assigned,\r\n% object near each other tend to have very similar colors. It might be\r\n% better sometimes to assign the colors differently. That's what the\r\n% |'shuffle'| argument to |label2rgb| is for.\r\n%\r\n% Here's the full syntax including |'shuffle'|:\r\n%\r\n%   rgb = label2rgb(L,map,zerocolor,'shuffle');\r\n%\r\n% |zerocolor| is a three-element vector specifying what color is used for\r\n% the background pixels.\r\n%\r\n% Let's try it with the jet colormap and a light gray background color.\r\n\r\nrgb = label2rgb(L,'jet',[.7 .7 .7],'shuffle');\r\nimshow(rgb)\r\n\r\n%%\r\n% Now it's easier to see where two objects might be actually touching and\r\n% so receive the same label. Let's zoom in closer to see:\r\n\r\nxlim([128 185]);\r\nylim([5 62]);\r\n\r\n%%\r\n% I hope you find this useful!\r\n##### SOURCE END ##### 244262ec1ea44d08b9c018a05519a680\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   I've written often here about various computational and visualization techniques involving labeling connected components in\r\n      binary images. Sometimes I use the function label2rgb to... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2012\/05\/15\/shuffling-label-colors\/\">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,76,36,152,563,360,298],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/558"}],"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=558"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/558\/revisions"}],"predecessor-version":[{"id":560,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/558\/revisions\/560"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=558"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=558"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}