{"id":1620,"date":"2016-03-14T09:51:52","date_gmt":"2016-03-14T13:51:52","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=1620"},"modified":"2019-11-01T12:18:47","modified_gmt":"2019-11-01T16:18:47","slug":"matlab-image-display-grayscale-and-binary-images","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2016\/03\/14\/matlab-image-display-grayscale-and-binary-images\/","title":{"rendered":"MATLAB image display &#8211; grayscale and binary images"},"content":{"rendered":"<div class=\"content\"><!--introduction--><p>In my previous posts (<a href=\"https:\/\/blogs.mathworks.com\/steve\/2016\/02\/09\/matlab-image-display-from-data-values-to-pixel-colors\/\">February 9<\/a>, <a href=\"https:\/\/blogs.mathworks.com\/steve\/2016\/02\/22\/matlab-image-display-truecolor-and-indexed-images\/\">February 22<\/a>, and <a href=\"https:\/\/blogs.mathworks.com\/steve\/2016\/02\/29\/matlab-image-display-scaled-indexed-images\/\">February 29<\/a>), I discussed the truecolor and indexed image display models in MATLAB, as well as the direct and scaled variations of indexed display. The Image Processing Toolbox has conventions for two additional image display models: grayscale and binary. These conventions are used by the MATLAB image display function <a href=\"https:\/\/blogs.mathworks.com\/steve\/2016\/02\/29\/matlab-image-display-scaled-indexed-images\/\"><tt>imshow<\/tt><\/a>, which originated in the Image Processing Toolbox.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#ef6c2800-7e8e-4db9-b75f-437e02db7076\">Grayscale image display<\/a><\/li><li><a href=\"#f44508a5-cb1f-4291-9cd9-ae7c3e5e4c08\">Controlling the grayscale display range<\/a><\/li><li><a href=\"#ad0fa801-3e0c-4550-b5eb-92645d3e2693\">Binary image display<\/a><\/li><\/ul><\/div><h4>Grayscale image display<a name=\"ef6c2800-7e8e-4db9-b75f-437e02db7076\"><\/a><\/h4><p>If you pass a single argument to <a href=\"https:\/\/www.mathworks.com\/help\/images\/ref\/imshow.html\"><tt>imshow<\/tt><\/a>, it interprets the input as a grayscale image. Here's an illustration using a simple sinusoid:<\/p><pre class=\"codeinput\">theta = linspace(0, 2*pi, 256);\r\nI = repmat((-cos(2*theta) + 1)\/2, [256 1]);\r\nim = imshow(I);\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/GrayscaleAndBinaryImagesExample_01.png\" alt=\"\"> <p>As far as the MATLAB Graphics system is concerned, this is a scaled indexed image being displayed in a figure with a grayscale colormap installed. Here are the key properties that have been set to control the image display:<\/p><pre class=\"codeinput\">ax = gca;\r\nfig = gcf;\r\n\r\nim.CDataMapping\r\n<\/pre><pre class=\"codeoutput\">\r\nans =\r\n\r\nscaled\r\n\r\n<\/pre><pre class=\"codeinput\">ax.CLim\r\n<\/pre><pre class=\"codeoutput\">\r\nans =\r\n\r\n     0     1\r\n\r\n<\/pre><pre class=\"codeinput\">map = fig.Colormap;\r\nmap(1:5,:)\r\n<\/pre><pre class=\"codeoutput\">\r\nans =\r\n\r\n         0         0         0\r\n    0.0039    0.0039    0.0039\r\n    0.0078    0.0078    0.0078\r\n    0.0118    0.0118    0.0118\r\n    0.0157    0.0157    0.0157\r\n\r\n<\/pre><p>The function <tt>imshow<\/tt> handles all these details for you.<\/p><h4>Controlling the grayscale display range<a name=\"f44508a5-cb1f-4291-9cd9-ae7c3e5e4c08\"><\/a><\/h4><p>Using <tt>imshow<\/tt>, you can override the conventional display range and specify your own black and white values. You do this by providing a second input argument, a two-element vector containing the black and white values. In the call to <tt>imshow<\/tt> below, 0.4 (and any lower value) gets displayed as black. The value 0.6 (and any higher value) gets displayed as white.<\/p><pre class=\"codeinput\">imshow(I, [0.4 0.6])\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/GrayscaleAndBinaryImagesExample_02.png\" alt=\"\"> <h4>Binary image display<a name=\"ad0fa801-3e0c-4550-b5eb-92645d3e2693\"><\/a><\/h4><p>The other Image Processing Toolbox image display model is the binary image. If you supply a single input argument that is logical, then <tt>imshow<\/tt> (as well as many other toolbox functions) interpret that input as a binary image.<\/p><pre class=\"codeinput\">bw = imread(<span class=\"string\">'text.png'<\/span>);\r\nislogical(bw)\r\n<\/pre><pre class=\"codeoutput\">\r\nans =\r\n\r\n     1\r\n\r\n<\/pre><pre class=\"codeinput\">imshow(bw)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/GrayscaleAndBinaryImagesExample_03.png\" alt=\"\"> <script language=\"JavaScript\"> <!-- \r\n    function grabCode_5223a001df3e45e8ad26544cea130afa() {\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='5223a001df3e45e8ad26544cea130afa ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 5223a001df3e45e8ad26544cea130afa';\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_5223a001df3e45e8ad26544cea130afa()\"><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\n5223a001df3e45e8ad26544cea130afa ##### SOURCE BEGIN #####\r\n%% All about pixel colors: Grayscale and binary images\r\n% In my previous posts\r\n% (<https:\/\/blogs.mathworks.com\/steve\/2016\/02\/09\/matlab-image-display-from-data-values-to-pixel-colors\/\r\n% February 9>,\r\n% <https:\/\/blogs.mathworks.com\/steve\/2016\/02\/22\/matlab-image-display-truecolor-and-indexed-images\/\r\n% February 22>, and\r\n% <https:\/\/blogs.mathworks.com\/steve\/2016\/02\/29\/matlab-image-display-scaled-indexed-images\/'\r\n% February 29>), I discussed the truecolor and indexed image display models\r\n% in MATLAB, as well as the direct and scaled variations of indexed display.\r\n% The Image Processing Toolbox has conventions for two additional image\r\n% display models: grayscale and binary. These conventions are used by the\r\n% MATLAB image display function\r\n% <https:\/\/blogs.mathworks.com\/steve\/2016\/02\/29\/matlab-image-display-scaled-indexed-images\/\r\n% |imshow|>, which originated in the Image Processing Toolbox.\r\n\r\n%% Grayscale image display\r\n% If you pass a single argument to <https:\/\/www.mathworks.com\/help\/images\/ref\/imshow.html \r\n% |imshow|>, it interprets the input as a grayscale image. Here's an illustration \r\n% using a simple sinusoid:\r\n\r\ntheta = linspace(0, 2*pi, 256);\r\nI = repmat((-cos(2*theta) + 1)\/2, [256 1]);\r\nim = imshow(I);\r\n%% \r\n% As far as the MATLAB Graphics system is concerned, this is a scaled indexed image being \r\n% displayed in a figure with a grayscale colormap installed. Here are the key \r\n% properties that have been set to control the image display:\r\n\r\nax = gca;\r\nfig = gcf;\r\n\r\nim.CDataMapping\r\n\r\n%%\r\nax.CLim\r\n\r\n%%\r\nmap = fig.Colormap;\r\nmap(1:5,:)\r\n\r\n%% \r\n% The function |imshow| handles all these details for you.\r\n\r\n%% Controlling the grayscale display range\r\n% Using |imshow|, you can override the conventional display range and\r\n% specify your own black and white values. You do this by providing a second\r\n% input argument, a two-element vector containing the black and white\r\n% values. In the call to |imshow| below, 0.4 (and any lower value) gets\r\n% displayed as black. The value 0.6 (and any higher value) gets displayed as\r\n% white.\r\n\r\nimshow(I, [0.4 0.6])\r\n\r\n%% Binary image display\r\n% The other Image Processing Toolbox image display model is the binary\r\n% image. If you supply a single input argument that is logical, then\r\n% |imshow| (as well as many other toolbox functions) interpret that input as\r\n% a binary image.\r\n\r\nbw = imread('text.png');\r\nislogical(bw)\r\n%% \r\n% \r\n\r\nimshow(bw)\r\n\r\n##### SOURCE END ##### 5223a001df3e45e8ad26544cea130afa\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/GrayscaleAndBinaryImagesExample_03.png\" onError=\"this.style.display ='none';\" \/><\/div><!--introduction--><p>In my previous posts (<a href=\"https:\/\/blogs.mathworks.com\/steve\/2016\/02\/09\/matlab-image-display-from-data-values-to-pixel-colors\/\">February 9<\/a>, <a href=\"https:\/\/blogs.mathworks.com\/steve\/2016\/02\/22\/matlab-image-display-truecolor-and-indexed-images\/\">February 22<\/a>, and <a href=\"https:\/\/blogs.mathworks.com\/steve\/2016\/02\/29\/matlab-image-display-scaled-indexed-images\/\">February 29<\/a>), I discussed the truecolor and indexed image display models in MATLAB, as well as the direct and scaled variations of indexed display. The Image Processing Toolbox has conventions for two additional image display models: grayscale and binary. These conventions are used by the MATLAB image display function <a href=\"https:\/\/blogs.mathworks.com\/steve\/2016\/02\/29\/matlab-image-display-scaled-indexed-images\/\"><tt>imshow<\/tt><\/a>, which originated in the Image Processing Toolbox.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2016\/03\/14\/matlab-image-display-grayscale-and-binary-images\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[13],"tags":[74,80,172,76,36,118,32,116],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1620"}],"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=1620"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1620\/revisions"}],"predecessor-version":[{"id":1630,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1620\/revisions\/1630"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=1620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=1620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=1620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}