Main Content

Specify Contrast Adjustment Limits

You can optionally specify the range of the input values and the output values usingimadjust. You specify these ranges in two vectors that you pass toimadjustas arguments. The first vector specifies the low- and high-intensity values that you want to map. The second vector specifies the scale over which you want to map them.

Note

You must specify the intensities as values between 0 and 1 regardless of the class ofI. IfIisuint8, the values you supply are multiplied by 255 to determine the actual values to use; ifIisuint16, the values are multiplied by 65535. To learn about an alternative way to set these limits automatically, seeSet Image Intensity Adjustment Limits Automatically.

Specify Contrast Adjustment Limits as Range

This example shows how to specify contrast adjustment limits as a range using theimadjustfunction. This example decreases the contrast of an image by narrowing the range of the data.

Read an image into the workspace.

I = imread('cameraman.tif');

Adjust the contrast of the image, specifying the range of values used in the output image. In the example below, the man's coat is too dark to reveal any detail.imadjustmaps the range[0,51]in theuint8input image to[128,255]in the output image. This brightens the image considerably, and also widens the dynamic range of the dark portions of the original image, making it much easier to see the details in the coat. Note, however, that because all values above 51 in the original image are mapped to 255 (white) in the adjusted image, the adjusted image appears washed out.

J = imadjust(I,[0 0.2],[0.5 1]);

Display the original image and the contrast-adjusted image.

imshowpair(I,J,'montage')

Figure contains an axes object. The axes object contains an object of type image.

Set Image Intensity Adjustment Limits Automatically

For a more convenient way to specify limits, use thestretchlimfunction. (Theimadjustfunction usesstretchlimfor its simplest syntax,imadjust(I).)

这个函数计算直方图的图像放大e and determines the adjustment limits automatically. Thestretchlimfunction returns these values as fractions in a vector that you can pass as the[low_in high_in]argument toimadjust; for example:

I = imread('rice.png'); J = imadjust(I,stretchlim(I),[0 1]);

By default,stretchlimuses the intensity values that represent the bottom 1% (0.01) and the top 1% (0.99) of the range as the adjustment limits. By trimming the extremes at both ends of the intensity range,stretchlimmakes more room in the adjusted dynamic range for the remaining intensities. But you can specify other range limits as an argument tostretchlim. See thestretchlimreference page for more information.