{"id":434,"date":"2011-12-28T07:00:45","date_gmt":"2011-12-28T12:00:45","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=434"},"modified":"2019-10-29T17:05:26","modified_gmt":"2019-10-29T21:05:26","slug":"modifying-centroid-locations-in-an-image-an-application-of-linear-indexing","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2011\/12\/28\/modifying-centroid-locations-in-an-image-an-application-of-linear-indexing\/","title":{"rendered":"Modifying centroid locations in an image &#8211; an application of linear indexing"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>Blog reader Mike posed the following question <a href=\"https:\/\/blogs.mathworks.com\/steve\/2006\/06\/10\/determining-point-position-in-mri-phantom\/#comment-24640\">recently<\/a>:\r\n   <\/p>\r\n   <p>If you have a bunch of point locations (for example, object centroids), how you make a binary image containing just those\r\n      points?\r\n   <\/p>\r\n   <p>For example, consider this image:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">bw = imread(<span style=\"color: #A020F0\">'text.png'<\/span>);\r\nimshow(bw, <span style=\"color: #A020F0\">'InitialMagnification'<\/span>, 200)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/centroids_on_image_01.png\"> <p>How can we make an image like this, where the dots are located at the centroids of the objects?<\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/centroids_on_image_02.png\"> <\/p>\r\n   <p>Solving this problem is a nice application of linear indexing, something I wrote about in this blog a <a href=\"https:\/\/blogs.mathworks.com\/steve\/2008\/02\/08\/linear-indexing\/\">long time ago<\/a>. Let's see how it can work for us here.\r\n   <\/p>\r\n   <p>First, let's find the centroids using <tt>regionprops<\/tt>:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">s = regionprops(bw, <span style=\"color: #A020F0\">'Centroid'<\/span>);<\/pre><p><tt>s<\/tt> is a struct array. Since we just asked for one measurement, the centroid, each element of s is a struct containing just one\r\n      field, <tt>'Centroid'<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">s(1)<\/pre><pre style=\"font-style:oblique\">ans = \r\n    Centroid: [11 13.5000]\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">s(2)<\/pre><pre style=\"font-style:oblique\">ans = \r\n    Centroid: [7.6829 38.1707]\r\n<\/pre><p>The length of <tt>s<\/tt> is the number of objects in the image.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">num_objects = length(s)<\/pre><pre style=\"font-style:oblique\">num_objects =\r\n    88\r\n<\/pre><p>Next, we gather all the individual centroid locations into x and y vectors. To accomplish this I use the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/matlab_prog\/br04bw6-38.html#bs6e2p_\">comma-separated list syntax for struct arrays<\/a>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">centroids = cat(1, s.Centroid);\r\nx = centroids(:,1);\r\ny = centroids(:,2);<\/pre><p>If the comma-separated list syntax makes your brain hurt, you can use a loop instead:<\/p><pre>  centroids = zeros(length(s), 2);\r\n  for k = 1:length(s)\r\n      centroids(k,:) = s(k).Centroid;\r\n  end<\/pre><p>Now let's round the centroid locations to get row and column subscripts.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">r = round(y);\r\nc = round(x);<\/pre><p>Here's where linear indexing comes into play. In order to assign to a bunch of scattered locations like this, you want to\r\n      use a single subscript. That's what we call linear indexing. You can use the function <tt>sub2ind<\/tt> to convert a set of subscripts to linear indices.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">ind = sub2ind(size(bw), r, c);<\/pre><p>And finally we can use the linear indices to assign a value to a bunch of image pixel locations all at once.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">bw2 = false(size(bw));\r\nbw2(ind) = true;\r\n\r\nimshow(bw2, <span style=\"color: #A020F0\">'InitialMagnification'<\/span>, 200)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/centroids_on_image_02.png\"> <p>See my <a href=\"https:\/\/blogs.mathworks.com\/steve\/2008\/02\/08\/linear-indexing\/\">08-Feb-2008 blog post<\/a> for more about linear indexing.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_a30b78c4c45049fcb395b0d7d31e1d1c() {\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='a30b78c4c45049fcb395b0d7d31e1d1c ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' a30b78c4c45049fcb395b0d7d31e1d1c';\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_a30b78c4c45049fcb395b0d7d31e1d1c()\"><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.13<br><\/p>\r\n<\/div>\r\n<!--\r\na30b78c4c45049fcb395b0d7d31e1d1c ##### SOURCE BEGIN #####\r\n%%\r\n% Blog reader Mike posed the following question\r\n% <https:\/\/blogs.mathworks.com\/steve\/2006\/06\/10\/determining-point-position-in-mri-phantom\/#comment-24640\r\n% recently>:\r\n%\r\n% If you have a bunch of point locations (for example, object centroids),\r\n% how you make a binary image containing just those points?\r\n%\r\n% For example, consider this image:\r\n\r\nbw = imread('text.png');\r\nimshow(bw, 'InitialMagnification', 200)\r\n\r\n%%\r\n% How can we make an image like this, where the dots are located at the\r\n% centroids of the objects?\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/images\/steve\/2011\/centroids_on_image_02.png>>\r\n%\r\n% Solving this problem is a nice application of linear indexing, something\r\n% I wrote about in this blog a\r\n% <https:\/\/blogs.mathworks.com\/steve\/2008\/02\/08\/linear-indexing\/ long time\r\n% ago>. Let's see how it can work for us here.\r\n%\r\n% First, let's find the centroids using |regionprops|:\r\n\r\ns = regionprops(bw, 'Centroid');\r\n\r\n%%\r\n% |s| is a struct array. Since we just asked for one measurement, the\r\n% centroid, each element of s is a struct containing just one field,\r\n% |'Centroid'|.\r\n\r\ns(1)\r\n\r\n%%\r\n\r\ns(2)\r\n\r\n%%\r\n% The length of |s| is the number of objects in the image.\r\n\r\nnum_objects = length(s)\r\n\r\n%%\r\n% Next, we gather all the individual centroid locations into x and y\r\n% vectors. To accomplish this I use the\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/matlab_prog\/br04bw6-38.html#bs6e2p_\r\n% comma-separated list syntax for struct arrays>.\r\n\r\ncentroids = cat(1, s.Centroid);\r\nx = centroids(:,1);\r\ny = centroids(:,2);\r\n\r\n%%\r\n% If the comma-separated list syntax makes your brain hurt, you can use a\r\n% loop instead:\r\n%\r\n%    centroids = zeros(length(s), 2);\r\n%    for k = 1:length(s)\r\n%        centroids(k,:) = s(k).Centroid;\r\n%    end\r\n\r\n%%\r\n% Now let's round the centroid locations to get row and column subscripts.\r\n\r\nr = round(y);\r\nc = round(x);\r\n\r\n%%\r\n% Here's where linear indexing comes into play. In order to assign to a\r\n% bunch of scattered locations like this, you want to use a single\r\n% subscript. That's what we call linear indexing. You can use the function\r\n% |sub2ind| to convert a set of subscripts to linear indices.\r\n\r\nind = sub2ind(size(bw), r, c);\r\n\r\n%%\r\n% And finally we can use the linear indices to assign a value to a bunch of\r\n% image pixel locations all at once.\r\n\r\nbw2 = false(size(bw));\r\nbw2(ind) = true;\r\n\r\nimshow(bw2, 'InitialMagnification', 200)\r\n\r\n%%\r\n% See my <https:\/\/blogs.mathworks.com\/steve\/2008\/02\/08\/linear-indexing\/\r\n% 08-Feb-2008 blog post> for more about linear indexing.\r\n\r\n\r\n\r\n##### SOURCE END ##### a30b78c4c45049fcb395b0d7d31e1d1c\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Blog reader Mike posed the following question recently:\r\n   \r\n   If you have a bunch of point locations (for example, object centroids), how you make a binary image containing just those\r\n     ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/12\/28\/modifying-centroid-locations-in-an-image-an-application-of-linear-indexing\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[17],"tags":[46,102,76,36,705,168,188,190,468,100,130],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/434"}],"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=434"}],"version-history":[{"count":4,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/434\/revisions"}],"predecessor-version":[{"id":550,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/434\/revisions\/550"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}