{"id":36,"date":"2006-02-21T07:00:52","date_gmt":"2006-02-21T12:00:52","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=36"},"modified":"2021-06-17T10:32:57","modified_gmt":"2021-06-17T14:32:57","slug":"tracing-george","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2006\/02\/21\/tracing-george\/","title":{"rendered":"Tracing George"},"content":{"rendered":"<div class=\"alert alert-info\">\r\n<span class=\"alert_icon icon-alert-info-reverse\"><\/span>\r\n<p class=\"alert_heading\"><strong>Note<\/strong><\/p>\r\n<p>See the <a href=\"https:\/\/blogs.mathworks.com\/steve\/2021\/06\/17\/use-imbinarize-to-threshold-gray-scale-images\/\">17-Jun-2021 post<\/a> for new or updated information about this topic.<\/p>\r\n<\/div>\r\n\r\n<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p><a href=\"https:\/\/blogs.mathworks.com\/steve\/?p=37\">Last time<\/a>, in my spatial transformations series, I introduced the head-and-shoulders outline that I like to call George (George\r\n      P. Burdell). Today I want to diverge briefly from my main topic and show how I recently \"recovered\" George.\r\n   <\/p>\r\n   <p>George appears in section 5.11 of Digital Image Processing Using MATLAB. While working on the book, I tried to keep all data\r\n      files and M-files necessary for regenerating the book's figures.  I found, however, that I could not locate the data for George's\r\n      curve that appears in Figure 5.12 and Table 5.3.\r\n   <\/p>\r\n   <p>After a few minutes of head scratching, I decided to get the curve data by taking a digital snapshot of the figure in the\r\n      book.  Here's a cropped version:\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\/36\/george.jpg'<\/span>;\r\nI = imread(url);\r\nimshow(I)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/36\/tracing_george_01.png\"> <p>A little fuzzy and dark, but maybe workable.  The first step is to threshold the image.  The Image Processing Toolbox function\r\n      <tt>graythresh<\/tt> computes binarization thresholds automatically.  It works well for a variety of images.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">threshold = graythresh(I);\r\nbw = im2bw(I, threshold);\r\nimshow(bw)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/36\/tracing_george_02.png\"> <p>Now we have a nice fat line.  Toolbox function <tt>bwmorph<\/tt> has a thinning option, but it works on white (foreground) pixels instead of black pixels.  So complement the image:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">bw2 = ~bw;<\/pre><p>and then thin it.  Specify the number of iterations to be <tt>Inf<\/tt> so that <tt>bwmorph<\/tt> will keep thinning until the lines are a single pixel wide.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">bw3 = bwmorph(bw2, <span style=\"color: #A020F0\">'thin'<\/span>, inf);\r\nimshow(bw3)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/36\/tracing_george_03.png\"> <p>Now trace the line using toolbox function <tt>bwboundaries<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">boundaries = bwboundaries(bw3);<\/pre><p><tt>boundaries<\/tt> is a cell array containing one P-by-2 matrix for each object boundary.  This image contains only one object, so <tt>boundaries<\/tt> contains only one P-by-2 matrix.  The matrix has twice as many rows as we need, because <tt>bwboundaries<\/tt> traces all the way around the object.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">b = boundaries{1};\r\nb = b(1:floor(end\/2), :);<\/pre><p>Finally we are ready to plot the curve.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">x = b(:,2);\r\ny = b(:,1);\r\nplot(x,y)\r\naxis <span style=\"color: #A020F0\">ij<\/span>     <span style=\"color: #228B22\">% Place the origin at upper left, with y values increasing from<\/span>\r\n            <span style=\"color: #228B22\">% top to bottom.<\/span>\r\naxis <span style=\"color: #A020F0\">equal<\/span>  <span style=\"color: #228B22\">% Set the aspect ratio so that the data units are the same in<\/span>\r\n            <span style=\"color: #228B22\">% every direction.<\/span>\r\ntitle(<span style=\"color: #A020F0\">'George P. Burdell'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/36\/tracing_george_04.png\"> <script language=\"JavaScript\"> \r\n<!--\r\n    function grabCode_36() {\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='36 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 36';\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_36()\"><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.1<br><\/p>\r\n<\/div>\r\n<!--\r\n36 ##### SOURCE BEGIN #####\r\n%%\r\n% Last time, in my spatial transformations series, I introduced the\r\n% head-and-shoulders outline that I like to call George (George P.\r\n% Burdell). Today I want to diverge briefly from my main topic and show how \r\n% I recently \"recovered\" George.\r\n%\r\n% George appears in section 5.11 of Digital Image Processing Using MATLAB.\r\n% While working on the book, I tried to keep all data files and M-files\r\n% necessary for regenerating the book's figures.  I found, however, that I\r\n% could not find the data for George's curve that appears in Figure 5.12\r\n% and Table 5.13.\r\n%\r\n% After a few minutes of head scratching, I decided to get the curve data\r\n% by taking a digital snapshot of the figure in the book.  Here's a cropped\r\n% version:\r\n\r\nurl = 'https:\/\/blogs.mathworks.com\/steve\/wp-content\/images\/36\/george.jpg';\r\nI = imread(url);\r\nimshow(I)\r\n\r\n%%\r\n% A little fuzzy and dark, but maybe workable.  The first step is to\r\n% threshold the image.  The Image Processing Toolbox function |graythresh|\r\n% computes binarization thresholds automatically.  It works well for a\r\n% variety of images.\r\n\r\nthreshold = graythresh(I);\r\nbw = im2bw(I, threshold);\r\nimshow(bw)\r\n\r\n%%\r\n% Now we have a nice fat line.  Toolbox function |bwmorph| has a thinning\r\n% option, but it works on white (foreground) pixels instead of black\r\n% pixels.  So complement the image:\r\n\r\nbw2 = ~bw;\r\n\r\n%%\r\n% and then thin it.  Specify the number of iterations to be |Inf| so that\r\n% |bwmorph| will keep thinning until the lines are a single pixel wide.\r\n\r\nbw3 = bwmorph(bw2, 'thin', inf);\r\nimshow(bw3)\r\n\r\n%%\r\n% Now trace the line using toolbox function |bwboundaries|.\r\n\r\nboundaries = bwboundaries(bw3);\r\n\r\n%%\r\n% |boundaries| is a cell array containing one P-by-2 matrix for each object\r\n% boundary.  This image contains only one object, so |boundaries| contains\r\n% only one P-by-2 matrix.  The matrix has twice as many rows as we need,\r\n% because |bwboundaries| traces all the way around the object.\r\n\r\nb = boundaries{1};\r\nb = b(1:floor(end\/2), :);\r\n\r\n%%\r\n% Finally we are ready to plot the curve.\r\n\r\nx = b(:,2);\r\ny = b(:,1);\r\nplot(x,y)\r\naxis ij     % Place the origin at upper left, with y values increasing from\r\n            % top to bottom.\r\naxis equal  % Set the aspect ratio so that the data units are the same in \r\n            % every direction.\r\ntitle('George P. Burdell')\r\n##### SOURCE END ##### 36\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n\r\nNote\r\nSee the 17-Jun-2021 post for new or updated information about this topic.\r\n\r\n\r\n\r\n   Last time, in my spatial transformations series, I introduced the head-and-shoulders outline that I like... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2006\/02\/21\/tracing-george\/\">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":[50,88,86,82,84,76,36,68,52],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/36"}],"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=36"}],"version-history":[{"count":4,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/36\/revisions"}],"predecessor-version":[{"id":4700,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/36\/revisions\/4700"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=36"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=36"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=36"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}