{"id":167,"date":"2007-08-31T15:12:17","date_gmt":"2007-08-31T19:12:17","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2007\/08\/31\/intensity-weighted-centroids\/"},"modified":"2019-10-23T13:49:40","modified_gmt":"2019-10-23T17:49:40","slug":"intensity-weighted-centroids","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2007\/08\/31\/intensity-weighted-centroids\/","title":{"rendered":"Intensity-weighted centroids"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>One the measurements provided by <tt>regionprops<\/tt> is <tt>'Centroid'<\/tt>.  Here's an example of labeling binary objects, computing the centroid of each object, and plotting the centroid location\r\n      on top of the image.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">I = imread(<span style=\"color: #A020F0\">'text.png'<\/span>);\r\nimshow(I)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/167\/intensity_weighted_centroid_01.png\"> <pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">L = bwlabel(I);\r\ns = regionprops(L, <span style=\"color: #A020F0\">'Centroid'<\/span>);\r\nhold <span style=\"color: #A020F0\">on<\/span>\r\n<span style=\"color: #0000FF\">for<\/span> k = 1:numel(s)\r\n    plot(s(k).Centroid(1), s(k).Centroid(2), <span style=\"color: #A020F0\">'r*'<\/span>)\r\n<span style=\"color: #0000FF\">end<\/span>\r\nhold <span style=\"color: #A020F0\">off<\/span>\r\nxlim([0 60])\r\nylim([0 60])<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/167\/intensity_weighted_centroid_02.png\"> <p>Last week, <a href=\"https:\/\/blogs.mathworks.com\/steve\/2007\/08\/21\/gray-scale-pixel-values-in-labeled-regions\/#comment-7126\">reader Daphne asked<\/a> how to compute the intensity-weighted centroid.  That is, if each labeled region corresponds to a region in a gray scale\r\n      image, how do you compute the centroid weighted by the gray scale pixel values? The easiest way, I think, is to use the both\r\n      the PixelIdxList and PixelList properties from regionprops.  Let's try it with a simple synthetic image (download the image\r\n      from <a href=\"https:\/\/blogs.mathworks.com\/images\/steve\/167\/spheres.png\">here<\/a>):\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">I = imread(<span style=\"color: #A020F0\">'spheres.png'<\/span>);\r\nimshow(I)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/167\/intensity_weighted_centroid_03.png\"> <p>Now let's threshold and label it:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">bw = I &lt; 255;\r\nbw = imfill(bw, <span style=\"color: #A020F0\">'holes'<\/span>);\r\nL = bwlabel(bw);\r\nimshow(label2rgb(L, @jet, [.7 .7 .7]))<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/167\/intensity_weighted_centroid_04.png\"> <p>Next, get the PixelIdxList and PixelList for each labeled region:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">s = regionprops(L, <span style=\"color: #A020F0\">'PixelIdxList'<\/span>, <span style=\"color: #A020F0\">'PixelList'<\/span>);<\/pre><p>Each row of the PixelList for a region contains the x and y coordinates of a pixel in that region.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">s(1).PixelList(1:4, :)<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n    48    94\r\n    48    95\r\n    48    96\r\n    48    97\r\n\r\n<\/pre><p>We can get the grayscale value of pixels in a region by indexing into <tt>I<\/tt> with PixelIdxList.  By weighting those values with the x and y coordinates, we can compute the centroid.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">idx = s(1).PixelIdxList;\r\nsum_region1 = sum(I(idx));\r\nx = s(1).PixelList(:, 1);\r\ny = s(1).PixelList(:, 2);\r\n\r\nxbar = sum(x .* double(I(idx))) \/ sum_region1<\/pre><pre style=\"font-style:oblique\">\r\nxbar =\r\n\r\n   74.6128\r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">ybar = sum(y .* double(I(idx))) \/ sum_region1<\/pre><pre style=\"font-style:oblique\">\r\nybar =\r\n\r\n   92.5121\r\n\r\n<\/pre><p>Let's do that in a loop and superimpose the centroid locations on the image.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">imshow(I)\r\nhold <span style=\"color: #A020F0\">on<\/span>\r\n<span style=\"color: #0000FF\">for<\/span> k = 1:numel(s)\r\n    idx = s(k).PixelIdxList;\r\n    pixel_values = double(I(idx));\r\n    sum_pixel_values = sum(pixel_values);\r\n    x = s(k).PixelList(:, 1);\r\n    y = s(k).PixelList(:, 2);\r\n    xbar = sum(x .* pixel_values) \/ sum_pixel_values;\r\n    ybar = sum(y .* pixel_values) \/ sum_pixel_values;\r\n\r\n    plot(xbar, ybar, <span style=\"color: #A020F0\">'*'<\/span>)\r\n<span style=\"color: #0000FF\">end<\/span>\r\nhold <span style=\"color: #A020F0\">off<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/167\/intensity_weighted_centroid_05.png\"> <script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_2809ba35febb42b19db37beb34f0e619() {\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='2809ba35febb42b19db37beb34f0e619 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 2809ba35febb42b19db37beb34f0e619';\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 2007 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_2809ba35febb42b19db37beb34f0e619()\"><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.5<br><\/p>\r\n<\/div>\r\n<!--\r\n2809ba35febb42b19db37beb34f0e619 ##### SOURCE BEGIN #####\r\n%% Intensity-Weighted Centroid\r\n% One the measurements provided by |regionprops| is |'Centroid'|.  Here's\r\n% an example of labeling binary objects, computing the centroid of each\r\n% object, and plotting the centroid location on top of the image.\r\n\r\nI = imread('text.png');\r\nimshow(I)\r\n\r\n%%\r\n\r\nL = bwlabel(I);\r\ns = regionprops(L, 'Centroid');\r\nhold on\r\nfor k = 1:numel(s)\r\n    plot(s(k).Centroid(1), s(k).Centroid(2), 'r*')\r\nend\r\nhold off\r\nxlim([0 60])\r\nylim([0 60])\r\n\r\n%%\r\n% Last week, \r\n% <https:\/\/blogs.mathworks.com\/steve\/2007\/08\/21\/gray-scale-pixel-values-in-labeled-regions\/#comment-7126 \r\n% reader Daphne asked> how to compute the intensity-weighted\r\n% centroid.  That is, if each labeled region corresponds to a region in a\r\n% gray scale image, how do you compute the centroid weighted by the gray\r\n% scale pixel values? The easiest way, I think, is to use the both the\r\n% PixelIdxList and PixelList properties from regionprops.  Let's try it\r\n% with a simple synthetic image (download the image from\r\n% <https:\/\/blogs.mathworks.com\/images\/steve\/167\/spheres.png here>):\r\n\r\nI = imread('spheres.png');\r\nimshow(I)\r\n\r\n%%\r\n% Now let's threshold and label it:\r\n\r\nbw = I < 255;\r\nbw = imfill(bw, 'holes');\r\nL = bwlabel(bw);\r\nimshow(label2rgb(L, @jet, [.7 .7 .7]))\r\n\r\n%%\r\n% Next, get the PixelIdxList and PixelList for each labeled region:\r\n\r\ns = regionprops(L, 'PixelIdxList', 'PixelList');\r\n\r\n%%\r\n% Each row of the PixelList for a region contains the x and y coordinates\r\n% of a pixel in that region.\r\n\r\ns(1).PixelList(1:4, :)\r\n\r\n%%\r\n% We can get the grayscale value of pixels in a region by indexing into |I|\r\n% with PixelIdxList.  By weighting those values with the x and y\r\n% coordinates, we can compute the centroid.\r\n\r\nidx = s(1).PixelIdxList;\r\nsum_region1 = sum(I(idx));\r\nx = s(1).PixelList(:, 1);\r\ny = s(1).PixelList(:, 2);\r\n\r\nxbar = sum(x .* double(I(idx))) \/ sum_region1\r\n\r\n%%\r\n\r\nybar = sum(y .* double(I(idx))) \/ sum_region1\r\n\r\n%%\r\n% Let's do that in a loop and superimpose the centroid locations on the\r\n% image.\r\n\r\nimshow(I)\r\nhold on\r\nfor k = 1:numel(s)\r\n    idx = s(k).PixelIdxList;\r\n    pixel_values = double(I(idx));\r\n    sum_pixel_values = sum(pixel_values);\r\n    x = s(k).PixelList(:, 1);\r\n    y = s(k).PixelList(:, 2);\r\n    xbar = sum(x .* pixel_values) \/ sum_pixel_values;\r\n    ybar = sum(y .* pixel_values) \/ sum_pixel_values;\r\n    \r\n    plot(xbar, ybar, '*')\r\nend\r\nhold off\r\n\r\n##### SOURCE END ##### 2809ba35febb42b19db37beb34f0e619\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   One the measurements provided by regionprops is 'Centroid'.  Here's an example of labeling binary objects, computing the centroid of each object, and plotting the centroid location\r\n      on top... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2007\/08\/31\/intensity-weighted-centroids\/\">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":[166,402,90,136,76,36,152,162,68,168,126,360,298],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/167"}],"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=167"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/167\/revisions"}],"predecessor-version":[{"id":3558,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/167\/revisions\/3558"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}