In this posting I want to summarize all of the factors that control image pixel colors in MATLAB. This information is in the MATLAB documentation, but it is scattered around in several locations. If this is way too much detail for you, that's OK - just come on back next week!
Contents
image - CData dimensionality
- If the image CData is three-dimensional, with the size of the third dimension equal to 3, then the CData values are taken to represent RGB values directly. No other image, axes, or figure properties affect the displayed colors. We sometimes call these "truecolor images."
- If the image CData is two-dimensional, then the image display colors come from the figure colormap. CData values are used (either directly or in scaled form) as lookup indices into the colormap.
image - CData class
- For truecolor images, the class of the CData array (uint8, uint16, or double) affects which values are displayed as white. The triple [0 0 0] is always black. For double CData, [1 1 1] is white. For uint8 CData, [255 255 255] is white. For uint16 CData, [65535 65535 65535] is white.
- For direct indexed images, the class of the CData array affects the indexing operation. For double CData arrays, the value 1 corresponds to the first colormap color. For uint8 or uint16 CData arrays, the value 0 corresponds to the first colormap color.
image - CDataMapping
If the image CData is two-dimensional, then the image display colors come from the figure's colormap, and the image CDataMapping property controls how the colormap lookup is done.
- If CDataMapping is 'direct', then CData values are used directly as lookup indices into the colormap.
- If CDataMapping is 'scaled', then CData values are scaled to form lookup indices into the colormap.
For truecolor images, the CDataMapping property has no effect on the displayed pixel colors.
axes - CLim
If the image CData is two-dimensional, and if the image CDataMapping is scaled, then the two-element axes CLim property determines the scaling function. The first element gives the value that maps to the first colormap color, and the second element gives the value that maps to the last colormap color.
For truecolor images, or if the image CDataMapping property is 'direct', the CLim property has no effect on the displayed pixel colors.
figure - Colormap
If the image CData is two-dimensional, then all displayed pixel colors come from the figure Colormap.
For truecolor images, the Colormap property has no effect on the displayed pixel colors.
Get
the MATLAB code
Published with MATLAB® 7.1


Steve, thanks for summarizing. Two questions:
1. When I use (u)int32 data it seems CData gets converted to double. Expected behavior?
2. Is there a way, short of rescaling CData before assigning to the image object, to have the values point to a section of the figure colormap, instead of spanning the entire colormap?
Thanks,
Dave—Good questions. 1. Unfortunately the answer is yes, that’s expected behavior. However, I anticipate that we’ll soon be able to do a better job with (u)int32 image CData. 2. No.
Steve, ignore second question, thanks.
Thanks for the summary but I’m still having trouble with a colormap scaling problem. I have a matrix where I have distinct integer values and I would like to assign a specific color to each value and then display the matrix using image. For example, suppose that my matrix has values (and assigned colors) of 10 – red, 100 – blue, 1000 –black, and 1e8 white while values from 1e5-1e7 are scaled from orange to white. If colormap were n x 4 as opposed to n x 3 then it would be pretty easy to do (one column represents the appropriate scaling). However, this isn’t the case and I can’t figure out how to do what I want using CData or CDataMapping. Any suggestions?
Thanks,
Jonathan Bar-on
Jonathan—Your desired color scaling goes beyond the scaling supported directly by the Handle Graphics image. You’ll have to write your own scaling function to convert your matrix to an RGB image.
Thank you for this summary
I used “slice” command to see a special part from a special direction of a 3D image and I used CData to analyze my image in a 2D gray level matrix.
I manipulated the 2D image(make some pixels blue) and then used “set(h,’CData’,MyImage)” to map it back to the 3D graph.
1. How can I find the position of those blue pixels in 3D space?
2. Does the new changes affect the original 3D Image?
Thank you
Masoud—1. You picked the slice and changed the pixels, so you must have enough information to determine where they are. 2. No. I don’t know what exactly you mean by “3D graph,” but setting the CData property on an image object, which in Handle Graphics is a 2D thing, won’t affect your 3D image.