{"id":5043,"date":"2014-01-03T09:00:02","date_gmt":"2014-01-03T14:00:02","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=5043"},"modified":"2014-01-03T15:28:48","modified_gmt":"2014-01-03T20:28:48","slug":"fit-a-sphere","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2014\/01\/03\/fit-a-sphere\/","title":{"rendered":"Fit a Sphere!"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/contributors\/3208495\">Sean<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/34129-sphere-fit--least-squared-\">Sphere Fit<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/104695\">Alan Jennings<\/a>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Fitting a Sphere to Data<a name=\"1\"><\/a><\/h3>\r\n   <p>I recently had some data and wanted to fit a sphere through it so that I could find the radius of this sphere.  As I started\r\n      writing out an objective function for one of the <a href=\"\">Optimization Toolbox<\/a> optimizers (yes I was taking the way too big a hammer\r\n      approach), a quick query on the File Exchange brought up <tt>sphereFit<\/tt>, a pleasant discovery.\r\n   <\/p>\r\n   <p>After downloading the files, I looked at Alan's <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/34129-sphere-fit--least-squared--sphere-fit-least-squared\/content\/sphereFit\/html\/sphereFit_Example.html\">published example<\/a>, it looked like it would work!\r\n   <\/p>\r\n   <p>Let's see it in action:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Load mri of a human head<\/span>\r\nS = load(<span style=\"color: #A020F0\">'mri'<\/span>);\r\nD = squeeze(S.D);\r\n\r\n<span style=\"color: #228B22\">% Generate data points on the edge of the head<\/span>\r\n<span style=\"color: #0000FF\">for<\/span> ii = size(D,3):-1:1;\r\n    <span style=\"color: #228B22\">% Perimeter of convex hull of each slice<\/span>\r\n    M(:,:,ii) = bwperim(bwconvhull(D(:,:,ii)&gt;50));\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainsf\/skull.gif\"> <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Find sub indices in our mask<\/span>\r\nidx = find(M); <span style=\"color: #228B22\">% find points in M<\/span>\r\n[rr,cc,pp] = ind2sub(size(M),idx); <span style=\"color: #228B22\">% sub indices<\/span>\r\npp = pp.*floor(size(D,1).\/size(D,3)); <span style=\"color: #228B22\">% rescale third dimension<\/span>\r\n\r\n<span style=\"color: #228B22\">% View data<\/span>\r\nfigure;\r\nscatter3(rr,cc,pp,10,pp);\r\ndaspect([1,1,1]);\r\nview(-121,36);\r\naxis <span style=\"color: #A020F0\">tight<\/span>;<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainsf\/mainsf_01.png\"> <pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Fit the Sphere:<\/span>\r\n[cent,radius] = sphereFit([rr, cc, pp]);\r\nfprintf(1,<span style=\"color: #A020F0\">'\\nRadius of sphere is %3.1f\\nIt is centered at [%3.1f %3.1f %3.1f]\\n'<\/span>,radius,cent);<\/pre><pre style=\"font-style:oblique\">\r\nRadius of sphere is 54.3\r\nIt is centered at [72.9 63.4 51.7]\r\n<\/pre><p>Use Alan's example code to show the sphere through the points<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">scatter3(cc,rr,pp,25,pp,<span style=\"color: #A020F0\">'*'<\/span>); <span style=\"color: #228B22\">%points<\/span>\r\nhold <span style=\"color: #A020F0\">on<\/span>; <span style=\"color: #228B22\">% hold<\/span>\r\ndaspect([1,1,1]); <span style=\"color: #228B22\">% equal axis so a sphere looks like a sphere<\/span>\r\n[Base_rr,Base_cc,Base_pp] = sphere(20);\r\nsurf(radius*Base_rr+cent(2),<span style=\"color: #0000FF\">...<\/span>\r\n     radius*Base_cc+cent(1),<span style=\"color: #0000FF\">...<\/span>\r\n     radius*Base_pp+cent(3),<span style=\"color: #A020F0\">'faceAlpha'<\/span>,0.3,<span style=\"color: #A020F0\">'Facecolor'<\/span>,<span style=\"color: #A020F0\">'m'<\/span>)\r\naxis <span style=\"color: #A020F0\">tight<\/span>;\r\nview(-121,36);<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainsf\/mainsf_02.png\"> <h3>Comments<a name=\"5\"><\/a><\/h3>\r\n   <p>Give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=5043#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/34129-sphere-fit--least-squared-#comments\">comment<\/a> for Alan.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_a243dd848a234378b4e1a17bbdf073fc() {\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='a243dd848a234378b4e1a17bbdf073fc ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' a243dd848a234378b4e1a17bbdf073fc';\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 = 'Sean de Wolski';\r\n        copyright = 'Copyright 2014 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_a243dd848a234378b4e1a17bbdf073fc()\"><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; R2013b<br><\/p>\r\n<\/div>\r\n<!--\r\na243dd848a234378b4e1a17bbdf073fc ##### SOURCE BEGIN #####\r\n%% Sphere Fit\r\n%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/answers\/contributors\/3208495 Sean>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/34129-sphere-fit--least-squared- Sphere Fit> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/104695 Alan Jennings>.\r\n% \r\n\r\n%% Fitting a Sphere to Data\r\n%\r\n% I recently had some data and wanted to fit a sphere through it so that I\r\n% could find the radius of this sphere.  As I started writing out\r\n% an objective function for one of the Optimization Toolbox optimizers (yes\r\n% I was taking the way too big a hammer approach), a quick query on the FEX\r\n% brought up |sphereFit| pleasantly surprised me.\r\n%\r\n% After downloading the files, I looked at Alan's\r\n% <\r\n% published> example, it looked like it would work!\r\n%\r\n% Let's see it in action:\r\n%\r\n\r\n% Load mri of a human head\r\nS = load('mri');\r\nD = squeeze(S.D);\r\n\r\n% Generate data points on the edge of the head\r\nfor ii = size(D,3):-1:1;\r\n    % Perimeter of convex hull of each slice\r\n    M(:,:,ii) = bwperim(bwconvhull(D(:,:,ii)>50));\r\nend\r\n\r\n%%\r\n%\r\n% <<skull.gif>>\r\n% \r\n\r\n% Find sub indices in our mask\r\nidx = find(M); % find points in M\r\n[rr,cc,pp] = ind2sub(size(M),idx); % sub indices\r\npp = pp.*floor(size(D,1).\/size(D,3)); % rescale third dimension\r\n\r\n% View data\r\nfigure;\r\nscatter3(rr,cc,pp,10,pp);\r\ndaspect([1,1,1]);\r\nview(-121,36);\r\naxis tight;\r\n\r\n%% \r\n\r\n% Fit the Sphere:\r\n[cent,radius] = sphereFit([rr, cc, pp]);\r\nfprintf(1,'\\nRadius of sphere is %3.1f\\nIt is centered at [%3.1f %3.1f %3.1f]\\n',radius,cent);\r\n\r\n%% \r\n% Use Alan's example code to show the sphere through the points\r\n%\r\n\r\nscatter3(cc,rr,pp,25,pp,'*'); %points\r\nhold on; % hold\r\ndaspect([1,1,1]); % equal axis so a sphere looks like a sphere\r\n[Base_rr,Base_cc,Base_pp] = sphere(20); \r\nsurf(radius*Base_rr+cent(2),...\r\n     radius*Base_cc+cent(1),...\r\n     radius*Base_pp+cent(3),'faceAlpha',0.3,'Facecolor','m')\r\naxis tight;\r\nview(-121,36);\r\n\r\n%% Comments\r\n% \r\n% Give it a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=5043#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/34129-sphere-fit--least-squared-#comments\r\n% comment> for Alan.\r\n%\r\n \r\n\r\n##### SOURCE END ##### a243dd848a234378b4e1a17bbdf073fc\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainsf\/skull.gif\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n   \r\n      Sean's pick this week is Sphere Fit by Alan Jennings.\r\n      \r\n   \r\n   Fitting a Sphere to Data\r\n   I recently had some data and wanted to fit a sphere through... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2014\/01\/03\/fit-a-sphere\/\">read more >><\/a><\/p>","protected":false},"author":87,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,6],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5043"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=5043"}],"version-history":[{"count":13,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5043\/revisions"}],"predecessor-version":[{"id":5057,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5043\/revisions\/5057"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=5043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=5043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=5043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}