{"id":42,"date":"2006-04-05T08:00:17","date_gmt":"2006-04-05T12:00:17","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=42"},"modified":"2019-10-22T09:13:14","modified_gmt":"2019-10-22T13:13:14","slug":"all-about-pixel-colors-grayscale-and-binary-images","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2006\/04\/05\/all-about-pixel-colors-grayscale-and-binary-images\/","title":{"rendered":"All about pixel colors: Grayscale and binary images"},"content":{"rendered":"<div class=\"content\">\r\n\r\nIn my previous postings on this topic, I've discussed the basic image display models in MATLAB - truecolor and indexed. The\r\nImage Processing Toolbox has conventions for two additional image display models: grayscale and binary.\r\n\r\n&nbsp;\r\n<h3>Contents<\/h3>\r\n<div>\r\n<ul>\r\n \t<li><a href=\"#1\">Grayscale image display<\/a><\/li>\r\n \t<li><a href=\"#6\">Controlling the grayscale display range<\/a><\/li>\r\n \t<li><a href=\"#7\">Binary image display<\/a><\/li>\r\n \t<li><a href=\"#9\">Relevant toolbox documentation<\/a><\/li>\r\n<\/ul>\r\n<\/div>\r\n<h3>Grayscale image display<a name=\"1\"><\/a><\/h3>\r\nIf you pass a single argument to the toolbox's two main image display functions (<tt>imtool<\/tt> and <tt>imshow<\/tt>), they'll interpret the input as a grayscale image. Here's an illustration using a simple sinusoid:\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">theta = linspace(0, 2*pi, 256);\r\nI = repmat((-cos(2*theta) + 1)\/2, [256 1]);\r\nh = imshow(I);                <span style=\"color: #228b22;\">% Save the handle for use below.<\/span><\/pre>\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/42\/grayscale_binary_01.png\" hspace=\"5\" vspace=\"5\" \/>\r\n\r\nAs far as MATLAB itself is concerned, this is a scaled indexed image being displayed in a figure with a grayscale colormap\r\ninstalled. Here are the key properties that have been set to control the image display:\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">get(h, <span style=\"color: #a020f0;\">'CDataMapping'<\/span>)<\/pre>\r\n<pre style=\"font-style: oblique;\">ans =\r\n\r\nscaled\r\n\r\n<\/pre>\r\nThe toolbox convention is that, for floating-point images, 0 is displayed as black and 1 is displayed as white.\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">get(gca, <span style=\"color: #a020f0;\">'CLim'<\/span>)<\/pre>\r\n<pre style=\"font-style: oblique;\">ans =\r\n\r\n     0     1\r\n\r\n<\/pre>\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">map = get(gcf, <span style=\"color: #a020f0;\">'Colormap'<\/span>);\r\nmap(1:5, :)                   <span style=\"color: #228b22;\">% Display the first few colormap colors<\/span><\/pre>\r\n<pre style=\"font-style: oblique;\">ans =\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>\r\n<tt>imshow<\/tt> (and <tt>imtool<\/tt>) handle all these details for you.\r\n<h3>Controlling the grayscale display range<a name=\"6\"><\/a><\/h3>\r\n<tt>imshow<\/tt> and <tt>imtool<\/tt> allow you to override the conventional display range and specify your own black and white values. You do this by providing\r\na second input argument, a two-element vector containing the black and white values. In the call to imshow below, 0.4 (and\r\nany lower value) gets displayed as black. The value 0.6 (and any higher value) gets displayed as white.\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">imshow(I, [0.4 0.6])<\/pre>\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/42\/grayscale_binary_02.png\" hspace=\"5\" vspace=\"5\" \/>\r\n<h3>Binary image display<a name=\"7\"><\/a><\/h3>\r\nThe other Image Processing Toolbox image display model is the binary image. If you supply a single input argument that is\r\nlogical, then <tt>imtool<\/tt> and <tt>imshow<\/tt> (as well as many other toolbox functions) interpret that input as a binary image.\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">bw = imread(<span style=\"color: #a020f0;\">'text.png'<\/span>);\r\nislogical(bw)<\/pre>\r\n<pre style=\"font-style: oblique;\">ans =\r\n\r\n     1\r\n\r\n<\/pre>\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">h = imshow(bw);<\/pre>\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/42\/grayscale_binary_03.png\" hspace=\"5\" vspace=\"5\" \/>\r\n<h3>Relevant toolbox documentation<a name=\"9\"><\/a><\/h3>\r\nThe <a href=\"https:\/\/www.mathworks.com\/help\/images\/index.html\">Image Processing Toolbox Users Guide<\/a> has a section called \"Image Types in the Toolbox.\" This section describes the image types binary, indexed, grayscale, and truecolor, and it explains important dynamic range\r\nconventions as well. This is essential information for you to know in order to use the toolbox effectively.\r\n\r\n<script language=\"JavaScript\"> \r\n<!--\r\n    function grabCode_42() {\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='42 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 42';\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('<\/p>\r\n<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>\r\n<p>\\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;\">\r\n<a><span style=\"font-size: x-small; font-style: italic;\">Get\r\nthe MATLAB code<\/span><\/a>\r\n\r\nPublished with MATLAB\u00ae 7.2<\/p>\r\n\r\n<\/div>\r\n<!--\r\n42 ##### SOURCE BEGIN #####\r\n%% All about pixel colors: Grayscale and binary images\r\n% In my previous postings on this topic, I've discussed the basic image\r\n% display models in MATLAB - truecolor and indexed.  The Image Processing\r\n% Toolbox has conventions for two additional image display models:\r\n% grayscale and binary.\r\n\r\n%% Grayscale image display\r\n% If you pass a single argument to the toolbox's two main image display\r\n% functions\r\n% (<https:\/\/www.mathworks.com\/help\/images\/index.htmlimtool.html % |imtool|> and\r\n% <https:\/\/www.mathworks.com\/help\/images\/index.htmlimshow.html % |imshow|>), they'll interpret the input as a grayscale\r\n% image.  Here's an illustration using a simple sinusoid:\r\n\r\ntheta = linspace(0, 2*pi, 256);\r\nI = repmat((-cos(2*theta) + 1)\/2, [256 1]);\r\nh = imshow(I);                % Save the handle for use below.\r\n\r\n%%\r\n% As far as MATLAB itself is concerned, this is a scaled indexed image\r\n% being displayed in a figure with a grayscale colormap installed.  Here\r\n% are the key properties that have been set to control the image display:\r\n\r\nget(h, 'CDataMapping')\r\n\r\n%%\r\n% The toolbox convention is that, for floating-point images, 0 is displayed\r\n% as black and 1 is displayed as white.\r\n\r\nget(gca, 'CLim')\r\n\r\n%%\r\n\r\nmap = get(gcf, 'Colormap');\r\nmap(1:5, :)                   % Display the first few colormap colors\r\n\r\n%%\r\n% |imshow| (and |imtool|) handle all these details for you.\r\n\r\n%% Controlling the grayscale display range\r\n% |imshow| and |imtool| allow you to override the conventional display range\r\n% and specify your own black and white values.  You do this by providing a\r\n% second input argument, a two-element vector containing the black and\r\n% white values.  In the call to imshow below, 0.4 (and any lower value)\r\n% gets displayed as black. The value 0.6 (and any higher value) gets\r\n% displayed as 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% |imtool| and |imshow| (as well as many other toolbox functions) interpret\r\n% that input as a binary image.\r\n\r\nbw = imread('text.png');\r\nislogical(bw)\r\n\r\n%%\r\n\r\nh = imshow(bw);\r\n\r\n%% Relevant toolbox documentation\r\n% The <https:\/\/www.mathworks.com\/help\/images\/index.html % Image Processing Toolbox Users Guide> has a section called\r\n% This section describes the image types binary,\r\n% indexed, grayscale, and truecolor, and it explains important dynamic\r\n% range conventions as well.  This is essential information for you to know\r\n% in order to use the toolbox effectively.\r\n\r\n##### SOURCE END ##### 42\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n\r\nIn my previous postings on this topic, I've discussed the basic image display models in MATLAB - truecolor and indexed. The\r\nImage Processing Toolbox has conventions for two additional image... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2006\/04\/05\/all-about-pixel-colors-grayscale-and-binary-images\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[80,76,36,118,32,116],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/42"}],"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=42"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/42\/revisions"}],"predecessor-version":[{"id":3058,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/42\/revisions\/3058"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=42"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=42"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=42"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}