{"id":284,"date":"2009-08-31T13:30:18","date_gmt":"2009-08-31T17:30:18","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2009\/08\/31\/functional-design-clunkers\/"},"modified":"2019-10-28T16:57:22","modified_gmt":"2019-10-28T20:57:22","slug":"functional-design-clunkers","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2009\/08\/31\/functional-design-clunkers\/","title":{"rendered":"Functional design clunkers"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>Sometimes I'd really like to be able to go back in time and fix a few of the functional designs in the Image Processing Toolbox.\r\n      In a few cases the original design flaw is that we bundled up a little too much functionality in a single function.  I'll\r\n      give you two examples: <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/toolbox\/images\/index.html?\/access\/helpdesk\/help\/releases\/R2009a\/toolbox\/images\/graythresh.html\"><tt>graythresh<\/tt><\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/toolbox\/images\/index.html?\/access\/helpdesk\/help\/releases\/R2009a\/toolbox\/images\/edge.html\"><tt>edge<\/tt><\/a>.\r\n   <\/p>\r\n   <p>The function <tt>graythresh<\/tt> uses <a href=\"http:\/\/en.wikipedia.org\/wiki\/Otsu%27s_method\">Otsu's method<\/a> to automatically determine a \"good\" threshold for a gray-scale image. You use it like this:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">I = imread(<span style=\"color: #A020F0\">'coins.png'<\/span>);\r\nimshow(I)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2009\/design_clunkers_1_01.jpg\"> <pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">T = graythresh(I); <span style=\"color: #228B22\">% T is a normalized threshold value between 0 and 1<\/span>\r\nbw = im2bw(I, T);\r\nimshow(bw)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2009\/design_clunkers_1_02.jpg\"> <p>The functional design flaw here is that Otsu's method doesn't really need the original image at all. It is defined solely\r\n      in terms of the <i>histogram<\/i> of the image.  The first thing <tt>graythresh<\/tt> does is compute the histogram.\r\n   <\/p>\r\n   <p>But what if you had already computed the histogram for some other purpose? There's no way to use <tt>graythresh<\/tt> without computing it again.\r\n   <\/p>\r\n   <p>My own functional design sensibility has evolved toward writing more functions that do smaller, simpler steps. So today I\r\n      would design a function like <tt>graythresh<\/tt> so that it took a histogram as the input argument.\r\n   <\/p>\r\n   <p>But isn't that less convenient? Yes. But I would move the \"convenience\" syntax elsewhere, probably to <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/toolbox\/images\/index.html?\/access\/helpdesk\/help\/releases\/R2009a\/toolbox\/images\/im2bw.html\"><tt>im2bw<\/tt><\/a> by giving it an \"automatic threshold\" syntax. If I could redesign <tt>im2bw<\/tt> together with <tt>graythresh<\/tt>, I would probably make the single-input syntax <tt>im2bw(I)<\/tt> use <tt>graythresh<\/tt> behind the scenes to compute the threshold automatically.\r\n   <\/p>\r\n   <p>That design would be <i>more<\/i> convenient than it is today, because you could type <tt>im2bw(I)<\/tt> instead of <tt>im2bw(I, graythresh(I))<\/tt>, and it would also be <i>more flexible<\/i> because you would have access to smaller computational pieces.\r\n   <\/p>\r\n   <p>The function <tt>edge<\/tt> has a similar problem. Several of the supported edge detection methods, such as Sobel and Roberts, use the gradient magnitude.\r\n       The gradient magnitude is computed within <tt>edge<\/tt>, but there's no separate toolbox function for computing it.  The toolbox and MATLAB have the low-level functions and operators\r\n      that can be used to compute the gradient magnitude (<a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/toolbox\/images\/index.html?\/access\/helpdesk\/help\/releases\/R2009a\/toolbox\/images\/fspecial.html\"><tt>fspecial<\/tt><\/a>, <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/toolbox\/images\/index.html?\/access\/helpdesk\/help\/releases\/R2009a\/toolbox\/images\/imfilter.html\"><tt>imfilter<\/tt><\/a>, <tt>^2<\/tt>, <tt>sqrt<\/tt>), and the high-level function <tt>edge<\/tt> uses the gradient magnitude, but there's no function in the middle that conveniently computes the gradient magnitude for\r\n      you.\r\n   <\/p>\r\n   <p>I mention these examples to encourage you to think about your own approach to breaking down your computations into functions.<\/p>\r\n   <p>I would also be interested to hear your opinions about what other functional designs in MATLAB and the Image Processing Toolbox\r\n      could be better. Please add your comments to this post.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_0b627b4bfdd142bfb0485e6c42b7eb93() {\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='0b627b4bfdd142bfb0485e6c42b7eb93 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 0b627b4bfdd142bfb0485e6c42b7eb93';\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 = '';\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_0b627b4bfdd142bfb0485e6c42b7eb93()\"><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.8<br><\/p>\r\n<\/div>\r\n<!--\r\n0b627b4bfdd142bfb0485e6c42b7eb93 ##### SOURCE BEGIN #####\r\n%%\r\n% Sometimes I'd really like to be able to go back in time and fix a few of the\r\n% functional designs in the Image Processing Toolbox.  In a few cases the original\r\n% design flaw is that we bundled up a little too much functionality in a single\r\n% function.  I'll give you two examples: \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009a\/toolbox\/images\/index.html?\/access\/helpdesk\/help\/releases\/R2009a\/toolbox\/images\/graythresh.html \r\n% |graythresh|> and \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009a\/toolbox\/images\/index.html?\/access\/helpdesk\/help\/releases\/R2009a\/toolbox\/images\/edge.html \r\n% |edge|>.\r\n%\r\n% The function |graythresh| uses <http:\/\/en.wikipedia.org\/wiki\/Otsu%27s_method \r\n% Otsu's method> to automatically determine a \"good\" threshold for a gray-scale\r\n% image. You use it like this:\r\n\r\nI = imread('coins.png');\r\nimshow(I)\r\n\r\n%%\r\nT = graythresh(I); % T is a normalized threshold value between 0 and 1\r\nbw = im2bw(I, T);\r\nimshow(bw)\r\n\r\n%%\r\n% The functional design flaw here is that Otsu's method doesn't really need the\r\n% original image at all. It is defined solely in terms of the _histogram_ of the\r\n% image.  The first thing |graythresh| does is compute the histogram.\r\n%\r\n% But what if you had already computed the histogram for some other purpose?\r\n% There's no way to use |graythresh| without computing it again.\r\n%\r\n% My own functional design sensibility has evolved toward writing more functions\r\n% that do smaller, simpler steps. So today I would design a function like\r\n% |graythresh| so that it took a histogram as the input argument.\r\n%\r\n% But isn't that less convenient? Yes. But I would move the \"convenience\" syntax\r\n% elsewhere, probably to \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009a\/toolbox\/images\/index.html?\/access\/helpdesk\/help\/releases\/R2009a\/toolbox\/images\/im2bw.html \r\n% |im2bw|> by giving it an \"automatic threshold\" syntax.\r\n% If I could redesign |im2bw| together with |graythresh|, I would probably make\r\n% the single-input syntax |im2bw(I)| use |graythresh| behind the scenes to\r\n% compute the threshold automatically.\r\n%\r\n% That design would be _more_ convenient than it is today, because you could\r\n% type |im2bw(I)| instead of |im2bw(I, graythresh(I))|, and it would also be\r\n% _more flexible_ because you would have access to smaller computational pieces.\r\n%\r\n% The function |edge| has a similar problem. Several of the supported edge\r\n% detection methods, such as Sobel and Roberts, use the gradient\r\n% magnitude.  The gradient magnitude is computed within |edge|, but there's no\r\n% separate toolbox function for computing it.  The toolbox and MATLAB have the\r\n% low-level functions and operators that can be used to compute the gradient\r\n% magnitude \r\n% (<https:\/\/www.mathworks.com\/help\/releases\/R2009a\/toolbox\/images\/index.html?\/access\/helpdesk\/help\/releases\/R2009a\/toolbox\/images\/fspecial.html \r\n% |fspecial|>, \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009a\/toolbox\/images\/index.html?\/access\/helpdesk\/help\/releases\/R2009a\/toolbox\/images\/imfilter.html \r\n% |imfilter|>, |^2|, |sqrt|), and the high-level function\r\n% |edge| uses the gradient magnitude, but there's no function in the middle that\r\n% conveniently computes the gradient magnitude for you.\r\n%\r\n% I mention these examples to encourage you to think about your own approach to\r\n% breaking down your computations into functions.\r\n%\r\n% I would also be interested to hear your opinions about what other functional\r\n% designs in MATLAB and the Image Processing Toolbox could be better. Please add\r\n% your comments to this post.\r\n##### SOURCE END ##### 0b627b4bfdd142bfb0485e6c42b7eb93\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Sometimes I'd really like to be able to go back in time and fix a few of the functional designs in the Image Processing Toolbox.\r\n      In a few cases the original design flaw is that we bundled... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2009\/08\/31\/functional-design-clunkers\/\">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":[228,292,82,84,370,76,36,194],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/284"}],"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=284"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/284\/revisions"}],"predecessor-version":[{"id":3651,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/284\/revisions\/3651"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}