{"id":3386,"date":"2012-11-25T17:28:25","date_gmt":"2012-11-25T22:28:25","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=3386"},"modified":"2016-11-16T15:13:52","modified_gmt":"2016-11-16T20:13:52","slug":"converting-images-from-grayscale-to-color","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2012\/11\/25\/converting-images-from-grayscale-to-color\/","title":{"rendered":"Converting Images from Grayscale to Color"},"content":{"rendered":"<div class=\"content\">\n<h3>Contents<\/h3>\n<div>\n<ul>\n<li><a href=\"#2\">Win One of Several MATLAB Hats! Read On!!<\/a><\/li>\n<li><a href=\"#3\">Grayscale to RGB<\/a><\/li>\n<li><a href=\"#9\">A Multi-Part Challenge<\/a><\/li>\n<li><a href=\"#17\">Happy Thanksgiving, and Happy MATLABbing!<\/a><\/li>\n<\/ul>\n<\/div>\n<p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911\">Brett<\/a>&#8216;s Pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/8214-gray-image-to-color-image-conversion\">gray2rgb,<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/18467\">Jeny Rajan<\/a>.<\/p>\n<h3>Win One of Several MATLAB Hats! Read On!!<a name=\"2\"><\/a><\/h3>\n<p>This week&#8217;s blog is image processing-centric, but it includes a challenge for MATLAB geeks everywhere. Jeny, of course, is<br \/>\ndue some swag&#8211;it&#8217;s on the way, Jeny! But I&#8217;ll also send MATLAB hats to four readers who can rise to the challenges I posed<br \/>\nbelow. (Who says the <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/contest\/\">MATLAB Central Contest Team<\/a> gets to have all the fun? :) ) If you&#8217;re interested, read on&#8230;<\/p>\n<h3>Grayscale to RGB<a name=\"3\"><\/a><\/h3>\n<p>Typically, when I&#8217;m analyzing color images, I find that I can do my analyses in &#8220;gray space&#8221; rather than in color space. That<br \/>\nis, I can usually get all of the information I need from one or more individual color planes, or from a grayscale representation<br \/>\nof the color image. Converting to grayscale generally simplifies&#8211;and often speeds up&#8211;the processing of these color images.<\/p>\n<p>Creating a grayscale image as a linear combination of multiple colorplanes is a common task, and one that the <a href=\"https:\/\/www.mathworks.com\/products\/image\/\">Image Processing Toolbox<\/a> facilitates with the function <tt>rgb2gray<\/tt>.<\/p>\n<p>Recently, while browsing the File Exchange, I came across Jeny&#8217;s file, which allows one to convert images &#8220;the other way.&#8221;<br \/>\nThat is, you can provide a grayscale image along with a color image whose pallete you want to copy, and <tt>gray2rgb<\/tt> will create a color version of your grayscale image, matching (to some extent) the color of the second input image. I was<br \/>\nintrigued, so I played with the file. Here is a sample that shows what this function can do:<\/p>\n<pre>figure\r\nsubplot(2,2,1)\r\nimshow('liftingbody.png');\r\nsubplot(2,2,2)\r\nimshow('gantrycrane.png');\r\ncolorIm = gray2rgb('liftingbody.png','gantrycrane.png');\r\nsubplot(2,2,3.5)\r\nimshow(colorIm)<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/sampleGray2RGB1-e1353093420206.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/><\/p>\n<p>It&#8217;s clear that many people have found <tt>gray2rgb<\/tt> to be useful; the file has been on the File Exchange for many years, and is getting close to 100 downloads per month. Plus,<br \/>\nthere are several &#8220;thank you&#8217;s&#8221; and good reviews. Nonetheless, I&#8217;d like to note that this calculation took <em>a very long time<\/em> &#8211;on the order of <strong>two-and-a-half minutes<\/strong> ! Jeny discussed a couple of approaches to making this faster. One could subsample the color image (at the potential cost<br \/>\nof quality), or one could use &#8220;jittered sampling.&#8221; Unfortunately, no examples are provided that show how one would implement<br \/>\nthese time-saving approaches.<\/p>\n<h3>A Multi-Part Challenge<a name=\"9\"><\/a><\/h3>\n<p>Digging into the underlying code, we see that Jeny&#8217;s function <a href=\"https:\/\/www.mathworks.com\/help\/images\/ref\/rgb2ycbcr.html\">converts both input images to yCbCr.<\/a> (The grayscale image is first expanded to 3D, with replicated information in the red, green, and blue colorplanes.) Then,<br \/>\nthe output image is generated by processing the converted images <em>pixel by pixel (!)<\/em> , using a nested for loop. That works, but it can be slow indeed. I can think of several ways to improve the performance<br \/>\nof this code, and I&#8217;m betting that some of our readers can as well.<\/p>\n<p>I&#8217;ll send a MATLAB cap to the first person who can <em>exactly duplicate<\/em> the results of <tt>colorIm<\/tt> generated above by <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/vectorization.html\">vectorizing<\/a> Jeny&#8217;s implementation of gray2rgb!<\/p>\n<p>It also occurs to me that there are other approaches to the same problem. For example, using a function that I have <em>previously shared<\/em> on the File Exchange, I generated this color image:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/colorliftingbody2.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/><\/p>\n<p>Creating this image, using my function on my computer, took 0.02 seconds.<\/p>\n<p>Swag to the first person who can find, from among my File Exchange submissions, a function that generates the image above<br \/>\nusing a one-line command. (Note that there&#8217;s also an Image Processing Toolbox function that facilitates the same calculation,<br \/>\nthough you&#8217;ll have to use it &#8220;cleverly.&#8221;)<\/p>\n<p>Next, I&#8217;ll send swag to anyone who can come up with an alternate solution that differs substantively from Jeny&#8217;s approach,<br \/>\nand from the one I used. The only other rule besides &#8220;differs substantively&#8221; is that it has to run <em>quickly<\/em> &#8211;say 1 second or faster, using the same input images.<\/p>\n<p>Now the question arises: why would one want to do this? Does anyone have a practical use case for converting grayscale images<br \/>\nto rgb by matching the colors of a second image? If anyone can provide an intriguing use case for modifying images in this<br \/>\nway, I&#8217;d love to hear it. Swag for the first <em>compelling<\/em> rationale. (For our purposes here, reasons like &#8220;it looks cool&#8221; or <a href=\"https:\/\/blogs.mathworks.com\/steve\/2012\/11\/13\/image-effects-part-1\/\">&#8220;it creates an interesting special effect&#8221;<\/a> don&#8217;t count!)<\/p>\n<h3>Happy Thanksgiving, and Happy MATLABbing!<a name=\"17\"><\/a><\/h3>\n<p>As always, <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=3386#respond\">comments to this blog post<\/a> are welcome. Or leave a comment for Jeny <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/8214-gray-image-to-color-image-conversion#comments\">here<\/a>.<\/p>\n<p><script type=\"text\/javascript\" language=\"JavaScript\">\/\/ <![CDATA[\n    function grabCode_0a217eebdd9b4923b5583d4a97d131f0() {\n        \/\/ Remember the title so we can use it in the new page\n        title = document.title;\n\n        \/\/ Break up these strings so that their presence\n        \/\/ in the Javascript doesn't mess up the search for\n        \/\/ the MATLAB code.\n        t1='0a217eebdd9b4923b5583d4a97d131f0 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 0a217eebdd9b4923b5583d4a97d131f0';\n\n        b=document.getElementsByTagName('body')[0];\n        i1=b.innerHTML.indexOf(t1)+t1.length;\n        i2=b.innerHTML.indexOf(t2);\n\n        code_string = b.innerHTML.substring(i1, i2);\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\n\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \n        \/\/ in the XML parser.\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\n        \/\/ doesn't go ahead and substitute the less-than character. \n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\n\n        author = 'Brett Shoelson';\n        copyright = 'Copyright 2012 The MathWorks, Inc.';\n\n        w = window.open();\n        d = w.document;\n        d.write('\n\n\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>\n\n\n\n\\n');\n\n      d.title = title + ' (MATLAB code)';\n      d.close();\n      }\n\/\/ ]]><\/script><\/p>\n<p style=\"text-align: right; font-size: xx-small; font-weight: lighter; font-style: italic; color: gray;\">\n<a href=\"javascript:grabCode_0a217eebdd9b4923b5583d4a97d131f0()\"><span style=\"font-size: x-small; font-style: italic;\">Get<br \/>\nthe MATLAB code <\/span><\/a><\/p>\n<p><noscript>(requires JavaScript)<\/noscript><\/p>\n<p>Published with MATLAB\u00ae R2012b<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\nContents<\/p>\n<p>Win One of Several MATLAB Hats! Read On!!<br \/>\nGrayscale to RGB<br \/>\nA Multi-Part Challenge<br \/>\nHappy Thanksgiving, and Happy MATLABbing!<\/p>\n<p>Brett&#8216;s Pick this week is gray2rgb, by Jeny Rajan.<br \/>\nWin&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2012\/11\/25\/converting-images-from-grayscale-to-color\/\">read more >><\/a><\/p>\n","protected":false},"author":34,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/3386"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=3386"}],"version-history":[{"count":23,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/3386\/revisions"}],"predecessor-version":[{"id":8048,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/3386\/revisions\/8048"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=3386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=3386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=3386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}