Displaying and Exploring Images in Matlab
Displaying and Exploring Images in Matlab

Overview

The Image Processing Toolbox software includes two display functions, imshow and imtool. Both functions work within the Handle Graphics architecture: they create an image object and display it in an axes object contained in a figure object.

imshow is the toolbox's fundamental image display function. Use imshow when you want to display any of the different image types supported by the toolbox, such as grayscale (intensity), truecolor (RGB), binary, and indexed. For more information, see Displaying Images Using the imshow Function. The imshow function is also a key building block for image applications you can create using the toolbox modular tools. For more information, see Building GUIs with Modular Tools.

The other toolbox display function, imtool, launches the Image Tool, which presents an integrated environment for displaying images and performing some common image-processing tasks. The Image Tool provides all the image display capabilities of imshow but also provides access to several other tools for navigating and exploring images, such as scroll bars, the Pixel Region tool, the Image Information tool, and the Adjust Contrast tool. For more information, see Using the Image Tool to Explore Images.

In general, using the toolbox functions to display images is preferable to using MATLAB image display functions image and imagesc because the toolbox functions set certainHandle Graphics properties automatically to optimize the image display. The following table lists these properties and their settings for each image type. In the table, X represents an indexed image, I represents a grayscale image, BW represents a binary image, and RGB represents a truecolor image.

    Note   Both imshow and imtool can perform automatic scaling of image data. When called with the syntax imshow(I,'DisplayRange',[]), and similarly for imtool, the functions set the axes CLim property to [min(I(:)) max(I(:))]. CDataMapping is always scaled for grayscale images, so that the value min(I(:)) is displayed using the first colormap color, and the value max(I(:)) is displayed using the last colormap color.

Handle Graphics Property

Indexed Images

Grayscale Images

Binary Images

Truecolor Images

CData (Image)

Set to the data in X

Set to the data in I

Set to data in BW

Set to data in RGB

CDataMapping (Image)

Set to 'direct'

Set to 'scaled'

Set to 'direct'

Ignored when CData is 3-D

CLim (Axes)

Does not apply

double: [0 1]
uint8:  [0 255]
uint16: [0 65535]

Set to [0 1]

Ignored when CData is 3-D

Colormap (Figure)

Set to data in map

Set to grayscale colormap

Set to a grayscale colormap whose values range from black to white

Ignored when CData is 3-D

Displaying Images Using the imshow Function

Overview

To display image data, use the imshow function. The following example reads an image into the MATLAB workspace and then displays the image in a MATLAB figure window.

moon = imread('moon.tif');  imshow(moon);

The imshow function displays the image in a MATLAB figure window, as shown in the following figure.

Image Displayed in a Figure Window by imshow

You can also pass imshow the name of a file containing an image.

imshow('moon.tif');

This syntax can be useful for scanning through images. Note, however, that when you use this syntax, imread does not store the image data in the MATLAB workspace. If you want to bring the image into the workspace, you must use the getimage function, which retrieves the image data from the current Handle Graphics image object. This example assigns the image data from moon.tif to the variable moon, if the figure window in which it is displayed is currently active.

moon = getimage;

For more information about using imshow to display the various image types supported by the toolbox, see Displaying Different Image Types.

Specifying the Initial Image Magnification

By default, imshow attempts to display an image in its entirety at 100% magnification (one screen pixel for each image pixel). However, if an image is too large to fit in a figure window on the screen at 100% magnification, imshow scales the image to fit onto the screen and issues a warning message.

To override the default initial magnification behavior for a particular call to imshow, specify the InitialMagnification parameter. For example, to view an image at 150% magnification, use this code.

pout = imread('pout.tif');  imshow(pout, 'InitialMagnification', 150)

imshow attempts to honor the magnification you specify. However, if the image does not fit on the screen at the specified magnification, imshow scales the image to fit and issues a warning message. You can also specify the text string 'fit' as the initial magnification value. In this case, imshow scales the image to fit the current size of the figure window.

To change the default initial magnification behavior of imshow, set the ImshowInitialMagnification toolbox preference. To set the preference, open the Image Processing Toolbox Preferences dialog by calling iptprefs or by selecting Preferences from the MATLAB Desktop File menu.

When imshow scales an image, it uses interpolation to determine the values for screen pixels that do not directly correspond to elements in the image matrix. For more information, see Specifying the Interpolation Method.

Controlling the Appearance of the Figure

By default, when imshow displays an image in a figure, it surrounds the image with a gray border. You can change this default and suppress the border using the 'border' parameter, as shown in the following example.

imshow('moon.tif','Border','tight')

The following figure shows the same image displayed with and without a border.

Image Displayed With and Without a Border

The 'border' parameters affect only the image being displayed in the call to imshow. If you want all the images that you display using imshow to appear without the gray border, set the Image Processing Toolbox 'ImshowBorder' preference to 'tight'. You can also use preferences to include visible axes in the figure. For more information about preferences, see iptprefs.

Displaying Each Image in a Separate Figure

The simplest way to display multiple images is to display them in separate figure windows. MATLAB does not place any restrictions on the number of images you can display simultaneously.

imshow always displays an image in the current figure. If you display two images in succession, the second image replaces the first image. To view multiple figures with imshow, use the figure command to explicitly create a new empty figure before calling imshow for the next image. For example, to view the first three frames in an array of grayscale images I,

imshow(I(:,:,:,1)) figure, imshow(I(:,:,:,2)) figure, imshow(I(:,:,:,3))

Displaying Multiple Images in the Same Figure

You can use the imshow function with the MATLAB subplot function or the MATLAB subimage function to display multiple images in a single figure window. For additional options, see Viewing Image Sequences.

    Note   imtool does not support this capability.

Dividing a Figure Window into Multiple Display Regions

subplot divides a figure into multiple display regions. The syntax of subplot is

subplot(m,n,p)

This syntax divides the figure into an m-by-n matrix of display regions and makes the pth display region active.

    Note   When you use subplot to display multiple color images in one figure window, the images must share the colormap of the last image displayed. In some cases, as illustrated by the following example, the display results can be unacceptable. As an alternative, you can use the subimage function, described in Using the subimage Function to Display Multiple Images, or you can map all images to the same colormap as you load them.

For example, you can use this syntax to display two images side by side.

[X1,map1]=imread('forest.tif'); [X2,map2]=imread('trees.tif'); subplot(1,2,1), imshow(X1,map1) subplot(1,2,2), imshow(X2,map2)

In the figure, note how the first image displayed, X1, appears dark after the second image is displayed.

Two Images in Same Figure Using the Same Colormap

Using the subimage Function to Display Multiple Images

subimage converts images to truecolor before displaying them and therefore circumvents the colormap sharing problem. This example uses subimage to display the forest and the trees images with better results.

[X1,map1]=imread('forest.tif'); [X2,map2]=imread('trees.tif'); subplot(1,2,1), subimage(X1,map1) subplot(1,2,2), subimage(X2,map2)

Two Images in Same Figure Using Separate Colormaps


Using the Image Tool to Explore Images

Image Tool Overview

The Image Tool is an image display and exploration tool that presents an integrated environment for displaying images and performing common image-processing tasks. The Image Tool provides access to several other tools:

  • Pixel Information tool — for getting information about the pixel under the pointer

  • Pixel Region tool — for getting information about a group of pixels

  • Distance tool — for measuring the distance between two pixels

  • Image Information tool — for getting information about image and image file metadata

  • Adjust Contrast tool and associated Window/Level tool — for adjusting the contrast of the image displayed in the Image Tool and modifying the actual image data. You can save the adjusted data to the workspace or a file.

  • Crop Image tool — for defining a crop region on the image and cropping the image. You can save the cropped image to the workspace or a file.

  • Display Range tool — for determining the display range of the image data

In addition, the Image Tool provides several navigation aids that can help explore large images:

  • Overview tool — for determining what part of the image is currently visible in the Image Tool and changing this view.

  • Pan tool — for moving the image to view other parts of the image

  • Zoom tool — for getting a closer view of any part of the image.

  • Scroll bars — for navigating over the image.

The following figure shows the image displayed in the Image Tool with many of the related tools open and active.

Image Tool and Related Tools

 

Opening the Image Tool

To start the Image Tool, use the imtool function. You can also start another Image Tool from within an existing Image Tool by using the New option from the File menu.

The imtool function supports many syntax options. For example, when called without any arguments, it opens an empty Image Tool.

imtool

To bring image data into this empty Image Tool, you can use either the Open or Import from Workspace options from the File menu — see Importing Image Data from the Workspace.

You can also specify the name of the MATLAB workspace variable that contains image data when you call imtool, as follows:

moon = imread('moon.tif');  imtool(moon)

Alternatively, you can specify the name of the graphics file containing the image. This syntax can be useful for scanning through graphics files.

imtool('moon.tif');

    Note   When you use this syntax, the image data is not stored in a MATLAB workspace variable. To bring the image displayed in the Image Tool into the workspace, you must use the getimage function or the Export from Workspace option from the Image Tool File menu — see Exporting Image Data to the Workspace.

Specifying the Initial Image Magnification

The imtool function attempts to display an image in its entirety at 100% magnification (one screen pixel for each image pixel) and always honors any magnification value you specify. If the image is too big to fit in a figure on the screen, the Image Tool shows only a portion of the image, adding scroll bars to allow navigation to parts of the image that are not currently visible. If the specified magnification would make the image too large to fit on the screen, imtool scales the image to fit, without issuing a warning. This is the default behavior, specified by the imtool 'InitialMagnification' parameter value 'adaptive'.

To override this default initial magnification behavior for a particular call to imtool, specify the InitialMagnification parameter. For example, to view an image at 150% magnification, use this code.

pout = imread('pout.tif'); imtool(pout, 'InitialMagnification', 150)

You can also specify the text string 'fit' as the initial magnification value. In this case, imtool scales the image to fit the default size of a figure window.

Another way to change the default initial magnification behavior of imtool is to set the ImtoolInitialMagnification toolbox preference. The magnification value you specify will remain in effect until you change it. To set the preference, use iptsetpref or open the Image Processing Preferences panel by calling iptprefs or by selecting File > Preferences in the Image Tool menu. To learn more about toolbox preferences, see iptprefs.

When imtool scales an image, it uses interpolation to determine the values for screen pixels that do not directly correspond to elements in the image matrix. For more information, see Specifying the Interpolation Method.

Specifying the Colormap

A colormap is a matrix that can have any number of rows, but must have three columns. Each row in the colormap is interpreted as a color, with the first element specifying the intensity of red, the second green, and the third blue.

To specify the color map used to display an indexed image or a grayscale image in the Image Tool, select the Choose Colormap option on the Tools menu. This activates the Choose Colormap tool. Using this tool you can select one of the MATLAB colormaps or select a colormap variable from the MATLAB workspace.

When you select a colormap, the Image Tool executes the colormap function you specify and updates the image displayed. You can edit the colormap command in the Evaluate Colormap text box; for example, you can change the number of entries in the colormap (default is 256). You can enter your own colormap function in this field. Press Enter to execute the command.

When you choose a colormap, the image updates to use the new map. If you click OK, the Image Tool applies the colormap and closes the Choose Colormap tool. If you click Cancel, the image reverts to the previous colormap.

Image Tool Choose Colormap Tool

Importing Image Data from the Workspace

To import image data from the MATLAB workspace into the Image Tool, use the Import from Workspace option on the Image Tool File menu. In the dialog box, shown below, you select the workspace variable that you want to import into the workspace.

The following figure shows the Import from Workspace dialog box. You can use the Filter menu to limit the images included in the list to certain image types, i.e., binary, indexed, intensity (grayscale), or truecolor.

Image Tool Import from Workspace Dialog Box

Exporting Image Data to the Workspace

To export the image displayed in the Image Tool to the MATLAB workspace, you can use the Export to Workspace option on the Image Tool File menu. (Note that when exporting data, changes to the display range will not be preserved.) In the dialog box, shown below, you specify the name you want to assign to the variable in the workspace. By default, the Image Tool prefills the variable name field with BW, for binary images, RGB, for truecolor images, and I for grayscale or indexed images.

If the Image Tool contains an indexed image, this dialog box also contains a field where you can specify the name of the associated colormap.

Image Tool Export Image to Workspace Dialog Box

Using the getimage Function to Export Image Data

You can also use the getimage function to bring image data from the Image Tool into the MATLAB workspace.

The getimage function retrieves the image data (CData) from the current Handle Graphics image object. Because, by default, the Image Tool does not make handles to objects visible, you must use the toolbox function imgca to get a handle to the image axes displayed in the Image Tool. The following example assigns the image data from moon.tif to the variable moon if the figure window in which it is displayed is currently active.

moon = getimage(imgca);

Saving the Image Data Displayed in the Image Tool

To save the image data displayed in the Image Tool, select the Save as option from the Image Tool File menu. The Image Tool opens the Save Image dialog box, shown in the following figure. Use this dialog box to navigate your file system to determine where to save the image file and specify the name of the file. Choose the graphics file format you want to use from among many common image file formats listed in the Files of Type menu. If you do not specify a file name extension, the Image Tool adds an extension to the file associated with the file format selected, such as .jpg for the JPEG format.

Image Tool Save Image Dialog Box

    Note   Changes you make to the display range will not be saved. If you would like to preserve your changes, use imcontrast.


Closing the Image Tool

To close the Image Tool window, use the Close button in the window title bar or select the Close option from the Image Tool File menu. You can also use the imtool function to return a handle to the Image Tool and use the handle to close the Image Tool. When you close the Image Tool, any related tools that are currently open also close.

Because the Image Tool does not make the handles to its figure objects visible, the Image Tool does not close when you call the MATLAB close all command. If you want to close multiple Image Tools, use the syntax

imtool close all

or select Close all from the Image Tool File menu.

 

Printing the Image in the Image Tool

To print the image displayed in the Image Tool, select the Print to Figure option from the File menu. The Image Tool opens another figure window and displays the image. Use the Print option on the File menu of this figure window to print the image. See Printing Images for more information.

Exploring Very Large Images

Overview

If you are viewing a very large image, it might not load in imtool, or it could load, but zooming and panning are slow. In either case, creating a reduced resolution data set (R-Set) can improve performance. Use imtool to navigate an R-Set image the same way you navigate a standard image.

Creating an R-Set File

To create an R-Set file, use the function rsetwrite. For example, to create an R-Set from a TIFF file called 'LargeImage.tif', enter the following:

rsetfile = rsetwrite ('LargeImage.tif')

rsetwrite saves an R-Set file named 'LargeImage.rset' in the current directory. Or, if you want to save the R-Set file under a different name, enter the following:

rsetfile = rsetwrite('LargeImage.tif', 'New_Name')

You can create an R-Set file directly from a TIFF or NITF file, or you can create one from another type of image file by first creating an Image Adapter object. See Writing an Image Adapter Class for more information.

The time required to create an R-Set varies, depending on the size of the initial file and the capability of your machine. A progress bar shows an estimate of time required. If you cancel the operation, processing stops, no file is written, and the rsetfile variable will be empty.

Opening an R-Set File

Open an R-Set file from the command line:

imtool('LargeImage.rset')

Using Image Tool Navigation Aids

Navigating an Image Using the Overview Tool

If an image is large or viewed at a large magnification, the Image Tool displays only a portion of the entire image, including scroll bars to allow navigation around the image. To determine which part of the image is currently visible in the Image Tool, use the Overview tool. The Overview tool displays the entire image, scaled to fit. Superimposed over this view of the image is a rectangle, called the detail rectangle. The detail rectangle shows which part of the image is currently visible in the Image Tool. You can change the portion of the image visible in the Image Tool by moving the detail rectangle over the image in the Overview tool.

Image Tool with Overview Tool

Starting the Overview Tool

You can start the Overview tool by clicking the Overview button in the Image Tool toolbar or by selecting the Overview option from the Tools menu in the Image Tool. You can also change the preferences, so the Overview tool will open automatically when you open the Image Tool. For more information on setting preferences, see iptprefs.

Moving the Detail Rectangle to Change the Image View

  1. Start the Overview tool by clicking the Overview button in the Image Tool toolbar or by selecting Overview from the Tools menu. The Overview tool opens in a separate window containing a view of the entire image, scaled to fit.

    If the Overview tool is already active, clicking the Overview button brings the tool to the front of the windows open on your screen.

  2. Using the mouse, move the pointer into the detail rectangle. The pointer changes to a fleur, .

  3. Press and hold the mouse button to drag the detail rectangle anywhere on the image. The Image Tool updates the view of the image to make the specified region visible.

Specifying the Color of the Detail Rectangle

By default, the color of the detail rectangle in the Overview tool is blue. You can change the color of the rectangle to achieve better contrast with the predominant color of the underlying image. To do this, right-click anywhere inside the boundary of the detail rectangle and select a color from the Set Color option on the context menu.

Getting the Position and Size of the Detail Rectangle

To get the current position and size of the detail rectangle, right-click anywhere inside it and select Copy Position from the context menu. You can also access this option from the Edit menu of the Overview tool.

This option copies the position information to the clipboard. The position information is a vector of the form [xmin ymin width height]. You can paste this position vector into the MATLAB workspace or another application.

Printing the View of the Image in the Overview Tool

You can print the view of the image displayed in the Overview tool. Select the Print to Figure option from the Overview tool File menu. See Printing Images for more information.

 

Panning the Image Displayed in the Image Tool

To change the portion of the image displayed in the Image Tool, you can use the Pan tool to move the image displayed in the window. This is called panning the image.

  1. Click the Pan tool button in the toolbar or select Pan from the Tools menu. When the Pan tool is active, a checkmark appears next to the Pan selection in the menu.

  2. Move the pointer over the image in the Image Tool, using the mouse. The pointer changes to an open-hand shape .

  3. Press and hold the mouse button and drag the image in the Image Tool. When you drag the image, the pointer changes to the closed-hand shape .

  4. To turn off panning, click the Pan tool button again or click the Pan option in the Tools menu.

Zooming In and Out on an Image in the Image Tool

To enlarge an image to get a closer look or shrink an image to see the whole image in context, use the Zoom buttons on the toolbar. (You can also zoom in or out on an image by changing the magnification — see Specifying the Magnification of the Image or by using the Ctrl+Plus or Ctrl+Minus keys. Note that these are the Plus(+) and Minus(-) keys on the numeric keypad of your keyboard.)

  1. Click the appropriate magnifying glass button in the Image Tool toolbar or select the Zoom In or Zoom Out option in the Tools menu. When the Zoom tool is active, a checkmark appears next to the appropriate Zoom selection in the menu.

  2. Move the pointer over the image you want to zoom in or out on, using the mouse. The pointer changes to the appropriate magnifying glass icon. With each click, the Image Tool changes the magnification of the image, centering the new view of the image on the spot where you clicked.

    When you zoom in or out on an image, the magnification value displayed in the magnification edit box changes and the Overview window updates the position of the detail rectangle.

  3. To leave zoom mode, click the active zoom button again to deselect it or click the Zoom option in the Tools menu.


Specifying the Magnification of the Image

To enlarge an image to get a closer look or to shrink an image to see the whole image in context, you can use the magnification edit box, shown in the following figure. (You can also use the Zoom buttons to enlarge or shrink an image. See Zooming In and Out on an Image in the Image Tool for more information.)

Image Tool Magnification Edit Box and Menu

To change the magnification of an image,

  1. Move the pointer into the magnification edit box. The pointer changes to the text entry cursor.

  2. Type a new value in the magnification edit box and press Enter. The Image Tool changes the magnification of the image and displays the new view in the window.

You can also specify a magnification by clicking the menu associated with the magnification edit box and selecting from a list of preset magnifications. If you choose the Fit to Window option, the Image Tool scales the image so that the entire image is visible.

Getting Information about the Pixels in an Image

Determining the Value of Individual Pixels

The Image Tool displays information about the location and value of individual pixels in an image in the bottom left corner of the tool. (You can also obtain this information by opening a figure with imshow and then calling impixelinfo from the command line.) The pixel value and location information represent the pixel under the current location of the pointer. The Image Tool updates this information as you move the pointer over the image.

For example, view an image in the Image Tool.

imtool('moon.tif')

The following figure shows the Image Tool with pixel location and value displayed in the Pixel Information tool. For more information, see Saving the Pixel Value and Location Information.

Pixel Information in Image Tool

Saving the Pixel Value and Location Information

To save the pixel location and value information displayed, right-click a pixel in the image and choose the Copy pixel info option. The Image Tool copies the x- and y-coordinates and the pixel value to the clipboard.

To paste this pixel information into the MATLAB workspace or another application, right-click and select Paste from the context menu.

Determining the Values of a Group of Pixels

To view the values of pixels in a specific region of an image displayed in the Image Tool, use the Pixel Region tool. The Pixel Region tool superimposes a rectangle, called the pixel region rectangle, over the image displayed in the Image Tool. This rectangle defines the group of pixels that are displayed, in extreme close-up view, in the Pixel Region tool window. The following figure shows the Image Tool with the Pixel Region tool. Note how the Pixel Region tool includes the value of each pixel in the display.

Selecting a Region

  1. To start the Pixel Region tool, click the Pixel Region button in the Image Tool toolbar or select the Pixel Region option from the Tools menu. (Another option is to open a figure using imshow and then call impixelregion from the command line.) The Image Tool displays the pixel region rectangle in the center of the target image and opens the Pixel Region tool.

      Note   Scrolling the image can move the pixel region rectangle off the part of the image that is currently displayed. To bring the pixel region rectangle back to the center of the part of the image that is currently visible, click the Pixel Region button again. For help finding the Pixel Region tool in large images, see Determining the Location of the Pixel Region Rectangle.

  2. Using the mouse, position the pointer over the pixel region rectangle. The pointer changes to the fleur shape, .
  3. Click the left mouse button and drag the pixel region rectangle to any part of the image. As you move the pixel region rectangle over the image, the Pixel Region tool updates the pixel values displayed. You can also move the pixel region rectangle by moving the scroll bars in the Pixel Region tool window.

Customizing the View

To get a closer view of image pixels, use the zoom buttons on the Pixel Region tool toolbar. As you zoom in, the size of the pixels displayed in the Pixel Region tool increase and fewer pixels are visible. As you zoom out, the size of the pixels in the Pixel Region tool decrease and more pixels are visible. To change the number of pixels displayed in the tool, without changing the magnification, resize the Pixel Region tool using the mouse.

As you zoom in or out, note how the size of the pixel region rectangle changes according to the magnification. You can resize the pixel region rectangle using the mouse. Resizing the pixel region rectangle changes the magnification of pixels displayed in the Pixel Region tool.

If the magnification allows, the Pixel Region tool overlays each pixel with its numeric value. For RGB images, this information includes three numeric values, one for each band of the image. For indexed images, this information includes the index value and the associated RGB value. If you would rather not see the numeric values in the display, go to the Pixel Region tool Edit menu and clear the Superimpose Pixel Values option.

Pixel Region Tool Edit Menu

Determining the Location of the Pixel Region Rectangle

To determine the current location of the pixel region in the target image, you can use the pixel information given at the bottom of the tool. This information includes the x- and y-coordinates of pixels in the target image coordinate system. When you move the pixel region rectangle over the target image, the pixel information given at the bottom of the tool is not updated until you move the cursor back over the Pixel Region tool.

You can also retrieve the current position of the pixel region rectangle by selecting the Copy Position option from the Pixel Region tool Edit menu. This option copies the position information to the clipboard. The position information is a vector of the form [xmin ymin width height].

To paste this position vector into the MATLAB workspace or another application, right-click and select Paste from the context menu.

The following figure shows these components of the Pixel Region tool.

Pixel Region Rectangle Location Information

Printing the View of the Image in the Pixel Region Tool

You can print the view of the image displayed in the Pixel Region tool. Select the Print to Figure option from the Pixel Region tool File menu. See Printing Images for more information.

Determining the Display Range of an Image

The Image Tool provides information about the display range of pixels in a grayscale image. The display range is the value of the axes CLim property, which controls the mapping of image CData to the figure colormap. CLim is a two-element vector [cmin cmax] specifying the CData value to map to the first color in the colormap (cmin) and the CData value to map to the last color in the colormap (cmax). Data values in between are linearly scaled.

The Image Tool displays this information in the Display Range tool at the bottom right corner of the window. The Image Tool does not show the display range for indexed, truecolor, or binary images. (You can also obtain this information by opening a figure window with imshow and then calling imdisplayrange from the command line.)

For example, view an image in the Image Tool.

imtool('moon.tif')

The following figure shows the Image Tool displaying the image with display range information.

Display Range Information in Image Tool

 

Measuring the Distance Between Two Pixels

Using the Distance Tool

  1. Display an image in the Image Tool.

    imtool('moon.tif') 
  2. Click the Distance tool button in the Image Tool toolbar or select Measure Distance from the Tools menu. The Distance tool allows you to measure distance with a click-and-drag approach. When you move the pointer over the image, the pointer changes to cross hairs . Position the cross hairs at the beginning of the region you wish to measure, hold down the mouse button, drag the cross hairs to the end of the region, and release the button.

    The Distance tool displays the Euclidean distance between the two endpoints of the line in a label superimposed over the line. The tool specifies the distance in data units determined by the XData and YData properties, which is pixels, by default.

  3. An alternative to using the Distance tool in Image Tool is to open a figure window using imshow and then call imdistline. The Distance tool appears as a horizontal line displayed over the image. You can reposition the line or grab either of its endpoints to resize the tool.

Exporting Endpoint and Distance Data

To save the endpoint locations and distance information, right-click the Distance tool and choose the Export to Workspace option from the context menu. The Distance tool opens the Export to Workspace dialog box. You can use this dialog box to specify the names of the variables used to store this information.

After you click OK, the Distance tool creates the variables in the workspace, as in the following example.

whos Name Size Bytes Class distance 1x1 8 double array moon 537x358 192246 uint8 array point1 1x2 16 double array point2 1x2 16 double array

Customizing the Appearance of the Distance Tool

Using the Distance tool context menu, you can customize many aspects of the Distance tool appearance and behavior. Position the pointer over the line and right-click to access these context menu options.

  • Toggling the distance tool label on and off using the Show Distance Label option.

  • Changing the color used to display the Distance tool line using the Set color option.

  • Constraining movement of the tool to either horizontal or vertical using the Constrain drag option.

  • Deleting the distance tool object using the Delete option.

Right-click the Distance tool to access this context menu.

Getting Information About an Image Using the Image Information Tool

To get information about the image displayed in the Image Tool, use the Image Information tool. The Image Information tool can provide two types of information about an image:

  • Basic information — Includes width, height, class, and image type. For grayscale and indexed images, this information also includes the minimum and maximum intensity values.

  • Image metadata — Displays all the metadata from the graphics file that contains the image. This is the same information returned by the imfinfo function or the dicominfo function.

    Note   The Image Information tool can display image metadata only when you specify the filename containing the image to Image Tool, e.g., imtool('moon.tif').

For example, view an image in the Image Tool.

imtool('moon.tif')

Start the Image Information tool by clicking the Image Information button in the Image Tool toolbar or selecting the Image Information option from the Tools menu in the Image Tool. (Another option is to open a figure window using imshow and then call imageinfo from the command line.)

The following figure shows the Image Tool with the Image Information tool. In the figure, the Image Information tool displays both basic image information and image metadata because a file name was specified with imtool.

Image Tool with Image Information Tool

 

Adjusting Image Contrast Using the Adjust Contrast Tool

Understanding Contrast Adjustment

An image lacks contrast when there are no sharp differences between black and white. Brightness refers to the overall lightness or darkness of an image.

To change the contrast or brightness of an image, the Adjust Contrast tool performs contrast stretching. In this process, pixel values below a specified value are displayed as black, pixel values above a specified value are displayed as white, and pixel values in between these two values are displayed as shades of gray. The result is a linear mapping of a subset of pixel values to the entire range of grays, from black to white, producing an image of higher contrast.

The following figure shows this mapping. Note that the lower limit and upper limit mark the boundaries of the window, displayed graphically as the red-tinted window in the Adjust Contrast tool — see Starting the Adjust Contrast Tool

Relationship of Pixel Values to Display Range

The Adjust Contrast tool accomplishes this contrast stretching by modifying the CLim property of the axes object that contains the image. The CLim property controls the mapping of image pixel values to display intensities.

By default, the Image Tool sets the CLim property to the default display range according to the data type. For example, the display range of an image of class uint8 is from 0 to 255. When you use the Adjust Contrast tool, you change the contrast in the image by changing the display range which affects the mapping between image pixel values and the black-to-white range. You create a window over the range that defines which pixels in the image map to the black in the display range by shrinking the range from the bottom up.

Starting the Adjust Contrast Tool

This section describes how to use the Adjust Contrast tool in the Image Tool.

  1. View an image in the Image Tool.

    imtool('moon.tif')

    You can also use the Adjust Contrast tool independent of the Image Tool by calling the imcontrast function.

  2. Click Adjust Contrast in the Image Tool toolbar, or select the Adjust Contrast option from the Image Tool Tools menu. The Adjust Contrast tool opens in a separate window containing a histogram of the image displayed in the Image Tool. The histogram shows the data range of the image and the display range of the image. The data range is the range of intensity values actually used in the image. The display range is the black-to-white mapping used to display the image, which is determined by the image class. The Adjust Contrast tool works by manipulating the display range; the data range of the image remains constant.

    For example, in the following figure, the histogram for the image shows that the data range of the image is 74 to 224 and the display range is the default display range for the uint8 class, 0 to 255. The pixel values for the image are clustered in the middle of the display range. Adjusting the contrast spreads the pixel values across the display range, revealing much more detail in the image.

    To adjust the contrast of the image, you can manipulate the red-tinted rectangular box, called a window, that the Adjust Contrast tool overlays on the histogram. By changing the size and position of this window using the mouse, you can modify the display range of the image and improve its contrast and brightness — see Using the Histogram Window to Adjust Image Contrast.

    Adjust Contrast in Image Tool

    Note   You can also use the Window/Level tool to adjust contrast and brightness using the mouse. (The name comes from medical applications.) Click Window/Level in the Image Tool toolbar or select the Window/Level option from the Image Tool Tools menu. For more information about using the Window/Level tool, see Using the Window/Level Tool to Adjust Image Contrast.

    When you close the Adjust Contrast tool, the Window/Level tool remains active. To turn off the Window/Level tool, click the Window/Level button or one of the navigation buttons in the Image Tool toolbar.

Using the Histogram Window to Adjust Image Contrast

To adjust image contrast using the Adjust Contrast tool, you manipulate the size of the red-tinted window displayed over the histogram, using any of the following methods.

  • Grabbing one of the red handles on the right and left edges of the window and dragging it. You can also change the position of the window by grabbing the center line and dragging the window to the right or left.

  • Specifying the size and position of the window in the Minimum and Maximum fields. You can also define these values by clicking the dropper button associated with these fields. When you do this, the pointer becomes an eye dropper shape. Position the eye dropper pointer over the pixel in the image that you want to be the minimum (or maximum) value and click the mouse button.

  • Specifying the size and position of the window in the Width and Center fields.

  • Selecting the Match data range button in the Scale Display Range part of the tool. When you choose this option, the size of the window changes from the default display range to the data range of the image.

  • Trimming outliers at the top and bottom of the image data range. If you select the Eliminate outliers option, the Adjust Contrast tool removes the top 1% and the bottom 1%, but you can specify other percentages. When you specify a percentage, the Adjust Contrast tool applies half the percentage to the top and half to the bottom. (You can perform this same operation using the stretchlim function.)

The following figure shows these methods of adjusting contrast. The Image Tool updates the display range values displayed in the lower right corner of the Image Tool as you change the size of the window.

Using the Window/Level Tool to Adjust Image Contrast

To start the Window/Level tool, click Window/Level in the Image Tool toolbar.

Using the Window/Level tool, you can change the contrast and brightness of an image by simply dragging the mouse over the image. Moving the mouse horizontally affects contrast; moving the mouse vertically affects brightness. Note that any contrast adjustments you make using the Window/Level tool are reflected immediately in the Adjust Contrast tool. For example, if you increase the brightness, the window in the Adjust Contrast moves over the histogram.

The following table summarizes how these mouse motions affect the size and position of the window in the Adjust Contrast tool.

Mouse Motion

 

Effect

Horizontally to the left

Shrinks the window from both sides.

Horizontally to the right

Expands the window from both sides.

Vertically up

Moves the window to the right over the histogram, increasing brightness.

Vertically down

Moves the window to the left over the image histogram, decreasing brightness.

To stop the Window/Level tool, click on the Window/Level button in the Image Tool toolbar, or click any of the navigation buttons in the toolbar.

Example: Adjusting Contrast with the Window/Level Tool

  1. Read an image from a sample DICOM file included with the toolbox.

    I = dicomread('CT-MONO2-16-ankle.dcm');
  2. View the image data using the Image Tool. Because the image data is signed 16-bit data, this example uses the autoscaling syntax.
    imtool(I,'DisplayRange',[])
  3. Click the Window/Level button to start the tool, or select Window/Level from the Tools menu in the Image Tool.

  4. Move the pointer over the image. The pointer changes to the Window/Level cursor .

  5. Click and drag the left (or right) mouse button and move the pointer horizontally to the left or right to adjust the contrast, or vertically up or down to change the brightness.

Modifying Image Data

By default, the Adjust Contrast tool adjusts the values of the pixels used to display the image in the Image Tool but does not change the actual image data. To modify pixel values in the image to reflect the contrast adjustments you made, you must click the Adjust Data button.

The following example illustrates this process.

  1. Display an image in the Image Tool. The example opens an image from a file.

    imtool('moon.tif');
  2. Start the Adjust Contrast tool by clicking the Adjust contrast button, , or by selecting Adjust Contrast from the Tools menu in the Image Tool.
  3. Adjust the contrast of the image. Use one of the mechanisms provided by Adjust Contrast tool, such as resizing the window over the histogram. See Using the Histogram Window to Adjust Image Contrast. You can also adjust contrast using the Window/Level tool, moving the pointer over the image.

  4. Adjust the image data to reflect the contrast adjustment you just made. Click the Adjust Data button in the Adjust Contrast Tool. When you click the Adjust Data button, the histogram will update. You can then adjust the contrast again, if necessary. If you have other interactive modular tool windows open, they will update automatically to reflect the contrast adjustment.

      Note   The Adjust Data button is unavailable until you make a change to the contrast of the image.

Saving the Modified Image Data

By default, if you close the Image Tool, it does not save the modified image data. To save these changed values, use the Save As option from the Image Tool File menu to store the modified data in a file or use the Export to Workspace option to save the modified data in a workspace variable.

Viewing Image Sequences

Overview

Some applications create collections of images related by time, such as frames in a movie, or by (spatial location, such as magnetic resonance imaging (MRI) slices. These collections of images are referred to by a variety of names, such as image sequences, image stacks, or videos.

The toolbox represents image sequences as four-dimensional arrays, where each separate image is called a frame, all frames are the same size, and the frames are concatenated along the fourth dimension. imtool and imshow can display one frame at a time, using standard MATLAB array indexing syntax, but cannot animate the sequence or provide any navigation within the sequence. A better choice to view image sequences is the Movie Player (implay). The Movie Player can animate the display of frames in an image sequence and provides playback controls that you can use to navigate among the frames in the sequence. To get a static view of all the frames in an image sequence at one time, use the montage function. For more information, see these additional topics.

Viewing Image Sequences in the Movie Player

This section describes how to use the Movie Player to view image sequences and provides information about configuring the Movie Player.

Example: Viewing a Sequence of MRI Images

  1. Load the image sequence into the MATLAB workspace. For this example, load the MRI data from the file mristack.mat, which is included in the imdemos directory.

    load mristack

    This places a variable named mristack in your workspace. The variable is an array of 21 grayscale frames containing MRI images of the brain. Each frame is a 256-by-256 array of uint8 data.

    mristack       256x256x21       1276256     uint8
  2. View the image sequence in the Movie Player. Call implay, specifying the name of the image sequence variable as an argument. You can also specify the name of a file that contains an image sequence, such as an Audio Video Interleaved (AVI) file.

    implay(mristack)

    You can also import an image sequence from the workspace into the Movie Player using the Import from workspace option on the File menu.

    The Movie Player opens, displaying the first frame of the image sequence. Note how the Movie Player displays information about the image sequence, such as the size of each frame and the total number of frames, at the bottom of the window.

  3. Explore the image sequence using Movie Player Playback controls.

    To view the image sequence or video as an animation, click the Play button in the Playback toolbar, select Play from the Playback menu, or press P or the Space bar. By default, the Movie Player plays the image sequence forward, once in its entirety, but you can view the frames in the image sequence in many ways, described in this table. As you view an image sequence, note how the Movie Player updates the Status Bar at the bottom of the window.

    Viewing Option Playback Control Keyboard Shortcut
    Specify the direction in which to play the image sequence. Click the Playback mode button in the Playback toolbar or select Playback Modes from the Playback menu. You can select forward, backward, or autoreverse. As you click the playback mode button, it cycles through these options and the appearance changes to indicate the current selection. A
    View the sequence repeatedly. Click the Repeat button in the Playback toolbar or select Playback Modes > Repeat from the Playback menu. You toggle this option on or off. R
    Jump to a specific frame in the sequence. Click the Jump to button in the Playback toolbar or select Jump to from the Playback menu. This option opens a dialog box in which you can specify the number of the frame. J
    Stop the sequence. Click the Stop button in the Playback toolbar or select Stop from the Playback menu. This button is only enabled when an image sequence is playing. S
    Step through the sequence, one frame at a time, or jump to the beginning or end of the sequence (rewind). Click one of the navigation buttons in the Playback toolbar, in the desired direction, or select an option, such as Fast Forward or Rewind from the Playback menu.

    Arrow keysPage Up/Page Down

    L (last frame) F (first frame)


  4. Change the view of the image sequence or examine a frame more closely.

    The Movie Player supports several tools listed in the Tools menu and on the Toolbar that you can use to examine the frames in the image sequence more closely.

    Viewing Option Playback Control
    Zoom in or out on the image, and pan to change the view. Click one of the zoom buttons in the toolbar or select Zoom In or Zoom Out from the Tools menu. Click the Pan button in the toolbar or select Pan from the Tools menu. If you click Maintain fit to window button in the toolbar or select Maintain fit to window or from the Tools menu, the zoom and pan buttons are disabled.
    Examine an area of the current frame in detail. Click the Pixel region button in the Playback toolbar or select Pixel Region from the Tools menu.
    Export frame to Image Tool Click the Export to Image tool button in the Playback toolbar or select Export to Image Tool from the File menu. The Movie Player opens an Image Tool containing the current frame.

Configuring the Movie Player

The Movie Player Configuration dialog box enables you to change the appearance and behavior of the player. To open the Configuration dialog box, select File > Configuration > Edit. (To load a preexisting configuration set, select File > Configuration > Load.)

The Movie Player displays the Configuration dialog box. This dialog box contains four panes, Core, Sources, Visuals, and Tools, which each offer various options. On each pane, you can select a category and then click Options to view configuration settings.

The following table lists the options that are available for each category on every pane.

Pane Option Category Option Descriptions
Core General UI

Display the full source path in the title bar check box — Select to display the full path to the video data source in the title bar. By default, Movie Player displays a shortened name in the title bar.

Open message log menu — Specify when the Message log window opens. You can use the Message log window to debug issues with video playback. By default, the window only opens for failure messages.

Core Source UI

Keyboard commands respect playback mode check box — Select to make keyboard shortcut keys aware of your playback mode selection. If you clear this check box, the keyboard shortcut keys behave as if the playback mode is set to Forward play and Repeat is set to off.

Recently used sources list parameter — Specifies the number of sources listed in the File menu.

Sources File

Select the Enabled check box to enable connections to files (the default).

Default open file path parameter — Specify the directory that is displayed in the Connect to File dialog box when you click File > Open.

Sources Workspace

Select the Enabled check box to enable connections to variables in the workspace (the default). There are no options associated with this selection.

Sources Simulink

Select the Enabled check box to enable connections to Simulink models. You must have Simulink installed.

Visuals Video Select the Enabled check box to use video visualization.
Visuals User-defined Vector Select the Enabled check box to use user-defined vector visualization.
Visuals Time Vector Select the Enabled check box to use time vector visualization.
Visuals Frequency Vector Select the Enabled check box to use frequency vector visualization.
Visuals Statistics Select the Enabled check box to use statistics visualization.
Tools Image Tool Select the Enabled check box to include the Image Tool.

Open new Image Tool window for each export check box — Opens a new Image Tool for each exported frame.

Tools Pixel Region Select the Enabled check box to include the Pixel Region tool in the Movie Player (the default).
Tools Image Navigation Tools Select the Enabled check box to include the zoom and pan tools in the Movie Player (the default).
Tools Instrumentation Sets Select the Enabled check box to include instrumentation sets in the Movie Player. Provides a way to save your current configuration.

Saving Your Configuration Settings.  To save your configuration settings for future use, select File > Configuration Set > Save as.

    Note   By default, the Movie Player uses the configuration settings from the file implay.cfg. If you want to store your configuration settings in this file, you should first create a backup copy of the file.

Specifying the Frame Rate

To decrease or increase the playback rate, select Frame Rate from the Playback menu, or use the keyboard shortcut T. The Frame Rate dialog box displays the frame rate of the source, lets you change the rate at which the Movie Player plays the image sequence or video, and displays the actual playback rate. The playback rate is the number of frames the Movie Player processes per second.

If you want to increase the actual playback rate, but your system's hardware cannot keep up with the desired rate, select the Allow frame drop to achieve desired playback rate check box. This parameter enables the Movie Player to achieve the playback rate by dropping frames. When you select this option, the Frame Rate dialog box displays several additional options that you can use to specify the minimum and maximum refresh rates. If your hardware allows it, increase the refresh rate to achieve a smoother playback. However, if you specify a small range for the refresh rate, the computed frame replay schedule may lead to a choppy replay, and a warning will appear.

Specifying the Color Map

To specify the colormap to apply to the intensity values, select Colormap from the Tools menu, or use the keyboard shortcut C. The Movie Player displays a dialog box that enables you to change the colormap.

Use the Colormap parameter to specify a particular colormap.

If you know that the pixel values do not use the entire data type range, you can select the Specify range of displayed pixel values check box and enter the range for your data. The dialog box automatically displays the range based on the data type of the pixel values.

Getting Information about the Image Frame

To view basic information about the image data, click the Video Information button in the Movie Player toolbar or select Video Information from the Tools menu. The Movie Player displays a dialog box containing basic information about the image sequence, such as the size of each frame, the frame rate, and the total number of frames.

Viewing Image Sequences as a Montage

To view multiple frames in a multiframe array at one time, use the montage function. montage displays all the image frames, arranging them into a rectangular grid. The montage of images is a single image object. The image frames can be grayscale, indexed, or truecolor images. If you specify indexed images, they all must use the same colormap.

This example creates an array of truecolor images and uses montage to display them all at once. Note how montage displays the images in a 2-by-2 grid. The first image frame is displayed in the first position of the first row, the next frame in the second position of the first row, and so on.

onion = imread('onion.png'); onionArray = repmat(onion, [ 1 1 1 4 ]); montage(onionArray);

montage supports several optional parameters that you can use to customize the display. For example, using the 'size' parameter, you can specify the number of rows and columns montage uses to display the images. To display the onion images in one horizontal row, specify the 'size' parameter with the value [1 NaN]. When you specify NaN for a dimension, montage calculates the number of images to display along that dimension. Using montage parameters you can also specify which images in the image array you want to display, and adjust the contrast of the images displayed. See montage for more information.

Converting a Multiframe Image to a Movie

To create a MATLAB movie from a multiframe image array, use the immovie function. This example creates a movie from a multiframe indexed image.

mov = immovie(X,map);

In the example, X is a four-dimensional array of images that you want to use for the movie.

To play the movie, use the implay function.

implay(mov);

This example loads the multiframe image mri.tif and makes a movie out of it. It won't do any good to show the results here, so try it out; it's fun to watch.

mri = uint8(zeros(128,128,1,27)); for frame=1:27 [mri(:,:,:,frame),map] = imread('mri.tif',frame); end mov = immovie(mri,map); implay(mov);

    Note   To view a MATLAB movie, you must have MATLAB software installed. To make a movie that can be run outside the MATLAB environment, use the avifile and addframe functions to create an AVI file, or use movie2avi. AVI files can be created using indexed and RGB images of classes uint8 and double, and don't require a multiframe image.

 

Displaying Different Image Types

If you need help determining what type of image you are working with, see Image Types in the Toolbox.

Displaying Indexed Images

To display an indexed image, using either imshow or imtool, specify both the image matrix and the colormap. This documentation uses the variable name X to represent an indexed image in the workspace, and map to represent the colormap.

imshow(X,map)

or

imtool(X,map)

For each pixel in X, these functions display the color stored in the corresponding row of map. If the image matrix data is of class double, the value 1 points to the first row in the colormap, the value 2 points to the second row, and so on. However, if the image matrix data is of class uint8 or uint16, the value 0 (zero) points to the first row in the colormap, the value 1 points to the second row, and so on. This offset is handled automatically by the imtool and imshow functions.

If the colormap contains a greater number of colors than the image, the functions ignore the extra colors in the colormap. If the colormap contains fewer colors than the image requires, the functions set all image pixels over the limits of the colormap's capacity to the last color in the colormap. For example, if an image of class uint8 contains 256 colors, and you display it with a colormap that contains only 16 colors, all pixels with a value of 15 or higher are displayed with the last color in the colormap.

Displaying Grayscale Images

To display a grayscale image, using either imshow or imtool, specify the image matrix as an argument. This documentation uses the variable name I to represent a grayscale image in the workspace.

imshow(I)

or

imtool(I)

Both functions display the image by scaling the intensity values to serve as indices into a grayscale colormap.

If I is double, a pixel value of 0.0 is displayed as black, a pixel value of 1.0 is displayed as white, and pixel values in between are displayed as shades of gray. If I is uint8, then a pixel value of 255 is displayed as white. If I is uint16, then a pixel value of 65535 is displayed as white.

Grayscale images are similar to indexed images in that each uses an m-by-3 RGB colormap, but you normally do not specify a colormap for a grayscale image. MATLAB displays grayscale images by using a grayscale system colormap (where R=G=B). By default, the number of levels of gray in the colormap is 256 on systems with 24-bit color, and 64 or 32 on other systems. (See Displaying Colors for a detailed explanation.)

Displaying Grayscale Images That Have Unconventional Ranges

In some cases, the image data you want to display as a grayscale image could have a display range that is outside the conventional toolbox range (i.e., [0,1] for single or double arrays, [0,255] for uint8 arrays, [0,65535] for uint16 arrays, or [-32767,32768] for int16 arrays). For example, if you filter a grayscale image, some of the output data could fall outside the range of the original data.

To display unconventional range data as an image, you can specify the display range directly, using this syntax for both the imshow and imtool functions.

imshow(I,'DisplayRange',[low high])

or

imtool(I,'DisplayRange',[low high])

If you use an empty matrix ([]) for the display range, these functions scale the data automatically, setting low and high to the minimum and maximum values in the array.

The next example filters a grayscale image, creating unconventional range data. The example calls imtool to display the image, using the automatic scaling option. If you execute this example, note the display range specified in the lower right corner of the Image Tool window.

I = imread('testpat1.png');  J = filter2([1 2;-1 -2],I);  imtool(J,'DisplayRange',[]);

 

Displaying Binary Images

In MATLAB, a binary image is of class logical. Binary images contain only 0's and 1's. Pixels with the value 0 are displayed as black; pixels with the value 1 are displayed as white.

    Note   For the toolbox to interpret the image as binary, it must be of class logical. Grayscale images that happen to contain only 0's and 1's are not binary images.

To display a binary image, using either imshow or imtool, specify the image matrix as an argument. For example, this code reads a binary image into the MATLAB workspace and then displays the image. This documentation uses the variable name BW to represent a binary image in the workspace

BW = imread('circles.png');  imshow(BW)

or

imtool(BW)

Changing the Display Colors of a Binary Image

You might prefer to invert binary images when you display them, so that 0 values are displayed as white and 1 values are displayed as black. To do this, use the NOT (~) operator in MATLAB. (In this figure, a box is drawn around the image to show the image boundary.) For example:

imshow(~BW)  

or

imtool(~BW)  

You can also display a binary image using the indexed image colormap syntax. For example, the following command specifies a two-row colormap that displays 0's as red and 1's as blue.

imshow(BW,[1 0 0; 0 0 1])

or

imtool(BW,[1 0 0; 0 0 1])

Displaying Truecolor Images

Truecolor images, also called RGB images, represent color values directly, rather than through a colormap. A truecolor image is an m-by-n-by-3 array. For each pixel (r,c) in the image, the color is represented by the triplet (r,c,1:3).

To display a truecolor image, using either imshow or imtool, specify the image matrix as an argument. For example, this code reads a truecolor image into the MATLAB workspace and then displays the image. This documentation uses the variable name RGB to represent a truecolor image in the workspace

RGB = imread(`peppers.png');  imshow(RGB)

or

imtool(RGB)

Systems that use 24 bits per screen pixel can display truecolor images directly, because they allocate 8 bits (256 levels) each to the red, green, and blue color planes. On systems with fewer colors, imshow displays the image using a combination of color approximation and dithering. See Displaying Colors for more information.

    Note   If you display a color image and it appears in black and white, check if the image is an indexed image. With indexed images, you must specify the colormap associated with the image. For more information, see Displaying Indexed Images.

 

Printing Images

If you want to output a MATLAB image to use in another application (such as a word-processing program or graphics editor), use imwrite to create a file in the appropriate format. See Writing Image Data to a File for details.

If you want to print an image, use imshow to display the image in a MATLAB figure window. If you are using the Image Tool, you must use the Print to Figure option on the Image Tool File menu. When you choose this option, the Image Tool opens a separate figure window and displays the image in it. You can access the standard MATLAB printing capabilities in this figure window. You can also use the Print to Figure option to print the image displayed in the Overview tool and the Pixel Region tool.

Once the image is displayed in a figure window, you can use either the MATLAB print command or the Print option from the File menu of the figure window to print the image. When you print from the figure window, the output includes nonimage elements such as labels, titles, and other annotations.

Printing and Handle Graphics Object Properties

The output reflects the settings of various properties of Handle Graphic objects. In some cases, you might need to change the settings of certain properties to get the results you want. Here are some tips that could be helpful when you print images:

  • Image colors print as shown on the screen. This means that images are not affected by the figure object's InvertHardcopy property.

  • To ensure that printed images have the proper size and aspect ratio, set the figure object's PaperPositionMode property to auto. When PaperPositionMode is set to auto, the width and height of the printed figure are determined by the figure's dimensions on the screen. By default, the value of PaperPositionMode is manual. If you want the default value of PaperPositionMode to be auto, you can add this line to your startup.m file.

    set(0,'DefaultFigurePaperPositionMode','auto')

For detailed information about printing with File/Print or the print command (and for information about Handle Graphics), see Printing and Exporting in the MATLAB Graphics documentation. For a complete list of options for the print command, enter help print at the MATLAB command-line prompt or see the print command reference page in the MATLAB documentation.

Setting Toolbox Preferences

Viewing and Changing Preferences Using the Preferences Dialog Box

You can view and change Image Processing Toolbox Preferences in the Preferences dialog box. To open this dialog box, select Preferences > Image Processing in the File menu in the MATLAB desktop or in the Image Tool, or type iptprefs at the command line.

Retrieving the Values of Toolbox Preferences Programmatically

You can use Image Processing Toolbox preferences to control certain characteristics of how imshow and imtool display images on your screen. For example, using toolbox preferences, you can specify the initial magnification used by imtool and imshow.

To determine the current value of a preference, use the iptgetpref function. This example uses iptgetpref to determine the value of the ImtoolInitialMagnification preference.

iptgetpref('ImtoolInitialMagnification') ans = 100

Preference names are case insensitive and can be abbreviated. For a complete list of toolbox preferences, see the iptprefs reference page.

Setting the Values of Toolbox Preferences Programmatically

To specify the value of a toolbox preference, use the iptsetpref function. This example calls iptsetpref to specify that imshow resize the figure window so that it fits tightly around displayed images.

iptsetpref('ImshowBorder', 'tight');

For a table of the available preferences, see the iptprefs reference page.

Source: http://www.mathworks.com