Displaying Graphics Images
Image Types and Display Methods
To display a graphics file image, use either image or imagesc. For example, assuming RGB is an image,
figure('Position',[100 100 size(RGB,2) size(RGB,1)]);
image(RGB); set(gca,'Position',[0 0 1 1])
[warning]This image was created with the support of the Space Telescope Science Institute, operated by the Association of Universities for Research in Astronomy, Inc., from NASA contract NAs5-26555, and is reproduced with permission from AURA/STScI. Digital renditions of images produced by AURA/STScI are obtainable royalty-free. Credits: J.P. Harrington and K.J. Orkowski (University of Maryland), and NASA.)[/warning]
This table summarizes display methods for the three types of images.
Image Type | Display Commands | Uses Colormap Colors |
Indexed | image(X); colormap(map) | Yes |
Intensity | imagesc(I,[0 1]); colormap(gray) | Yes |
RGB (truecolor) | image(RGB) | No |
Controlling Aspect Ratio and Display Size
The image function displays the image in a default-sized figure and axes. The image stretches or shrinks to fit the display area. Sometimes you want the aspect ratio of the display to match the aspect ratio of the image data matrix. The easiest way to do this is with the axis image command.
For example, these commands display the earth image in the demos folder using the default figure and axes positions:
load earth
image(X); colormap(map)
The elongated globe results from stretching the image display to fit the axes position. Use the axis image command to force the aspect ratio to be one-to-one.
axis image
The axis image command works by setting the DataAspectRatio property of the axes object to [1 1 1]. See axis and axes for more information on how to control the appearance of axes objects.
Sometimes you want to display an image so that each element in the data matrix corresponds to a single screen pixel. To display an image with this one-to-one matrix-element-to-screen-pixel mapping, you need to resize the figure and axes. For example, these commands display the earth image so that one data element corresponds to one screen pixel:
[m,n] = size(X);
figure('Units','pixels','Position',[100 100 n m])
image(X); colormap(map)
set(gca,'Position',[0 0 1 1])
The figure’s Position property is a four-element vector that specifies the figure’s location on the screen as well as its size. The figure command positions the figure so that its lower left corner is at position (100,100) on the screen and so that its width and height match the image width and height. Setting the axes position to [0 0 1 1] in normalized units creates an axes that fills the figure. The resulting picture is shown.