{"id":62,"date":"2006-06-10T21:52:44","date_gmt":"2006-06-11T01:52:44","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=62"},"modified":"2019-10-22T12:28:20","modified_gmt":"2019-10-22T16:28:20","slug":"determining-point-position-in-mri-phantom","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2006\/06\/10\/determining-point-position-in-mri-phantom\/","title":{"rendered":"Determining point positions in MRI peg phantom"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>Blog reader Jonathan from St. Jude Children's Research Hospital sent me an image derived from an MRI peg phantom:<\/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\/62\/mri_peg_phantom.png'<\/span>);\r\nimshow(bw)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/62\/phantom_point_positions_01.png\"> <p>Jonathan wanted to know how to determine the position of each point. The functions <tt>bwlabel<\/tt> and<tt>regionprops<\/tt> do the trick.\r\n   <\/p>\r\n   <p>The function <tt>bwlabel<\/tt> takes a binary image and figures which groups of white pixels are connected to each other.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">L = bwlabel(bw);<\/pre><p>The output <tt>L<\/tt> is called a <i>label matrix<\/i>.  It has the same size as <tt>bw<\/tt> and contains nonnegative integers.  Each positive integer value corresponds to a particular object.  For example, to display\r\n      the 10th object, just compare <tt>L<\/tt> to 10:\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\/62\/phantom_point_positions_02.png\"> <p>The function <tt>regionprops<\/tt> computes a number of different geometric properties of all the different regions contained in a label matrix. All we need\r\n      for this application is the centroid:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">s = regionprops(L, <span style=\"color: #A020F0\">'Centroid'<\/span>)<\/pre><pre style=\"font-style:oblique\">\r\ns = \r\n\r\n205x1 struct array with fields:\r\n    Centroid\r\n\r\n<\/pre><p>The size of the structure array <tt>s<\/tt> tells you the number of labeled objects: 205.  The centroid of the 10th object is:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">centroid_10 = s(10).Centroid<\/pre><pre style=\"font-style:oblique\">\r\ncentroid_10 =\r\n\r\n   44.1687   77.8072\r\n\r\n<\/pre><p>Here's a simple way to superimpose the centroid locations onto the original image:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">imshow(bw)\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\">'x'<\/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\/62\/phantom_point_positions_03.png\"> <p>Thanks for letting me show this image in the blog, Jonathan.<\/p>\r\n<script language=\"JavaScript\"> \r\n<!--\r\n    function grabCode_62() {\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='62 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 62';\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 2006 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      <\/script>\r\n<noscript>\r\n<em>A JavaScript-enabled browser is required to use the \"Get the MATLAB code\" link.<\/em>\r\n<\/noscript>\r\n<p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_62()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code<\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.2<br><\/p>\r\n<\/div>\r\n<!--\r\n62 ##### SOURCE BEGIN #####\r\n%%\r\n% Blog reader Jonathan from St. Jude Children's Research Hospital sent me\r\n% an image derived from an MRI peg phantom:\r\n\r\nbw = imread('https:\/\/blogs.mathworks.com\/images\/steve\/62\/mri_peg_phantom.png');\r\nimshow(bw)\r\n\r\n%%\r\n% Jonathan wanted to know how to determine the position of each point. The\r\n% functions <https:\/\/www.mathworks.com\/help\/images\/index.htmlbwlabel.html \r\n% |bwlabel|> and <https:\/\/www.mathworks.com\/help\/images\/index.htmlregionprops.html \r\n% |regionprops|> do the trick.\r\n%\r\n% The function |bwlabel| takes a binary image and figures which groups of\r\n% white pixels are connected to each other.  \r\n\r\nL = bwlabel(bw);\r\n\r\n%%\r\n% The output |L| is called a _label matrix_.  It has the same size as |bw|\r\n% and contains nonnegative integers.  Each positive integer value\r\n% corresponds to a particular object.  For example, to display the 10th \r\n% object, just compare |L| to 10:\r\n\r\nimshow(L == 10)\r\n\r\n%%\r\n% The function |regionprops| computes a number of different geometric \r\n% properties of all the different regions contained in a label matrix. All\r\n% we need for this application is the centroid:\r\n\r\ns = regionprops(L, 'Centroid')\r\n\r\n%%\r\n% The size of the structure array |s| tells you the number of labeled\r\n% objects: 205.  The centroid of the 10th object is:\r\n\r\ncentroid_10 = s(10).Centroid\r\n\r\n%%\r\n% Here's a simple way to superimpose the centroid locations onto the\r\n% original image:\r\n\r\nimshow(bw)\r\nhold on\r\nfor k = 1:numel(s)\r\n    plot(s(k).Centroid(1), s(k).Centroid(2), 'x')\r\nend\r\nhold off\r\n\r\n%%\r\n% Thanks for letting me show this image in the blog, Jonathan.\r\n##### SOURCE END ##### 62\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Blog reader Jonathan from St. Jude Children's Research Hospital sent me an image derived from an MRI peg phantom:bw =... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2006\/06\/10\/determining-point-position-in-mri-phantom\/\">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,90,76,36,162,68,168],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/62"}],"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=62"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/62\/revisions"}],"predecessor-version":[{"id":2188,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/62\/revisions\/2188"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}