{"id":1774,"date":"2016-04-28T07:00:08","date_gmt":"2016-04-28T11:00:08","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=1774"},"modified":"2019-11-01T16:39:24","modified_gmt":"2019-11-01T20:39:24","slug":"image-binarization-new-functional-designs","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2016\/04\/28\/image-binarization-new-functional-designs\/","title":{"rendered":"Image binarization &#8211; new functional designs"},"content":{"rendered":"<div class=\"content\"><p>With the very first version of the Image Processing Toolbox, released more than 22 years ago, you could convert a gray-scale image to binary using the function <tt>im2bw<\/tt>.<\/p><pre class=\"codeinput\">I = imread(<span class=\"string\">'rice.png'<\/span>);\r\nimshow(I)\r\ntitle(<span class=\"string\">'Original gray-scale image'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/intro_binarization_01.jpg\" alt=\"\"> <pre class=\"codeinput\">bw = im2bw(I);\r\nimshow(bw)\r\ntitle(<span class=\"string\">'Binary image'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/intro_binarization_02.jpg\" alt=\"\"> <p>You can think of this as the most fundamental form of image segmentation: separating pixels into two categories (foreground and background).<\/p><p>Aside from the introduction of <tt>graythresh<\/tt> in the mid-1990s, this area of the Image Processing Toolbox has stayed quietly unchanged. Now, suddenly, the latest release (R2016a) has introduced an overhaul of binarization. Take a look at the <a href=\"https:\/\/www.mathworks.com\/help\/images\/release-notes.html\">release notes<\/a>:<\/p><p><i><tt>imbinarize<\/tt>, <tt>otsuthresh<\/tt>, and <tt>adaptthresh<\/tt>: Threshold images using global and locally adaptive thresholds<\/i><\/p><p><i>The toolbox includes the new function, <tt>imbinarize<\/tt>, that converts grayscale images to binary images using global threshold or a locally adaptive threshold. The toolbox includes two new functions, <tt>otsuthresh<\/tt> and <tt>adaptthresh<\/tt>, that provide a way to determine the threshold needed to convert a grayscale image into a binary image.<\/i><\/p><p>What's up with this? Why were new functions needed?<\/p><p>I want to take advantage of this functionality update to dive into the details of image binarization in a short series of posts. Here's what I have in mind:<\/p><div><ol><li>The state of image binarization in the Image Processing Toolbox prior to R2016a. How did it work? What user pains motivated the redesign?<\/li><li>How binarization works in R2016a<\/li><li>Otsu's method for computing a global threshold<\/li><li>Bradley's method for computing an adaptive threshold<\/li><\/ol><\/div><p>Mostly, I haven't written this yet. If there is something particular you'd like to know, tell me in the comments, and I'll try to work it in.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_e4142a33b9c946be8096a6560586ff18() {\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='e4142a33b9c946be8096a6560586ff18 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' e4142a33b9c946be8096a6560586ff18';\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        copyright = 'Copyright 2016 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 copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\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     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_e4142a33b9c946be8096a6560586ff18()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2016a<br><\/p><\/div><!--\r\ne4142a33b9c946be8096a6560586ff18 ##### SOURCE BEGIN #####\r\n%%\r\n% With the very first version of the Image Processing Toolbox, released more\r\n% than 22 years ago, you could convert a gray-scale image to binary using\r\n% the function |im2bw|.\r\n\r\nI = imread('rice.png');\r\nimshow(I)\r\ntitle('Original gray-scale image')\r\n\r\n%%\r\n\r\nbw = im2bw(I);\r\nimshow(bw)\r\ntitle('Binary image')\r\n\r\n%%\r\n% You can think of this as the most fundamental form of image segmentation:\r\n% separating pixels into two categories (foreground and background).\r\n%\r\n% Aside from the introduction of |graythresh| in the mid-1990s, this area of\r\n% the Image Processing Toolbox has stayed quietly unchanged. Now, suddenly,\r\n% the latest release (R2016a) has introduced an overhaul of binarization.\r\n% Take a look at the <https:\/\/www.mathworks.com\/help\/images\/release-notes.html \r\n% release notes>:\r\n%\r\n% _|imbinarize|, |otsuthresh|, and |adaptthresh|: Threshold images using\r\n% global and locally adaptive thresholds_\r\n%\r\n% _The toolbox includes the new function, |imbinarize|, that converts\r\n% grayscale images to binary images using global threshold or a locally\r\n% adaptive threshold. The toolbox includes two new functions, |otsuthresh|\r\n% and |adaptthresh|, that provide a way to determine the threshold needed to\r\n% convert a grayscale image into a binary image._\r\n%\r\n% What's up with this? Why were new functions needed?\r\n%\r\n% I want to take advantage of this functionality update to dive into the\r\n% details of image binarization in a short series of posts. Here's what I\r\n% have in mind:\r\n%\r\n% # The state of image binarization in the Image Processing Toolbox prior to\r\n% R2016a. How did it work? What user pains motivated the redesign?\r\n% # How binarization works in R2016a\r\n% # Otsu's method for computing a global threshold\r\n% # Bradley's method for computing an adaptive threshold\r\n%\r\n% Mostly, I haven't written this yet. If there is something particular you'd\r\n% like to know, tell me in the comments, and I'll try to work it in.\r\n##### SOURCE END ##### e4142a33b9c946be8096a6560586ff18\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/steve\/files\/intro_binarization_02.jpg\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><p>With the very first version of the Image Processing Toolbox, released more than 22 years ago, you could convert a gray-scale image to binary using the function im2bw.I =... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2016\/04\/28\/image-binarization-new-functional-designs\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":1777,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[1159,84,1155,76,36,1157,52],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1774"}],"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=1774"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1774\/revisions"}],"predecessor-version":[{"id":1775,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1774\/revisions\/1775"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media\/1777"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=1774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=1774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=1774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}