You can optionally specify the range of the input values and the output values using
imadjust. You specify these ranges in two vectors that you pass
imadjust as 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.
You must specify the intensities as values between 0 and 1 regardless of the class
values you supply are multiplied by 255 to determine the actual values to use; if
uint16, the values are multiplied by
65535. To learn about an alternative way to set these limits automatically, see
Set Image Intensity Adjustment Limits Automatically.
This example shows how to specify contrast adjustment limits as a range using the
imadjust function. 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.
imadjust maps the range
[0,51] in the
uint8 input 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.
For a more convenient way to specify limits, use the
stretchlim function. (The
imadjust function uses
stretchlim for its simplest syntax,
This function calculates the histogram of the image and determines the adjustment
limits automatically. The
stretchlim function returns these
values as fractions in a vector that you can pass as the
high_in] argument to
imadjust; for example:
I = imread('rice.png'); J = imadjust(I,stretchlim(I),[0 1]);
stretchlim uses 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,
stretchlim makes more room in the adjusted dynamic range for
the remaining intensities. But you can specify other range limits as an argument to
stretchlim. See the
stretchlim reference page for more information.