Main Content

Adjust Image Contrast in Image Viewer App

Image contrast refers to the difference in brightness between the lightest and darkest parts of a grayscale image. An image lacks contrast when there is no sharp difference between the lowest and highest pixel values. Contrast adjustment works by manipulating the display range, or the range of pixel values that map to different display colors. Pixel values within the display range appear as shades of gray. Pixels values less than or equal to the display range minimum appear black. Pixel values greater than or equal to the display range maximum appear white.

This figure shows an image before and after contrast adjustment. In the original image, the pixel values occupy fewer shades of gray, and the image has poor contrast. The adjusted image stretches the pixel values to occupy the full range of colors from black to white, and the image has higher contrast.

Side-by-side comparison of an image with low contrast and a version with higher contrast after stretching the data range to the full display range of the data type

Load Image into Image Viewer

Load a grayscale image into the app by using the imageViewer function, or by selecting Import Image from the app toolstrip. For more information about opening images in the app, see Open Image Viewer App.

The Contrast tab of the app toolstrip displays information about the image contrast. The Data Range section displays the minimum and maximum pixel values of the imported image. The Window Bounds section displays the minimum, maximum, width, and center of the display range window, and has a drop-down list of preset options.

By default, the drop-down value is Match Datatype Range, and the window bounds values match the default display range for the data type. The default display range for integer data types is the full range of that data type. For example, the default display range for uint8 is 0 to 255, such that 0 appears black and 255 appears white. The default display range for the single and double data types is 0 to 1, such that 0 appears black and 1 appears white.

This figure shows the default contrast information for a uint8 image with a data range of 74 to 224. The histogram shows a cluster of values in the middle of the default display range window.

Grayscale image with poor contrast in the Image Viewer. The window bounds are wider than the histogram of pixel values.

Adjust Contrast and Brightness

To adjust the contrast of an image, modify the display range window using one of the methods listed in this table.

DescriptionImage

Scale the display range automatically.

  • Select Match Data Range to make the display range equal to the data range of the image.

  • Select Eliminate Outliers (%) to saturate an equal percentage of pixels at the top and bottom of the display range. By default, the tool eliminates 2%, so the top 1% and the bottom 1% of pixel values. This is equivalent to the operation performed by the stretchlim function. Adjust the percentage by entering a value in the field below the drop down.

  • Select Match Datatype Range to restore the default display range based on the data type of the image.

If you select an automatic preset, the other fields in the Window Bounds section update to reflect the new display range window.

Automatically scale display range

Interactively adjust the minimum and maximum values of the display range in the Histogram pane by dragging the left and right edges of the window. Change the center of the window by dragging the interior of the window.

Interactively adjust display window by dragging the window limits

Enter specific values in the Window Bounds section of the app toolstrip. You can also define the minimum or maximum value by clicking the dropper button next to the corresponding field. When you do this and move the pointer over the image, the pointer becomes an eye dropper shape. Position the eye dropper shape over the pixel with the value that you want to be the minimum or maximum value and left-click.

Manually enter display window values

This animation shows how to increase the image contrast by selecting Eliminate Outliers (%) from the Window Bounds section of the app toolstrip. The display range window changes to 78 to 161. When you change the display range window, the image display updates in real time. You can restore the default display range window by selecting Undo Changes.

Animation showing the effect of selecting Eliminate Outliers on the displayed image.

View Imported and Adjusted Image Values

As you modify the display range window, Image Viewer calculates adjusted values for each pixel by linearly scaling the display window to the data type range. This is similar to calling the imadjust function and specifying the window bounds as the input contrast limits and the data type range as the output contrast limits. For example, this figure shows a graphical representation of mapping imported values between 78 and 161 to an adjusted value range of 0 to 255. Imported values less than or equal to 78 map to an adjusted value of 0. Imported values greater than or equal to 161 map to 255.

Graphical representation of contrast stretching, with arrows pointing from imported values between 78 and 161 to their corresponding adjusted values from 0 to 255

After you adjust the contrast, you can view both the imported and adjusted value for each pixel. When you pause on the image, the bottom-left corner of the app window displays information about the pixel that is under the cursor. After making contrast changes, the information includes the imported and adjusted value for the pixel. If you zoom in far enough on the image, with Show Pixel Values selected, Image Viewer displays the imported value I and adjusted value Adj overlaid on individual pixels. For more details on zooming to view pixel values, see Get Pixel Information in Image Viewer App.

Pixel info showing the imported and adjusted value

Export Contrast-Adjusted Image

You can export the contrast-adjusted image to a file or to a workspace variable. On the Viewer tab of the app toolstrip, select Export > Export Image, and select either To Workspace or To Image File. The app saves the adjusted pixel values using the same data type as the imported image.

See Also

| | |

Related Topics