{"id":73,"date":"2007-01-10T11:58:09","date_gmt":"2007-01-10T16:58:09","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=73"},"modified":"2016-07-29T16:12:55","modified_gmt":"2016-07-29T21:12:55","slug":"colormap-manipulations","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2007\/01\/10\/colormap-manipulations\/","title":{"rendered":"Colormap Manipulations"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>We use color as a dimension to understand the world. For some datasets, judicious choice of colormaps provides a useful means\r\n         for gleaning information.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Basic Colormap Manipulation<\/a><\/li>\r\n         <li><a href=\"#8\">Other \"Simple\" Colormap Manipulations<\/a><\/li>\r\n         <li><a href=\"#12\">Pitfalls<\/a><\/li>\r\n         <li><a href=\"#17\">Resources<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Basic Colormap Manipulation<a name=\"1\"><\/a><\/h3>\r\n   <p>Let's start with a grayscale image from the <a href=\"https:\/\/www.mathworks.com\/products\/image\/\">Image Processing Toolbox<\/a> and display it using <tt>imshow<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">imshow <span style=\"color: #A020F0\">cameraman.tif<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/73\/colormapManip_01.png\"> <p>and let's take a quick look at the colormap<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">cm = get(1,<span style=\"color: #A020F0\">'colormap'<\/span>);\r\nfigure\r\nrgbplot(cm)\r\nlegend(get(gca,<span style=\"color: #A020F0\">'children'<\/span>),{<span style=\"color: #A020F0\">'blue'<\/span> <span style=\"color: #A020F0\">'green'<\/span> <span style=\"color: #A020F0\">'red'<\/span>},<span style=\"color: #A020F0\">'location'<\/span>,<span style=\"color: #A020F0\">'NorthWest'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/73\/colormapManip_02.png\"> <p>or superpose a <tt>colorbar<\/tt> on the image plot.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">figure(1)\r\ncolorbar <span style=\"color: #A020F0\">SouthOutside<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/73\/colormapManip_03.png\"> <p>It's difficult at first to see much detail about the man's clothing. Using the MATLAB function <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/brighten.html\"><tt>brighten<\/tt><\/a>, we can change the colormap and see more detail in the darker regions.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">brighten(.2)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/73\/colormapManip_04.png\"> <p>Notice the extra detail around the buttons and glove when we brighten the colormap more (at the expense of the lighter background).<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">brighten(.4)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/73\/colormapManip_05.png\"> <p>Now let's see what the individual channels look like.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">cm = get(1,<span style=\"color: #A020F0\">'colormap'<\/span>);\r\nfigure(2)\r\nrgbplot(cm)\r\nlegend(get(gca,<span style=\"color: #A020F0\">'children'<\/span>),{<span style=\"color: #A020F0\">'blue'<\/span> <span style=\"color: #A020F0\">'green'<\/span> <span style=\"color: #A020F0\">'red'<\/span>},<span style=\"color: #A020F0\">'location'<\/span>,<span style=\"color: #A020F0\">'NorthWest'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/73\/colormapManip_06.png\"> <pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">close(2)<\/pre><h3>Other \"Simple\" Colormap Manipulations<a name=\"8\"><\/a><\/h3>\r\n   <p>Colormaps contain are 3-column matrices with double values lying between 0 and 1.  So what manipulations can we easily do\r\n      to create new colormaps from existing ones? Here are some simple ones.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">colorbar <span style=\"color: #A020F0\">off<\/span>\r\ncolormap(1-cm)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/73\/colormapManip_07.png\"> <p>Get another monotonic colormap and see other colormap choices.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">help <span style=\"color: #A020F0\">pink<\/span><\/pre><pre style=\"font-style:oblique\"> PINK   Pastel shades of pink color map\r\n    PINK(M) returns an M-by-3 matrix containing a \"pink\" colormap.\r\n    PINK, by itself, is the same length as the current figure's\r\n    colormap. If no figure exists, MATLAB creates one.\r\n \r\n    For example, to reset the colormap of the current figure:\r\n \r\n              colormap(pink)\r\n \r\n    See also HSV, GRAY, HOT, COOL, BONE, COPPER, FLAG, \r\n    COLORMAP, RGBPLOT.\r\n\r\n<\/pre><p>This is not quite the sepia tones for an old-fashioned photograph look, but it's getting close.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">colormap(pink)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/73\/colormapManip_08.png\"> <pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">colormap(fliplr(pink))<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/73\/colormapManip_09.png\"> <h3>Pitfalls<a name=\"12\"><\/a><\/h3>\r\n   <p>You can run into some pitfalls with colormaps.  Here are a few to watch out for.<\/p>\r\n   <div>\r\n      <ul>\r\n         <li>Using the wrong length colormap can lead to very poor results.  This makes the display look totally wrong.  Try this.<\/li>\r\n      <\/ul>\r\n   <\/div><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">load <span style=\"color: #A020F0\">clown<\/span>\r\nimage(X),colormap(map)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/73\/colormapManip_10.png\"> <p>Now make the colormap gray<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">colormap(gray(length(map)))<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/73\/colormapManip_11.png\"> <p>Next display the clown using an inappropriate colormap.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">colormap(gray(256))<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/73\/colormapManip_12.png\"> <div>\r\n      <ul>\r\n         <li>Colormap is not double or not in the correct range (between 0 and 1).<\/li>\r\n         <li>In MATLAB version 4, the default colormap was <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/colormap.html\"><tt>hsv<\/tt><\/a>. The <tt>hsv<\/tt> colormap starts and ends with red, making it is very hard to interpret colors as high or low.\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">close\r\nimage(1:64)\r\naxis <span style=\"color: #A020F0\">off<\/span>\r\ncolormap(hsv(64))<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/73\/colormapManip_13.png\"> <p>Starting in MATLAB 5, the default colormap is <tt>jet<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">colormap(jet)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/73\/colormapManip_14.png\"> <h3>Resources<a name=\"17\"><\/a><\/h3>\r\n   <p>Here are some resources for learning more about color and colormaps in MATLAB.<\/p>\r\n   <div>\r\n      <ul>\r\n         <li>Steve's blog article: <a href=\"https:\/\/blogs.mathworks.com\/steve\/?p=57\"> A Lab-based uniform color scale<\/a><\/li>\r\n         <li>Eos, Vol. 85, No. 40, 5 October 2004, The End of the Rainbow?  Color Schemes for Improved Data Graphics<\/li>\r\n         <li>MATLAB Central File Exchange: <a title=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadCategory.do?objectType=category&amp;objectId=128&amp;objectName=Color (link no longer works)\">Image Processing - Color<\/a><\/li>\r\n         <li>MATLAB's <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/colormapeditor.html\"><tt>colormapeditor<\/tt><\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>What additional resources on color would be useful?  Let me know <a href=\"?p=73#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_768d19ac51f5462f920813e84114cf38() {\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='768d19ac51f5462f920813e84114cf38 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 768d19ac51f5462f920813e84114cf38';\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 = 'Loren Shure';\r\n        copyright = 'Copyright 2007 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-->\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_768d19ac51f5462f920813e84114cf38()\"><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.3<br><\/p>\r\n<\/div>\r\n<!--\r\n768d19ac51f5462f920813e84114cf38 ##### SOURCE BEGIN #####\r\n%% Colormap Manipulations\r\n% We use color as a dimension to understand the world. For some datasets,\r\n% judicious choice of colormaps provides a useful means for gleaning\r\n% information.\r\n%\r\n%% Basic Colormap Manipulation\r\n% Let's start with a grayscale image from the\r\n% <https:\/\/www.mathworks.com\/products\/image\/ Image Processing Toolbox> and\r\n% display it using\r\n% <https:\/\/www.mathworks.com\/help\/images\/index.htmlimshow.html |imshow|>.\r\nimshow cameraman.tif \r\n%%\r\n% and let's take a quick look at the colormap\r\ncm = get(1,'colormap');\r\nfigure\r\nrgbplot(cm)\r\nlegend(get(gca,'children'),{'blue' 'green' 'red'},'location','NorthWest')\r\n%%\r\n% or superpose a <https:\/\/www.mathworks.com\/help\/images\/index.htmlcolorbar.html |colorbar|>\r\n% on the image plot.\r\nfigure(1)\r\ncolorbar SouthOutside\r\n%%\r\n% It's difficult at first to see much detail about the man's clothing.\r\n% Using the MATLAB function\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/brighten.html |brighten|>,\r\n% we can change the colormap and see more detail in the darker regions.\r\nbrighten(.2)\r\n%%\r\n% Notice the extra detail around the buttons and glove when we\r\n% brighten the colormap more (at the expense of the lighter background).\r\nbrighten(.4)\r\n%%\r\n% Now let's see what the individual channels look like.\r\ncm = get(1,'colormap');\r\nfigure(2)\r\nrgbplot(cm)\r\nlegend(get(gca,'children'),{'blue' 'green' 'red'},'location','NorthWest')\r\n%%\r\nclose(2)\r\n%% Other \"Simple\" Colormap Manipulations\r\n% Colormaps contain are 3-column matrices with double values lying between\r\n% 0 and 1.  So what manipulations can we easily do to create new colormaps\r\n% from existing ones? Here are some simple ones.\r\ncolorbar off\r\ncolormap(1-cm)\r\n%%\r\n% Get another monotonic colormap and see other colormap choices.  \r\nhelp pink\r\n%%\r\n% This is not quite the sepia tones for an old-fashioned photograph look,\r\n% but it's getting close.\r\ncolormap(pink)\r\n%%\r\ncolormap(fliplr(pink))\r\n%% Pitfalls\r\n% You can run into some pitfalls with colormaps.  Here are a few to watch\r\n% out for.\r\n%\r\n% * Using the wrong length colormap can lead to very poor results.  This\r\n% makes the display look totally wrong.  Try this.\r\n%\r\nload clown\r\nimage(X),colormap(map)\r\n%%\r\n% Now make the colormap gray\r\ncolormap(gray(length(map)))\r\n%%\r\n% Next display the clown using an inappropriate colormap.\r\ncolormap(gray(256))\r\n%%\r\n%\r\n% * Colormap is not double or not in the correct range (between 0 and 1).\r\n% * In MATLAB version 4, the default colormap was\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/colormap.html\r\n% |hsv|>.\r\n% The |hsv| colormap starts and ends with red, making it is very hard to\r\n% interpret colors as high or low.  \r\nclose\r\nimage(1:64)\r\naxis off\r\ncolormap(hsv(64))\r\n%%\r\n% Starting in MATLAB 5, the default\r\n% colormap is |jet|.\r\ncolormap(jet)\r\n%% Resources\r\n% Here are some resources for learning more about color and colormaps in\r\n% MATLAB.  \r\n%\r\n% * Steve's blog article: <https:\/\/blogs.mathworks.com\/steve\/?p=57  A Lab-based uniform color scale>\r\n% * Eos, Vol. 85, No. 40, 5 October 2004,\r\n% The End of the Rainbow?  Color Schemes for Improved Data Graphics\r\n% * MATLAB Central File Exchange: <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadCategory.do?objectType=category&objectId=128&objectName=Color Image Processing - Color>\r\n% * MATLAB's <https:\/\/www.mathworks.com\/help\/matlab\/ref\/colormapeditor.html |colormapeditor|>\r\n%\r\n% What additional resources on color would be useful?  Let me know\r\n% <?p=73#respond here>.\r\n##### SOURCE END ##### 768d19ac51f5462f920813e84114cf38\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      We use color as a dimension to understand the world. For some datasets, judicious choice of colormaps provides a useful means\r\n         for gleaning information.\r\n      \r\n  ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2007\/01\/10\/colormap-manipulations\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[15],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/73"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=73"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/73\/revisions"}],"predecessor-version":[{"id":1818,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/73\/revisions\/1818"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=73"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=73"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=73"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}