{"id":32,"date":"2006-02-03T07:00:12","date_gmt":"2006-02-03T12:00:12","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=32"},"modified":"2022-05-29T18:51:55","modified_gmt":"2022-05-29T22:51:55","slug":"all-about-pixel-colors-part-2","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2006\/02\/03\/all-about-pixel-colors-part-2\/","title":{"rendered":"All about pixel colors: Truecolor and indexed images"},"content":{"rendered":"<div class=\"alert alert-info\">\r\n<span class=\"alert_icon icon-alert-info-reverse\"><\/span>\r\n<p class=\"alert_heading\"><strong>Note<\/strong><\/p>\r\n<p>See the following posts for new or updated information about this topic:<\/p>\r\n<li><a href=\"https:\/\/blogs.mathworks.com\/steve\/2016\/02\/09\/matlab-image-display-from-data-values-to-pixel-colors\/\">MATLAB image display - from data values to pixel colors<\/a><\/li>\r\n<li><a href=\"https:\/\/blogs.mathworks.com\/steve\/2016\/02\/22\/matlab-image-display-truecolor-and-indexed-images\/\">MATLAB image display - truecolor and indexed images<\/a><\/li>\r\n<li><a href=\"https:\/\/blogs.mathworks.com\/steve\/2016\/02\/29\/matlab-image-display-scaled-indexed-images\/\">MATLAB image display - scaled indexed images<\/a><\/li>\r\n<li><a href=\"https:\/\/blogs.mathworks.com\/steve\/2016\/03\/14\/matlab-image-display-grayscale-and-binary-images\/\">MATLAB image display - grayscale and binary images<\/a><\/li>\r\n<li>MATLAB image display - autoscaling values with imshow<\/li>\r\n<\/div>\r\n<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/blogs.mathworks.com\/steve\/?p=31\">Last week<\/a> I posed this question: How does MATLAB associate the value of a particular matrix element with a color displayed\r\n         on the screen?  Let's start by exploring MATLAB's two basic pixel-color display models:\r\n      <\/p>\r\n      <div>\r\n         <ul>\r\n            <li>Matrix element values specify pixel colors directly<\/li>\r\n            <li>Matrix element values specify pixel colors indirectly, through the figure's colormap<\/li>\r\n         <\/ul>\r\n      <\/div>\r\n   <\/introduction>\r\n   <h2>Contents<\/h2>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Truecolor images<\/a><\/li>\r\n         <li><a href=\"#3\">Indexed images<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h2>Truecolor images<a name=\"1\"><\/a><\/h2>\r\n   <p>The matrix (or array) of pixel values is stored in the Handle Graphics image object's CData property.  If the CData array\r\n      is a three-dimensional array with size M-by-N-by-3, then the pixel values specify the colors directly as a mix of red (first\r\n      plane), green (second plane), and blue (third plane).  We sometimes call such an image a \"truecolor\" image.  (I believe this\r\n      term originated in the computer graphics display industry.  Someone please correct me if I'm wrong.)\r\n   <\/p>\r\n   <p>Here's an illustrative image with just three pixels: red, blue, and yellow.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">plane_1 = [1 0 1];\r\nplane_2 = [0 0 1];\r\nplane_3 = [0 1 0];\r\nrgb = cat(3, plane_1, plane_2, plane_3);\r\nsize(rgb)\r\n\r\nimage(rgb)\r\naxis <span style=\"color: #A020F0\">image<\/span>\r\ntitle(<span style=\"color: #A020F0\">'Truecolor image with one red, one blue, and one yellow pixel'<\/span>)<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n     1     3     3\r\n\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/32\/pixel_colors_2_01.png\"> <p>With truecolor images, changing the colormap has no effect on the image colors displayed.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">colormap(hot)\r\ntitle(<span style=\"color: #A020F0\">'Changing the figure colormap does not affect the pixel colors'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/32\/pixel_colors_2_02.png\"> <h2>Indexed images<a name=\"3\"><\/a><\/h2>\r\n   <p>If the image CData is two-dimensional, then the CData values are treated as lookup indices into the figure's colormap.  As\r\n      an example, let's use an indexed image that ships with MATLAB, clown.mat (Ned's favorite).\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">s = load(<span style=\"color: #A020F0\">'clown'<\/span>)  <span style=\"color: #228B22\">% This the functional form of load.  This form returns<\/span>\r\n                   <span style=\"color: #228B22\">% a structure whose fields are the variables stored<\/span>\r\n                   <span style=\"color: #228B22\">% in the MAT-file.<\/span><\/pre><pre style=\"font-style:oblique\">\r\ns = \r\n\r\n          X: [200x320 double]\r\n        map: [81x3 double]\r\n    caption: [2x1 char]\r\n\r\n<\/pre><p>The <tt>X<\/tt> and <tt>map<\/tt> variables stored in clown.mat are both necessary to display the image correctly.  <tt>X<\/tt> contains the pixel values, and <tt>map<\/tt> contains the associated colormap.\r\n   <\/p>\r\n   <p>To get the color of the (5,5) pixel, first see what <tt>X(5,5)<\/tt> is:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">s.X(5,5)<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n    61\r\n\r\n<\/pre><p>Then use that value as a row index into the colormap matrix, map:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">s.map(61,:)<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n    0.9961    0.5781    0.1250\r\n\r\n<\/pre><p>So the (5,5) pixel has a lot of red, some green, and a small amount of blue.<\/p>\r\n   <p>Displaying the image requires two MATLAB commands, one to create the image and one to set the figure's colormap:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">image(s.X)\r\ncolormap(s.map)\r\ntitle(<span style=\"color: #A020F0\">'Indexed image'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/32\/pixel_colors_2_03.png\"> <p>Unlike truecolor images, indexed images are affected by changes in the figure's colormap.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">colormap(cool)\r\ntitle(<span style=\"color: #A020F0\">'Indexed image displays incorrectly if you use the wrong colormap'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/32\/pixel_colors_2_04.png\"> <p>In my <a href=\"https:\/\/blogs.mathworks.com\/steve\/?p=31\">first post on this subject<\/a>, I suggested that there might really be three pixel-color display models in MATLAB instead\r\n      of two.  The third display model is a variation of the indexed image model.\r\n   <\/p>\r\n   <p>I'll talk about that next time.<\/p>\r\n <script language=\"JavaScript\"> \r\n<!--\r\n    function grabCode_32() {\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='32 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 32';\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('<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      <\/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\"><br><a href=\"javascript:grabCode_32()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code<\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.1<br><\/p>\r\n<\/div>\r\n<!--\r\n32 ##### SOURCE BEGIN #####\r\n%% All about pixel colors, part 2\r\n% Last week I posed this question: How does MATLAB associate the value of a\r\n% particular matrix element with a color displayed on the screen?  Let's\r\n% start by exploring MATLAB's two basic pixel-color display models:\r\n%\r\n% * Matrix element values specify pixel colors directly\r\n% * Matrix element values specify pixel colors indirectly, through the\r\n% figure's colormap\r\n\r\n%% Truecolor images\r\n% The matrix (or array) of pixel values is stored in the Handle Graphics\r\n% image object's CData property.  If the CData array is a\r\n% three-dimensional array with size M-by-N-by-3, then the pixel values\r\n% specify the colors directly as a mix of red (first plane), green (second\r\n% plane), and blue (third plane).  We sometimes call such an image a\r\n% \"truecolor\" image.  (I believe this term originated in the computer\r\n% graphics display industry.  Someone please correct me if I'm wrong.)\r\n%\r\n% Here's an illustrative image with just three pixels: red, blue, and\r\n% yellow.\r\n\r\nplane_1 = [1 0 1];\r\nplane_2 = [0 0 1];\r\nplane_3 = [0 1 0];\r\nrgb = cat(3, plane_1, plane_2, plane_3);\r\nsize(rgb)\r\n\r\nimage(rgb)\r\naxis image\r\ntitle('Truecolor image with one red, one blue, and one yellow pixel')\r\n\r\n%%\r\n% With truecolor images, changing the colormap has no effect on the image\r\n% colors displayed.\r\n\r\ncolormap(hot)\r\ntitle('Changing the figure colormap does not affect the pixel colors')\r\n\r\n%% Indexed images\r\n% If the image CData is two-dimensional, then the CData values are treated\r\n% as lookup indices into the figure's colormap.  As an example, let's use \r\n% an indexed image \r\n% that ships with MATLAB, clown.mat (Ned's favorite).\r\n\r\ns = load('clown')  % This the functional form of load.  This form returns\r\n                   % a structure whose fields are the variables stored\r\n                   % in the MAT-file.\r\n\r\n%%\r\n% The |X| and |map| variables stored in clown.mat are both necessary to\r\n% display the image correctly.  |X| contains the pixel values, and |map|\r\n% contains the associated colormap.\r\n%\r\n% To get the color of the (5,5) pixel, first see what |X(5,5)| is:\r\n\r\ns.X(5,5)\r\n\r\n%%\r\n% Then use that value as a row index into the colormap matrix, map:\r\n\r\ns.map(61,:)\r\n\r\n%%\r\n% So the (5,5) pixel has a lot of red, some green, and a small amount of\r\n% blue.\r\n%\r\n% Displaying the image requires two MATLAB commands, one to create the\r\n% image and one to set the figure's colormap:\r\n\r\nimage(s.X)\r\ncolormap(s.map)\r\ntitle('Indexed image')\r\n\r\n%%\r\n% Unlike truecolor images, indexed images are affected by changed in the\r\n% figure's colormap.\r\n\r\ncolormap(cool)\r\ntitle('Indexed image displays incorrectly if you use the wrong colormap')\r\n\r\n%%\r\n% In my first post on this subject, I suggested that there might really be\r\n% three pixel-color display models in MATLAB instead of two.  The third\r\n% display model is a variation of the indexed image model.\r\n%\r\n% I'll talk about that next time.\r\n\r\n\r\n##### SOURCE END ##### 32\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n\r\nNote\r\nSee the following posts for new or updated information about this topic:\r\nMATLAB image display - from data values to pixel colors\r\nMATLAB image display - truecolor and indexed... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2006\/02\/03\/all-about-pixel-colors-part-2\/\">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":[50,46,48,52],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/32"}],"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=32"}],"version-history":[{"count":5,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/32\/revisions"}],"predecessor-version":[{"id":5543,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/32\/revisions\/5543"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=32"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=32"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=32"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}