{"id":4095,"date":"2020-04-27T16:19:20","date_gmt":"2020-04-27T20:19:20","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=4095"},"modified":"2020-08-18T09:10:42","modified_gmt":"2020-08-18T13:10:42","slug":"making-color-spectrum-plots-part-1","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2020\/04\/27\/making-color-spectrum-plots-part-1\/","title":{"rendered":"Making Color Spectrum Plots &#8211; Part 1"},"content":{"rendered":"<div class=\"content\"><p>The new edition of <i>Digital Image Processing Using MATLAB<\/i> (<a href=\"http:\/\/www.imageprocessingplace.com\/DIPUM-3E\/dipum3e_main_page.htm\"><b>DIPUM3E<\/b><\/a>) contains a number of MATLAB functions related to color, color calculations, and color visualization. I wrote about functions for displaying color swatches in my <a href=\"https:\/\/blogs.mathworks.com\/steve\/2020\/03\/10\/how-to-display-color-swatches\/\">March 10 post<\/a>. You can find some of the functions in <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/64161-matlab-color-tools\">MATLAB Color Tools<\/a> on the File Exchange, as well as in the <a href=\"https:\/\/github.com\/dipum\/dipum-toolbox\">GitHub repository<\/a> containing the MATLAB source code for the book.<\/p><p>A MathWorks friend asked me, earlier this year, why I chose to write the specific color-related functions that I did. The answer is that I wrote the functions that would help tell the story I wanted to tell. For example, I wanted to talk early in the chapter about the interactions between illumination, object reflectance, and the color sensitivity of retinal receptors in the eye. To help with that story, I wanted to include plots like <a href=\"http:\/\/www.imageprocessingplace.com\/DIPUM-3E\/dipum3e_main_page.htm\"><b>DIPUM3E<\/b><\/a> Figure 7.2(a):<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/illuminant-d65.png\" alt=\"\"> <\/p><p>I thought I would give you a tour of the various algorithms and functions that went into making this and similar plots, including:<\/p><div><ul><li>MATLAB functions <tt>readtable<\/tt>, <tt>interp1<\/tt>, <tt>conv2<\/tt>, <tt>linspace<\/tt>, <tt>colorbar<\/tt><\/li><li>Image Processing Toolbox functions <tt>xyz2rgb<\/tt>, <tt>lin2rgb<\/tt><\/li><li><a href=\"http:\/\/www.imageprocessingplace.com\/DIPUM-3E\/dipum3e_main_page.htm\"><b>DIPUM3E<\/b><\/a> functions <tt>illuminant<\/tt>, <tt>lambda2xyz<\/tt>, <tt>colorMatchingFunctions<\/tt>, <tt>spectrumColors<\/tt>, <tt>spectrumBar<\/tt><\/li><\/ul><\/div><p>I expect the tour to take a couple more blog posts.<\/p><p>First, where does the data in the plot come from? The curve is called a relative spectral power distribution curve. This particular curve is for a standard <i>illuminant<\/i> called D65, which is intended to approximate average mid-day daylight. The curve is a reference curve created by the <i>International Commission on Illumination<\/i>, usually written as <i>CIE<\/i> for the French acronym.<\/p><p>I could have provided this standard data in a MAT-file, I suppose, but I chose instead to provide it as a spreadsheet file, Illuminants.xlsx. Here's what it looks like:<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/excel-illuminants.png\" alt=\"\"> <\/p><p>Along the bottom, you can see that the file contains several sheets: D50, D55, D65, D75, A, C, and F7. These sheets contain the data for different types of illuminants.<\/p><p>My tool of choice for reading an Excel file like this is <tt>readtable<\/tt>. In the call below, I indicate that I don't want <tt>readtable<\/tt> to invoke Excel (this is now the default behavior), and I also indicate that I want to read the D65 sheet.<\/p><pre class=\"codeinput\">T = readtable(<span class=\"string\">'Illuminants.xlsx'<\/span>,<span class=\"string\">'UseExcel'<\/span>,false,<span class=\"string\">'Sheet'<\/span>,<span class=\"string\">'D65'<\/span>);\r\n<\/pre><p>The <a href=\"http:\/\/www.imageprocessingplace.com\/DIPUM-3E\/dipum3e_main_page.htm\"><b>DIPUM3E<\/b><\/a> function <tt>illuminant<\/tt> is basically just this call to <tt>readtable<\/tt>.<\/p><p>And here's what the resulting MATLAB table looks like:<\/p><pre class=\"codeinput\">head(T)\r\n<\/pre><pre class=\"codeoutput\">\r\nans =\r\n\r\n  8&times;2 table\r\n\r\n    lambda      S   \r\n    ______    ______\r\n\r\n     300      0.0341\r\n     305      1.6643\r\n     310      3.2945\r\n     315      11.765\r\n     320      20.236\r\n     325      28.645\r\n     330      37.053\r\n     335      38.501\r\n\r\n<\/pre><p>I can produce a basic plot like this:<\/p><pre class=\"codeinput\">plot(T.lambda,T.S)\r\nxlabel(<span class=\"string\">'wavelength (nm)'<\/span>)\r\nylabel(<span class=\"string\">'Relative spectral power'<\/span>)\r\ntitle(<span class=\"string\">'Illuminant D65'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/spectrum_bar_explanation_01.png\" alt=\"\"> <p>Next time, I'll talk about computing the rainbow colors that appear at the bottom of <a href=\"http:\/\/www.imageprocessingplace.com\/DIPUM-3E\/dipum3e_main_page.htm\"><b>DIPUM3E<\/b><\/a> Figure 7.2(a):<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/illuminant-d65.png\" alt=\"\"> <\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_dbaf04c26fb54dd0833223213eb6b36c() {\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='dbaf04c26fb54dd0833223213eb6b36c ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' dbaf04c26fb54dd0833223213eb6b36c';\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 2020 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_dbaf04c26fb54dd0833223213eb6b36c()\"><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; R2020a<br><\/p><\/div><!--\r\ndbaf04c26fb54dd0833223213eb6b36c ##### SOURCE BEGIN #####\r\n%%\r\n% The new edition of _Digital Image Processing Using MATLAB_ \r\n% (<http:\/\/www.imageprocessingplace.com\/DIPUM-3E\/dipum3e_main_page.htm \r\n% *DIPUM3E*>)\r\n% contains a number of MATLAB functions related to color, color\r\n% calculations, and color visualization. I wrote about functions for\r\n% displaying color swatches in my <https:\/\/blogs.mathworks.com\/steve\/2020\/03\/10\/how-to-display-color-swatches\/ \r\n% March 10 post>. You can find some of the functions in\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/64161-matlab-color-tools\r\n% MATLAB Color Tools> on the File Exchange, as well as in the\r\n% <https:\/\/github.com\/dipum\/dipum-toolbox GitHub repository> containing\r\n% the MATLAB source code for the book.\r\n%\r\n% A MathWorks friend asked me, earlier this year, why I chose to write\r\n% the specific color-related functions that I did. The answer is that I\r\n% wrote the functions that would help tell the story I wanted to tell.\r\n% For example, I wanted to talk early in the chapter about the\r\n% interactions between illumination, object reflectance, and the color\r\n% sensitivity of retinal receptors in the eye. To help with that story,\r\n% I wanted to include plots like <http:\/\/www.imageprocessingplace.com\/DIPUM-3E\/dipum3e_main_page.htm \r\n% *DIPUM3E*> Figure 7.2(a):\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/steve\/files\/illuminant-d65.png>>\r\n%\r\n% I thought I would give you a tour of the various algorithms and functions \r\n% that went into making this and similar plots, including:\r\n%\r\n% * MATLAB functions |readtable|, |interp1|, |conv2|, |linspace|,\r\n% |colorbar|\r\n% * Image Processing Toolbox functions |xyz2rgb|, |lin2rgb|\r\n% * <http:\/\/www.imageprocessingplace.com\/DIPUM-3E\/dipum3e_main_page.htm \r\n% *DIPUM3E*> functions |illuminant|, |lambda2xyz|, |colorMatchingFunctions|,\r\n% |spectrumColors|, |spectrumBar|\r\n%\r\n% I expect the tour to take a couple more blog posts.\r\n%\r\n% First, where does the data in the plot come from? The curve is called\r\n% a relative spectral power distribution curve. This particular curve is\r\n% for a standard _illuminant_ called D65, which is intended to\r\n% approximate average mid-day daylight. The curve is a reference curve\r\n% created by the _International Commission on Illumination_, usually\r\n% written as _CIE_ for the French acronym.\r\n%\r\n% I could have provided this standard data in a MAT-file, I supposed,\r\n% but I chose instead to provide it as a spreadsheet file,\r\n% Illuminants.xlsx. Here's what it looks like:\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/steve\/files\/excel-illuminants.png>>\r\n%\r\n% Along the bottom, you can see that the file contains several sheets:\r\n% D50, D55, D65, D75, A, C, and F7. These sheets contain the data for\r\n% different types of illuminants.\r\n%\r\n% My tool of choice for reading an Excel file like this is |readtable|.\r\n% In the call below, I indicate that I don't want |readtable| to invoke\r\n% Excel (this is now the default behavior), and I also indicate that I\r\n% want to read the D65 sheet.\r\n\r\nT = readtable('Illuminants.xlsx','UseExcel',false,'Sheet','D65');\r\n\r\n%%\r\n% The <http:\/\/www.imageprocessingplace.com\/DIPUM-3E\/dipum3e_main_page.htm \r\n% *DIPUM3E*> function |illuminant| is basically just this call to\r\n% |readtable|.\r\n\r\n%%\r\n% And here's what the resulting MATLAB table looks like:\r\n\r\nhead(T)\r\n\r\n%%\r\n% I can produce a basic plot like this:\r\n\r\nplot(T.lambda,T.S)\r\nxlabel('wavelength (nm)')\r\nylabel('Relative spectral power')\r\ntitle('Illuminant D65')\r\n\r\n%%\r\n% Next time, I'll talk about computing the rainbow colors that appear at\r\n% the bottom of <http:\/\/www.imageprocessingplace.com\/DIPUM-3E\/dipum3e_main_page.htm \r\n% *DIPUM3E*> Figure 7.2(a):\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/steve\/files\/illuminant-d65.png>>\r\n##### SOURCE END ##### dbaf04c26fb54dd0833223213eb6b36c\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/steve\/files\/illuminant-d65.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><p>The new edition of Digital Image Processing Using MATLAB (DIPUM3E) contains a number of MATLAB functions related to color, color calculations, and color visualization. I wrote about functions for... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2020\/04\/27\/making-color-spectrum-plots-part-1\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":4091,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[1183,68,1310,52,94,96],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/4095"}],"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=4095"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/4095\/revisions"}],"predecessor-version":[{"id":4103,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/4095\/revisions\/4103"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media\/4091"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=4095"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=4095"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=4095"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}