{"id":246,"date":"2009-01-28T18:05:02","date_gmt":"2009-01-28T23:05:02","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2009\/01\/28\/have-you-heard-of-the-feature-transform\/"},"modified":"2019-10-28T15:24:56","modified_gmt":"2019-10-28T19:24:56","slug":"have-you-heard-of-the-feature-transform","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2009\/01\/28\/have-you-heard-of-the-feature-transform\/","title":{"rendered":"Have you heard of the &#8220;feature transform&#8221;?"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>Today I want to know if you've heard of an operation sometimes called the <i>feature transform<\/i>, and whether you've had a particular application for it.\r\n   <\/p>\r\n   <p>But first, greetings to those who attended the SPIE Electronic Imaging Conference in California last week. I heard yesterday\r\n      that several people who stopped at the MathWorks booth mentioned the blog. Sorry I missed you! Your feedback has motivated\r\n      me to get back to the keyboard and put up some new posts.\r\n   <\/p>\r\n   <p>Now about the feature transform.  I've been thinking about this operation because we've been having some design discussions\r\n      recently about an Image Processing Toolbox function called <tt>bwdist<\/tt>, which computes the Euclidean distance transform.  For each pixel in a binary image, <tt>bwdist<\/tt> computes the distance between that pixel and the nearest foreground (nonzero) pixel.\r\n   <\/p>\r\n   <p>Here's a small example to show you what I mean.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">bw = magic(5) &gt; 21<\/pre><pre style=\"font-style:oblique\">\r\nbw =\r\n\r\n     0     1     0     0     0\r\n     1     0     0     0     0\r\n     0     0     0     0     1\r\n     0     0     0     0     0\r\n     0     0     1     0     0\r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">D = bwdist(bw)<\/pre><pre style=\"font-style:oblique\">\r\nD =\r\n\r\n    1.0000         0    1.0000    2.0000    2.0000\r\n         0    1.0000    1.4142    1.4142    1.0000\r\n    1.0000    1.4142    2.0000    1.0000         0\r\n    2.0000    1.4142    1.0000    1.4142    1.0000\r\n    2.0000    1.0000         0    1.0000    2.0000\r\n\r\n<\/pre><p>And here's a bigger example, where I display the distance transform as a false-color image.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">bw2 = false(400, 400);\r\nbw2(200, 200) = true;\r\nbw2(100, 100:300) = true;\r\nbw2(300, 100:300) = true;\r\nbw2(100:300, 100) = true;\r\nbw2(100:300, 300) = true;\r\nimshow(bw2)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2009\/feature_transform_01.jpg\"> <pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">D2 = bwdist(bw2);\r\nimshow(D2, [])\r\ncolormap(copper)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2009\/feature_transform_02.jpg\"> <p>It seems that not many people (even at MathWorks!) know that <tt>bwdist<\/tt> has an optional second output that tells you, for each image pixel, <b>which<\/b> foreground pixel it is closest to.  This is the thing that's sometimes called the <i>feature transform<\/i>, or <i>closest feature transform.<\/i> The second output is a matrix of linear indices.  (See my <a href=\"https:\/\/blogs.mathworks.com\/steve\/2008\/02\/08\/linear-indexing\/\">linear indexing post<\/a>.)  Here's what it looks like for the small matrix I started with.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">bw<\/pre><pre style=\"font-style:oblique\">\r\nbw =\r\n\r\n     0     1     0     0     0\r\n     1     0     0     0     0\r\n     0     0     0     0     1\r\n     0     0     0     0     0\r\n     0     0     1     0     0\r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[D, L] = bwdist(bw);\r\nL<\/pre><pre style=\"font-style:oblique\">\r\nL =\r\n\r\n     2     6     6     6    23\r\n     2     6     6    23    23\r\n     2     2    15    23    23\r\n     2    15    15    15    23\r\n    15    15    15    15    23\r\n\r\n<\/pre><p>Looking at <tt>L<\/tt> tells us that the pixel with linear index 2 (2nd row, 1st column) is the closest foreground pixel to 5 image pixels, including\r\n      itself. (Ties are broken in some arbitrary fashion.)\r\n   <\/p>\r\n   <p>I've come across one user application for this capability: a 3-D blood vessel tracing algorithm that I mentioned in this <a href=\"https:\/\/blogs.mathworks.com\/steve\/2006\/12\/01\/3-d-blood-vessel-tracing\/\">old post from 2006<\/a>.\r\n   <\/p>\r\n   <p>Do you have an application for the feature transform?  Let us know by <a href=\"https:\/\/blogs.mathworks.com\/steve\/2009\/01\/28\/have-you-heard-of-the-feature-transform\/#respond\">commenting<\/a>.<\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_910574d9157941cba1efad5cebd5b2e3() {\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='910574d9157941cba1efad5cebd5b2e3 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 910574d9157941cba1efad5cebd5b2e3';\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 2009 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_910574d9157941cba1efad5cebd5b2e3()\"><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.7<br><\/p>\r\n<\/div>\r\n<!--\r\n910574d9157941cba1efad5cebd5b2e3 ##### SOURCE BEGIN #####\r\n%%\r\n% Today I want to know if you've heard of an operation sometimes called the\r\n% _feature transform_, and whether you've had a particular application for it.\r\n%\r\n% But first, greetings to those who attended the SPIE Electronic Imaging\r\n% Conference in California last week. I heard yesterday that several people who\r\n% stopped at the MathWorks booth mentioned the blog. Sorry I missed you! Your\r\n% feedback has motivated me to get back to the keyboard and put up some new\r\n% posts.\r\n%\r\n% Now about the feature transform.  I've been thinking about this operation\r\n% because we've been having some design discussions recently about an Image\r\n% Processing Toolbox function called |bwdist|, which computes the Euclidean\r\n% distance transform.  For each pixel in a binary image, |bwdist| computes the\r\n% distance between that pixel and the nearest foreground (nonzero) pixel.\r\n%\r\n% Here's a small example to show you what I mean.\r\nbw = magic(5) > 21\r\n\r\n%%\r\nD = bwdist(bw)\r\n\r\n%%\r\n% And here's a bigger example, where I display the distance transform as a\r\n% false-color image.\r\n\r\nbw2 = false(400, 400);\r\nbw2(200, 200) = true;\r\nbw2(100, 100:300) = true;\r\nbw2(300, 100:300) = true;\r\nbw2(100:300, 100) = true;\r\nbw2(100:300, 300) = true;\r\nimshow(bw2)\r\n\r\n%%\r\nD2 = bwdist(bw2);\r\nimshow(D2, [])\r\ncolormap(copper)\r\n\r\n%%\r\n% It seems that not many people (even at MathWorks!) know that |bwdist| has an\r\n% optional second output that tells you, for each image pixel, *which*\r\n% foreground pixel it is closest to.  This is the thing that's sometimes called\r\n% the _feature transform_, or _closest feature transform._ The second output is\r\n% a matrix of linear indices.  (See my \r\n% <https:\/\/blogs.mathworks.com\/steve\/2008\/02\/08\/linear-indexing\/ linear indexing\r\n% post>.)  Here's what it looks like for the small matrix I started with.\r\n\r\nbw\r\n\r\n%%\r\n[D, L] = bwdist(bw);\r\nL\r\n\r\n%%\r\n% Looking at |L| tells us that the pixel with linear index 2 (2nd row, 1st\r\n% column) is the closest foreground pixel to 5 image pixels, including itself.\r\n% (Ties are broken in some arbitrary fashion.)\r\n%\r\n% I've come across one user application for this capability: a 3-D blood vessel\r\n% tracing algorithm that I mentioned in this \r\n% <https:\/\/blogs.mathworks.com\/steve\/2006\/12\/01\/3-d-blood-vessel-tracing\/ \r\n% old post from 2006>.\r\n%\r\n% Do you have an application for the feature transform?  Let us know by commenting.\r\n##### SOURCE END ##### 910574d9157941cba1efad5cebd5b2e3\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Today I want to know if you've heard of an operation sometimes called the feature transform, and whether you've had a particular application for it.\r\n   \r\n   But first, greetings to those who... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2009\/01\/28\/have-you-heard-of-the-feature-transform\/\">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":[456,58,533,102,36,54,100],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/246"}],"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=246"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/246\/revisions"}],"predecessor-version":[{"id":3613,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/246\/revisions\/3613"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}