{"id":176,"date":"2009-03-25T15:33:31","date_gmt":"2009-03-25T15:33:31","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2009\/03\/25\/four-color-images\/"},"modified":"2016-08-04T08:19:35","modified_gmt":"2016-08-04T13:19:35","slug":"four-color-images","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2009\/03\/25\/four-color-images\/","title":{"rendered":"Four Color Images"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>I recently attended a show at the Institute for Contemporary Art in Boston featuring work by Shepard Fairey. It got me thinking how simple it can be in MATLAB to make a 4-color image and adjust it however you like.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Import Image<\/a><\/li>\r\n         <li><a href=\"#3\">Define Four Colors<\/a><\/li>\r\n         <li><a href=\"#4\">Segment Gray Image into Four Levels<\/a><\/li>\r\n         <li><a href=\"#7\">Change the Order of Colors<\/a><\/li>\r\n         <li><a href=\"#8\">Change Ranges for Four Colors to Bring Out Details<\/a><\/li>\r\n         <li><a href=\"#9\">Colormaps are Important<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Import Image<a name=\"1\"><\/a><\/h3>\r\n   <p>First, let me get an image.  This one is a gray-scale image.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">Ic = imread(<span style=\"color: #A020F0\">'pout.tif'<\/span>);\r\nimshow(Ic)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/176\/shepardFairey_01.png\"> <p>Note: I am using <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/toolbox\/images\/imshow.html\"><tt>imshow<\/tt><\/a> from <a href=\"https:\/\/www.mathworks.com\/products\/image\/\">Image Processing Toolbox<\/a> to help manage the color and axes scaling.\r\n   <\/p>\r\n   <h3>Define Four Colors<a name=\"3\"><\/a><\/h3>\r\n   <p>Next let me choose a palette of colors, and display them.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">lilac = [.5 .4 .9];\r\ndarkblue = [.1 .3 .8];\r\nlightblue = [0 .5 .6];\r\nred = [.8 0 0];\r\ncm = [darkblue;  red; lightblue;  lilac];\r\nimage([1 3;2 4]),colormap(cm), axis <span style=\"color: #A020F0\">off<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/176\/shepardFairey_02.png\"> <h3>Segment Gray Image into Four Levels<a name=\"4\"><\/a><\/h3>\r\n   <p>Next I choose levels for grouping pixel values into 4 color bins.  The minimum and maximum values of my image are close enough\r\n      to 0 and 255 to not bother stretching the range.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">min(Ic(:))\r\nmax(Ic(:))<\/pre><pre style=\"font-style:oblique\">ans =\r\n   74\r\nans =\r\n  224\r\n<\/pre><p>My first attempt at binning creates equal size bins.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">levels = linspace(0,255,5)<\/pre><pre style=\"font-style:oblique\">levels =\r\n            0        63.75        127.5       191.25          255\r\n<\/pre><p>Next I create a new image by combining 4 binary images with the appropriate multipliers.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">Idb = (Ic &lt;= levels(2));\r\nIr = Ic&lt;=levels(3) &amp; Ic&gt;levels(2);\r\nIlb = Ic&lt;=levels(4) &amp; Ic&gt;levels(3);\r\nIpy = Ic&lt;=levels(5) &amp; Ic&gt;levels(4);\r\nIc4 = Idb+2*Ir+3*Ilb+4*Ipy;\r\nimshow(Ic4,cm)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/176\/shepardFairey_03.png\"> <h3>Change the Order of Colors<a name=\"7\"><\/a><\/h3>\r\n   <p>What happens when I change the color order?  Features appear different with different color selections.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">cm = [darkblue;  lightblue;  lilac; red];\r\nimshow(Ic4,cm)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/176\/shepardFairey_04.png\"> <h3>Change Ranges for Four Colors to Bring Out Details<a name=\"8\"><\/a><\/h3>\r\n   <p>Now I finetune the color bins to emphasis more detail from the original image.  I then recalculate the binary images according\r\n      to the new bins and recombine them.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">levels = [0 90 128 155 255]\r\nIdb = (Ic &lt;= levels(2));\r\nIr = Ic&lt;=levels(3) &amp; Ic&gt;levels(2);\r\nIlb = Ic&lt;=levels(4) &amp; Ic&gt;levels(3);\r\nIpy = Ic&lt;=levels(5) &amp; Ic&gt;levels(4);\r\nIc4 = Idb+2*Ir+3*Ilb+4*Ipy;\r\nimshow(Ic4,cm)<\/pre><pre style=\"font-style:oblique\">levels =\r\n     0    90   128   155   255\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/176\/shepardFairey_05.png\"> <h3>Colormaps are Important<a name=\"9\"><\/a><\/h3>\r\n   <p>Choosing appropriate colormaps for displaying data is very important. You need to be careful to not exaggerate unimportant\r\n      details.  Here's an interesting reference from one of my geophysics periodicals.\r\n   <\/p>\r\n   <p><i>A. Light &amp; P.J. Bartlein, \"The End of the Rainbow? Color Schemes for Improved Data Graphics,\" Eos,Vol. 85, No. 40, 5 October\r\n         2004.<\/i><\/p>\r\n   <p>Do you have any special colormaps you use for exploring or displaying your data?  If so, can you post information (perhaps\r\n      with links of plots to look at) <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=176#respond\">here<\/a>?\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_aedfb18bf6404e29bfcd11d971aef994() {\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='aedfb18bf6404e29bfcd11d971aef994 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' aedfb18bf6404e29bfcd11d971aef994';\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 2009 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_aedfb18bf6404e29bfcd11d971aef994()\"><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.8<br><\/p>\r\n<\/div>\r\n<!--\r\naedfb18bf6404e29bfcd11d971aef994 ##### SOURCE BEGIN #####\r\n%% Four Color Images\r\n% I recently attended a show at the Institute for Contemporary Art in\r\n% Boston featuring work by \r\n% <http:\/\/www.icaboston.org\/exhibitions\/exhibit\/fairey\/ Shepard Fairey>.\r\n% It got me thinking how simple it can be in MATLAB to make a 4-color image\r\n% and adjust it however you like.\r\n%% Import Image\r\n% First, let me get an image.  This one is a gray-scale image.\r\nIc = imread('pout.tif');\r\nimshow(Ic)\r\n%%\r\n% Note: I am using\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009a\/toolbox\/images\/imshow.html |imshow|>\r\n% from <https:\/\/www.mathworks.com\/products\/image\/ Image Processing Toolbox>\r\n% to help manage the color and axes scaling.\r\n%% Define Four Colors\r\n% Next let me choose a palette of colors, and display them.\r\nlilac = [.5 .4 .9];\r\ndarkblue = [.1 .3 .8];\r\nlightblue = [0 .5 .6];\r\nred = [.8 0 0];\r\ncm = [darkblue;  red; lightblue;  lilac];\r\nimage([1 3;2 4]),colormap(cm), axis off\r\n%% Segment Gray Image into Four Levels\r\n% Next I choose levels for grouping pixel values into 4 color bins.  The\r\n% minimum and maximum values of my image are close enough to 0 and 255 to\r\n% not bother stretching the range.\r\nmin(Ic(:))\r\nmax(Ic(:))\r\n%%\r\n% My first attempt at binning creates equal size bins.\r\nlevels = linspace(0,255,5)\r\n%%\r\n% Next I create a new image by combining 4 binary images with the\r\n% appropriate multipliers.\r\nIdb = (Ic <= levels(2));\r\nIr = Ic<=levels(3) & Ic>levels(2);\r\nIlb = Ic<=levels(4) & Ic>levels(3);\r\nIpy = Ic<=levels(5) & Ic>levels(4);\r\nIc4 = Idb+2*Ir+3*Ilb+4*Ipy;\r\nimshow(Ic4,cm)\r\n%% Change the Order of Colors\r\n% What happens when I change the color order?  Features appear different\r\n% with different color selections.\r\ncm = [darkblue;  lightblue;  lilac; red];\r\nimshow(Ic4,cm)\r\n%% Change Ranges for Four Colors to Bring Out Details\r\n% Now I finetune the color bins to emphasis more detail from the original\r\n% image.  I then recalculate the binary images according to the new bins\r\n% and recombine them.\r\nlevels = [0 90 128 155 255]\r\nIdb = (Ic <= levels(2));\r\nIr = Ic<=levels(3) & Ic>levels(2);\r\nIlb = Ic<=levels(4) & Ic>levels(3);\r\nIpy = Ic<=levels(5) & Ic>levels(4);\r\nIc4 = Idb+2*Ir+3*Ilb+4*Ipy;\r\nimshow(Ic4,cm)\r\n%% Colormaps are Important\r\n% Choosing appropriate colormaps for displaying data is very important.\r\n% You need to be careful to not exaggerate unimportant details.  Here's an\r\n% interesting reference from one of my geophysics periodicals.\r\n%\r\n% _A. Light & P.J. Bartlein, \"The End of the Rainbow? Color Schemes for\r\n% Improved Data Graphics,\" Eos,Vol. 85, No. 40, 5 October 2004._\r\n%\r\n% Do you have any special colormaps you use for exploring or displaying\r\n% your data?  If so, can you post information (perhaps with links of plots\r\n% to look at) <https:\/\/blogs.mathworks.com\/loren\/?p=176#respond here>?\r\n##### SOURCE END ##### aedfb18bf6404e29bfcd11d971aef994\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      I recently attended a show at the Institute for Contemporary Art in Boston featuring work by Shepard Fairey. It got me thinking how simple it can be in MATLAB to make a 4-color image and... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2009\/03\/25\/four-color-images\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[27],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/176"}],"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=176"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/176\/revisions"}],"predecessor-version":[{"id":1938,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/176\/revisions\/1938"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}