{"id":362,"date":"2011-02-04T15:32:27","date_gmt":"2011-02-04T20:32:27","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2011\/02\/04\/more-on-segmenting-in-a-b-space\/"},"modified":"2019-10-29T13:54:07","modified_gmt":"2019-10-29T17:54:07","slug":"more-on-segmenting-in-a-b-space","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2011\/02\/04\/more-on-segmenting-in-a-b-space\/","title":{"rendered":"More on segmenting in a*-b* space"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>I'm back to looking at M&amp;Ms today. (Previous posts: <a href=\"https:\/\/blogs.mathworks.com\/steve\/2010\/12\/17\/what-color-is-green\/\">17-Dec-2010<\/a>, <a href=\"https:\/\/blogs.mathworks.com\/steve\/2010\/12\/23\/two-dimensional-histograms\/\">23-Dec-2010<\/a>, <a href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/01\/11\/freehand-segmentation-in-the-a-b-plane\/\">30-Dec-2010<\/a>, <a href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/01\/11\/freehand-segmentation-in-the-a-b-plane\/\">11-Jan-2011<\/a>)\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\/2010\/mms.jpg'<\/span>;\r\nrgb = imread(url);\r\nimshow(rgb)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/blob_segment_01.png\"> <p>Last time I showed how I used <tt>imfreehand<\/tt> to segment the region in the a*-b* plane corresponding to the green M&amp;Ms.\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/freehand-segment-screenshot.png\"> <\/p>\r\n   <p>This time I'll use connected-components labeling and <tt>regionprops<\/tt> to segment the image based on all the colors, including the desk background.\r\n   <\/p>\r\n   <p>I saved the previously computed a*-b* histogram in a MAT-file online; here's how to retrieve and display it.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">matfile_url = <span style=\"color: #A020F0\">'https:\/\/blogs.mathworks.com\/images\/steve\/2011\/freehand_segment.mat'<\/span>;\r\ntemp_matfile = [tempname <span style=\"color: #A020F0\">'.mat'<\/span>];\r\nurlwrite(matfile_url, temp_matfile);\r\ns = load(temp_matfile);\r\ndelete(temp_matfile)\r\nH = s.H;\r\nimshow(H, [0 1000], <span style=\"color: #A020F0\">'InitialMagnification'<\/span>, 300, <span style=\"color: #A020F0\">'XData'<\/span>, [-100 100], <span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'YData'<\/span>, [-100 100])\r\naxis <span style=\"color: #A020F0\">on<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/blob_segment_02.png\"> <p>Next, let's threshold the image. (Magic Number Alert! I chose the threshold manually based on visual inspection of pixel values.)<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">mask = H &gt; 100;<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">imshow(mask, <span style=\"color: #A020F0\">'InitialMagnification'<\/span>, 300, <span style=\"color: #A020F0\">'XData'<\/span>, [-100 100], <span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'YData'<\/span>, [-100 100])\r\naxis <span style=\"color: #A020F0\">on<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/blob_segment_03.png\"> <p>So we've got seven \"blobs\". Let's measure their centroids.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">props = regionprops(mask, s.H, <span style=\"color: #A020F0\">'Centroid'<\/span>);\r\nprops(1)<\/pre><pre style=\"font-style:oblique\">\r\nans = \r\n\r\n    Centroid: [20.2143 82.5357]\r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">centers = cat(1, props.Centroid)<\/pre><pre style=\"font-style:oblique\">\r\ncenters =\r\n\r\n   20.2143   82.5357\r\n   45.4706   92.2353\r\n   49.2973   29.0541\r\n   51.6029   67.1765\r\n   52.2667   53.4667\r\n   77.8000   67.8000\r\n   80.1724   84.4483\r\n\r\n<\/pre><p>These centroid values are in the intrinsic pixel coordinates of the image. To convert them to a* and b* values, we have to\r\n      scale and shift.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">ab_centers = 2*centers - 102<\/pre><pre style=\"font-style:oblique\">\r\nab_centers =\r\n\r\n  -61.5714   63.0714\r\n  -11.0588   82.4706\r\n   -3.4054  -43.8919\r\n    1.2059   32.3529\r\n    2.5333    4.9333\r\n   53.6000   33.6000\r\n   58.3448   66.8966\r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">a_centers = ab_centers(:,1);\r\nb_centers = ab_centers(:,2);<\/pre><p>Next question: where are these centers, and what are the regions in the a*-b* plane closest to each one? The <tt>voronoi<\/tt> function shows you both.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">hold <span style=\"color: #A020F0\">on<\/span>\r\nvoronoi(a_centers, b_centers, <span style=\"color: #A020F0\">'r'<\/span>)\r\nhold <span style=\"color: #A020F0\">off<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/blob_segment_04.png\"> <p>To perform a nearest-neighbor classification of all the pixels, let's compute the Delaunay triangulation, from which we can\r\n      easily do the nearest-neighbor calculation.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">dt = DelaunayTri(a_centers, b_centers)<\/pre><pre style=\"font-style:oblique\">\r\ndt = \r\n\r\n  DelaunayTri\r\n\r\n  Properties:\r\n      Constraints: []\r\n                X: [7x2 double]\r\n    Triangulation: [7x3 double]\r\n\r\n\r\n<\/pre><p>Compute the a* and b* values of all the pixels:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">lab = lab2double(applycform(rgb, makecform(<span style=\"color: #A020F0\">'srgb2lab'<\/span>)));\r\nL = lab(:,:,1);\r\na = lab(:,:,2);\r\nb = lab(:,:,3);<\/pre><p>For every pixel in the original image, find the closest a*-b* centroid by using the <tt>nearestNeighbor<\/tt> function with the Delaunay triangulation.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">X = nearestNeighbor(dt, a(:), b(:));\r\nX = reshape(X, size(a));<\/pre><p>I would like to make a colormap of the seven colors in our segmentation. We have a* and b* values for each of the colors,\r\n      but not L* values. We could just make up a constant L* value. Instead, I'll compute the mean L* value for all the pixels closest\r\n      to the centroid of each of the histogram blobs.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">L_mean = zeros(size(a_centers));\r\n<span style=\"color: #0000FF\">for<\/span> k = 1:numel(L_mean)\r\n    L_mean(k) = mean(L(X == k));\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><p>Now we convert the L*a*b* values corresponding to each of our seven colors back to RGB using <tt>applycform<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">map = applycform([L_mean, a_centers, b_centers], makecform(<span style=\"color: #A020F0\">'lab2srgb'<\/span>))<\/pre><pre style=\"font-style:oblique\">\r\nmap =\r\n\r\n    0.2587    0.8349    0.1934\r\n    0.8724    0.8465    0.0271\r\n    0.1095    0.4111    0.6803\r\n    0.8382    0.7591    0.5286\r\n    0.3304    0.2993    0.2751\r\n    0.7264    0.2055    0.2008\r\n    0.9653    0.3595    0.0690\r\n\r\n<\/pre><p>And finally we can use <tt>X<\/tt> and <tt>map<\/tt> to display our segmented result as an indexed image.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">close <span style=\"color: #A020F0\">all<\/span>\r\nimshow(X, map)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/blob_segment_05.png\"> <p>Not bad!<\/p>\r\n   <p>Unless I have some inspiration between now and next week, I might be ready to let this image go and search for something else\r\n      to write about.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_7ffe9bd4b61e402f8524fa8318dcdafb() {\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='7ffe9bd4b61e402f8524fa8318dcdafb ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 7ffe9bd4b61e402f8524fa8318dcdafb';\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 2011 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_7ffe9bd4b61e402f8524fa8318dcdafb()\"><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\n7ffe9bd4b61e402f8524fa8318dcdafb ##### SOURCE BEGIN #####\r\n%%\r\n% I'm back to looking at M&Ms today. (Previous posts:\r\n% <https:\/\/blogs.mathworks.com\/steve\/2010\/12\/17\/what-color-is-green\/\r\n% 17-Dec-2010>,\r\n% <https:\/\/blogs.mathworks.com\/steve\/2010\/12\/23\/two-dimensional-histograms\/\r\n% 23-Dec-2010>,\r\n% <https:\/\/blogs.mathworks.com\/steve\/2011\/01\/11\/freehand-segmentation-in-the-a-b-plane\/\r\n% 30-Dec-2010>,\r\n% <https:\/\/blogs.mathworks.com\/steve\/2011\/01\/11\/freehand-segmentation-in-the-a-b-plane\/\r\n% 11-Jan-2011>)\r\n\r\nurl = 'https:\/\/blogs.mathworks.com\/images\/steve\/2010\/mms.jpg';\r\nrgb = imread(url);\r\nimshow(rgb)\r\n\r\n%%\r\n% Last time I showed how I used |imfreehand| to segment the region in the a*-b*\r\n% plane corresponding to the green M&Ms.\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/images\/steve\/2011\/freehand-segment-screenshot.png>>\r\n%\r\n% This time I'll use connected-components labeling and |regionprops| to\r\n% segment the image based on all the colors, including the desk background.\r\n%\r\n% I saved the previously computed a*-b* histogram in a MAT-file online;\r\n% here's how to retrieve and display it.\r\n\r\n%%\r\nmatfile_url = 'https:\/\/blogs.mathworks.com\/images\/steve\/2011\/freehand_segment.mat';\r\ntemp_matfile = [tempname '.mat'];\r\nurlwrite(matfile_url, temp_matfile);\r\ns = load(temp_matfile);\r\ndelete(temp_matfile)\r\nH = s.H;\r\nimshow(H, [0 1000], 'InitialMagnification', 300, 'XData', [-100 100], ...\r\n    'YData', [-100 100])\r\naxis on\r\n\r\n%%\r\n% Next, let's threshold the image. (Magic Number Alert! I chose the\r\n% threshold manually based on visual inspection of pixel values.)\r\n\r\nmask = H > 100;\r\n\r\n%%\r\nimshow(mask, 'InitialMagnification', 300, 'XData', [-100 100], ...\r\n    'YData', [-100 100])\r\naxis on\r\n\r\n%%\r\n% So we've got seven \"blobs\". Let's measure their centroids.\r\n\r\n%%\r\nprops = regionprops(mask, s.H, 'Centroid');\r\nprops(1)\r\n\r\n%%\r\ncenters = cat(1, props.Centroid)\r\n\r\n%%\r\n% These centroid values are in the intrinsic pixel coordinates of the\r\n% image. To convert them to a* and b* values, we have to scale and shift.\r\n\r\nab_centers = 2*centers - 102\r\n\r\n%%\r\na_centers = ab_centers(:,1);\r\nb_centers = ab_centers(:,2);\r\n\r\n%%\r\n% Next question: where are these centers, and what are the regions in the\r\n% a*-b* plane closest to each one? The |voronoi| function shows you both.\r\nhold on\r\nvoronoi(a_centers, b_centers, 'r')\r\nhold off\r\n\r\n%%\r\n% To perform a nearest-neighbor classification of all the pixels, let's\r\n% compute the Delaunay triangulation, from which we can easily do the\r\n% nearest-neighbor calculation.\r\ndt = DelaunayTri(a_centers, b_centers)\r\n\r\n%%\r\n% Compute the a* and b* values of all the pixels:\r\nlab = lab2double(applycform(rgb, makecform('srgb2lab')));\r\nL = lab(:,:,1);\r\na = lab(:,:,2);\r\nb = lab(:,:,3);\r\n\r\n%%\r\n% For every pixel in the original image, find the closest a*-b* centroid by \r\n% using the |nearestNeighbor| function with the Delaunay triangulation.\r\nX = nearestNeighbor(dt, a(:), b(:));\r\nX = reshape(X, size(a));\r\n\r\n%%\r\n% I would like to make a colormap of the seven colors in our segmentation.\r\n% We have a* and b* values for each of the colors, but not L* values. We\r\n% could just make up a constant L* value. Instead, I'll compute the mean L*\r\n% value for all the pixels closest to the centroid of each of the histogram\r\n% blobs.\r\n\r\nL_mean = zeros(size(a_centers));\r\nfor k = 1:numel(L_mean)\r\n    L_mean(k) = mean(L(X == k));\r\nend\r\n\r\n%%\r\n% Now we convert the L*a*b* values corresponding to each of our seven colors\r\n% back to RGB using |applycform|.\r\n\r\nmap = applycform([L_mean, a_centers, b_centers], makecform('lab2srgb'))\r\n\r\n%%\r\n% And finally we can use |X| and |map| to display our segmented result as\r\n% an indexed image.\r\nclose all\r\nimshow(X, map)\r\n\r\n%%\r\n% Not bad!\r\n%\r\n% Unless I have some inspiration between now and next week, I might be\r\n% ready to let this image go and search for something else to write about.\r\n\r\n##### SOURCE END ##### 7ffe9bd4b61e402f8524fa8318dcdafb\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   I'm back to looking at M&amp;Ms today. (Previous posts: 17-Dec-2010, 23-Dec-2010, 30-Dec-2010, 11-Jan-2011)\r\n   url = 'https:\/\/blogs.mathworks.com\/images\/steve\/2010\/mms.jpg';\r\nrgb =... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/02\/04\/more-on-segmenting-in-a-b-space\/\">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":[114,50,46,278,549,733,90,390,76,36,362,112,308,741,162,168,170,190,729,731,739,130],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/362"}],"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=362"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/362\/revisions"}],"predecessor-version":[{"id":3719,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/362\/revisions\/3719"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}