Adjust Image Contrast In Image Viewer App

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 Open 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 Viewer 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.

Open the Adjust Contrast Tool

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

  1. View an image in the Image Viewer.

    imtool('moon.tif')

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

  2. Click Adjust Contrast in the Image Viewer toolbar, or select the Adjust Contrast option from the Image Viewer Tools menu. The Adjust Contrast tool opens in a separate window containing a histogram of the image displayed in the Image Viewer. 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 Adjust Image Contrast Using the Histogram Window.

    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 Viewer toolbar or select the Window/Level option from the Image Viewer Tools menu. For more information about using the Window/Level tool, see Adjust Image Contrast Using Window/Level Tool.

    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 Viewer toolbar.

Adjust Image Contrast Using the Histogram Window

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 Viewer updates the display range values displayed in the lower right corner of the Image Viewer as you change the size of the window.

Adjust Image Contrast Using Window/Level Tool

To start the Window/Level tool, click Window/Level in the Image Viewer 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 the Window/Level button in the Image Viewer toolbar, or click any of the navigation buttons in the toolbar.

Adjust 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 Viewer. 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 Viewer.

  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.

Make Contrast Adjustments Permanent

By default, the Adjust Contrast tool adjusts the values of the pixels used to display the image in the Image Viewer 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 Viewer. 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 Viewer.

  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 Adjust Image Contrast Using the Histogram Window. 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 Viewer, it does not save the modified image data. To save these changed values, use the Save As option from the Image Viewer 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.

Was this topic helpful?