Skip to Main Content Skip to Search
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Steve on Image Processing

July 20th, 2007

imoverlay and imagesc

In March 2006 I wrote about how to overlay a specified color onto specified pixels of a grayscale or RGB image. I put a utility function (imoverlay) for performing this operation on the MATLAB Central File Exchange.

Earlier this week, Matt W. posted a review of that submission, suggesting that additional documentation might be helpful for people displaying image data that's not in the range [0, 1]. MATLAB users often call imagesc to "autoscale" image data, and Image Processing Toolbox users can call imshow(I,[]) to get a similar effect.

Here's an example using imagesc.

L = membrane(1, 100);
imagesc(L), axis equal, colormap(hot)
axis equal

Note that L has negative values.

min(L(:))
ans =

   -0.3338

max(L(:))
ans =

     1

imagesc automatically scales the values of L "into" the colormap, so that the minimum value is displaying using the first colormap color and the maximum value is displayed using the last colormap color. (See my post about scaled indexed images.)

Although Matt's specific suggestion was about getting imoverlay to work with this data, there's a more general concept here about how to save images like the one above to image file formats, such as JPEG or PNG. Image file formats have generally much less flexibility than MATLAB about displaying pixel values as colors. In particular, the most common formats don't have the notion of scaling an arbitrary range of data values into a colormap. In addition, the JPEG format doesn't even have the notion of using a colormap at all.

We could try writing the L values directly into a JPEG file, like this:

imwrite(L,'membrane1.jpg')
imshow membrane1.jpg

But we lost all the color information, and there's no automatic data scaling. All the values of L less than 0 were simply clipped to black in the JPEG file.

So how can we turn L and the hot colormap into something that can be stored in a JPEG file?

We can do it in three steps:

  • Scale L into the dynamic range [0, 1] by using mat2gray.
  • Convert the result into a normal (unscaled) indexed image using gray2ind.
  • Convert the indexed image into RGB form by using ind2rgb.
gray = mat2gray(L);
X = gray2ind(gray, 256);
rgb = ind2rgb(X, hot(256));

imwrite(rgb, 'membrane2.jpg')
imshow membrane2.jpg

Now that the membrane image is in RGB form, we can use imoverlay. For example, let's overlay all the negative pixels with a light blue color.

mask = L < 0;
light_blue = [.6 .6 1];
overlay = imoverlay(rgb, mask, light_blue);

imwrite(overlay, 'membrane3.jpg')
imshow membrane3.jpg

Of course, every time you launch MATLAB you see this membrane shape in a more familiar form that looks something like this:

membrane(1,50)
colormap(hot)
camlight
shading interp

The L-shaped membrane has played a fascinating role in the history of MATLAB and The MathWorks. If you are interested in the origins and history of MATLAB, you should check out the Origins of MATLAB video by MATLAB creator and company co-founder Cleve Moler.


Get the MATLAB code

Published with MATLAB® 7.4

2 Responses to “imoverlay and imagesc”

  1. sohan replied on :

    hi u might be able to help me out here,,, as we do axis ij to make y axis start from top,,, is there any way we can do same for Z axis for 3D plot ????

    cheers

  2. Steve replied on :

    Sohan—

    set(gca,'ZDir','reverse')
    

Leave a Reply


Steve Eddins manages the Image & Geospatial development team at The MathWorks and coauthored Digital Image Processing Using MATLAB. He writes here about image processing concepts, algorithm implementations, and MATLAB.

  • ismail: i love chess keep posting :) can we make a web cam identify a chess set ? so we have a roboarm plays for real...
  • Navan: While black is going to win with a smothered mate, it is hard to see what moves would have led to this...
  • Doug: Forced ’smothermate&# 8217; is about to happen, with the added insult of threatening the queen on the...
  • Viton: Let’s give it a try: - RxQ : White Rook takes black Queen (White King was checked, can’t take...
  • Omar: Hi Steve, when using tformfwd to find corresponding points in the new space, the resulting co-ordinates from...
  • Steve: Cris—You’ ;re right, I should have caught the plot scaling issue. I wasn’t actually trying...
  • Cris Luengo: Not to spoil your upcoming bog entry too much, but if you scale the first graph (times vs Q) by setting...
  • Steve: Jim—Thanks for adding your comment showing how the syntax works for matrices.
  • Jim: for i = A …statements 230; end; Description: The …statements 230; are executed (as MATLAB...
  • Steve: Omar—Nice work.

These postings are the author's and don't necessarily represent the opinions of The MathWorks.

Related Topics