File Exchange Pick of the Week

Our best user submissions

Converting Images from Grayscale to Color

Contents

Brett‘s Pick this week is gray2rgb, by Jeny Rajan.

Win One of Several MATLAB Hats! Read On!!

This week’s blog is image processing-centric, but it includes a challenge for MATLAB geeks everywhere. Jeny, of course, is
due some swag–it’s on the way, Jeny! But I’ll also send MATLAB hats to four readers who can rise to the challenges I posed
below. (Who says the MATLAB Central Contest Team gets to have all the fun? :) ) If you’re interested, read on…

Grayscale to RGB

Typically, when I’m analyzing color images, I find that I can do my analyses in “gray space” rather than in color space. That
is, I can usually get all of the information I need from one or more individual color planes, or from a grayscale representation
of the color image. Converting to grayscale generally simplifies–and often speeds up–the processing of these color images.

Creating a grayscale image as a linear combination of multiple colorplanes is a common task, and one that the Image Processing Toolbox facilitates with the function rgb2gray.

Recently, while browsing the File Exchange, I came across Jeny’s file, which allows one to convert images “the other way.”
That is, you can provide a grayscale image along with a color image whose pallete you want to copy, and gray2rgb will create a color version of your grayscale image, matching (to some extent) the color of the second input image. I was
intrigued, so I played with the file. Here is a sample that shows what this function can do:

figure
subplot(2,2,1)
imshow('liftingbody.png');
subplot(2,2,2)
imshow('gantrycrane.png');
colorIm = gray2rgb('liftingbody.png','gantrycrane.png');
subplot(2,2,3.5)
imshow(colorIm)

It’s clear that many people have found gray2rgb to be useful; the file has been on the File Exchange for many years, and is getting close to 100 downloads per month. Plus,
there are several “thank you’s” and good reviews. Nonetheless, I’d like to note that this calculation took a very long time –on the order of two-and-a-half minutes ! Jeny discussed a couple of approaches to making this faster. One could subsample the color image (at the potential cost
of quality), or one could use “jittered sampling.” Unfortunately, no examples are provided that show how one would implement
these time-saving approaches.

A Multi-Part Challenge

Digging into the underlying code, we see that Jeny’s function converts both input images to yCbCr. (The grayscale image is first expanded to 3D, with replicated information in the red, green, and blue colorplanes.) Then,
the output image is generated by processing the converted images pixel by pixel (!) , using a nested for loop. That works, but it can be slow indeed. I can think of several ways to improve the performance
of this code, and I’m betting that some of our readers can as well.

I’ll send a MATLAB cap to the first person who can exactly duplicate the results of colorIm generated above by vectorizing Jeny’s implementation of gray2rgb!

It also occurs to me that there are other approaches to the same problem. For example, using a function that I have previously shared on the File Exchange, I generated this color image:

Creating this image, using my function on my computer, took 0.02 seconds.

Swag to the first person who can find, from among my File Exchange submissions, a function that generates the image above
using a one-line command. (Note that there’s also an Image Processing Toolbox function that facilitates the same calculation,
though you’ll have to use it “cleverly.”)

Next, I’ll send swag to anyone who can come up with an alternate solution that differs substantively from Jeny’s approach,
and from the one I used. The only other rule besides “differs substantively” is that it has to run quickly –say 1 second or faster, using the same input images.

Now the question arises: why would one want to do this? Does anyone have a practical use case for converting grayscale images
to rgb by matching the colors of a second image? If anyone can provide an intriguing use case for modifying images in this
way, I’d love to hear it. Swag for the first compelling rationale. (For our purposes here, reasons like “it looks cool” or “it creates an interesting special effect” don’t count!)

Happy Thanksgiving, and Happy MATLABbing!

As always, comments to this blog post are welcome. Or leave a comment for Jeny here.

Published with MATLAB® R2012b

 

|
  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.