# stretchlim

Find limits to contrast stretch image

## Syntax

• ``Low_High = stretchlim(I)``
example
• ``Low_High = stretchlim(I,Tol)``
• ``Low_High = stretchlim(RGB,Tol)``
• ``Low_High = stretchlim(gpuarrayI,___)``

## Description

example

````Low_High = stretchlim(I)` returns `Low_High`, a two-element vector of pixel values that specify lower and upper limits that can be used for contrast stretching image `I`. By default, values in `Low_High` specify the bottom 1% and the top 1% of all pixel values. The gray values returned can be used by the `imadjust` function to increase the contrast of an image.```
````Low_High = stretchlim(I,Tol)` returns `Low_High`, a two-element vector of pixel values that specify lower and upper limits that can be used for contrast stretching image `I`. where `Tol` specifies the fraction of the image to saturate at low and high pixel values.```
````Low_High = stretchlim(RGB,Tol)` returns `Low_High`, a two-element vector of pixel values that specify lower and upper limits that can be used for contrast stretching truecolor image `RGB`. ```
````Low_High = stretchlim(gpuarrayI,___)` performs the operation on a GPU. . This syntax requires the Parallel Computing Toolbox™.Code Generation support: Yes.MATLAB Function Block support: Yes.```

## Examples

collapse all

### Find Limits to Stretch Contrast in Grayscale Image

Read grayscale image into the workspace and display it.

```I = imread('pout.tif'); figure imshow(I) ```

Adjust the contrast in the image using `stretchlim` to set the limits, and display the result. The example uses the default limits `[0.01 0.99]`, saturating the upper 1% and the lower 1%.

```J = imadjust(I,stretchlim(I),[]); figure imshow(J) ```

### Find Limits to Stretch Contrast in Grayscale Image on a GPU

Read grayscale image, creating a gpuArray.

```gpuarrayI = gpuArray(imread('pout.tif')); figure, imshow(gpuarrayI)```

Adjust the contrast in the image using `stretchlim` to set the limits. Display the result.

```gpuarrayJ = imadjust(gpuarrayI,stretchlim(I),[]); figure, imshow(gpuarrayJ) ```

## Input Arguments

collapse all

### `I` — Grayscale imagereal, nonsparse, numeric array

Grayscale image, specified as a real, nonsparse, numeric array.

Example: ```I = imread(‘pout.tif'); lohi = stretchlim(I);```

Data Types: `single` | `double` | `int8` | `int16` | `int32` | `int64` | `uint8` | `uint16` | `uint32` | `uint64` | `logical`

### `Tol` — Fraction of the image to saturate`[0.01 0.99]` (default) | numeric scalar or two-element vector in the range `[0 1]`

Fraction of the image to saturate, specified as a numeric scalar or two-element vector `[Low_Fract High_Fract]` in the range `[0 1]`.

ValueDescription
ScalarIf `Tol` is a scalar, ```Low_Fract = Tol```, and `High_Fract = 1 - Low_Fract`, which saturates equal fractions at low and high pixel values.
0If `Tol = 0`, ```Low_High = [min(I(:)); max(I(:))]```.
DefaultIf you omit the `Tol` argument, ```[Low_Fract High_Fract]``` defaults to `[0.01 0.99]`, saturating 2%.
Too bigIf `Tol` is too big, such that no pixels would be left after saturating low and high pixel values, `stretchlim` returns ```[0 1]```

Example: `lohi = stretchlim(I,[.02 .80]);`

Data Types: `single` | `double` | `int8` | `int16` | `int32` | `int64` | `uint8` | `uint16` | `uint32` | `uint64`

### `RGB` — Truecolor imagereal, nonsparse, numeric array

Truecolor image, specified as a numeric array.

Example: ```RGB = imread(‘peppers.png'); lohi = stretchlim(RGB); ```

Data Types: `single` | `double` | `int8` | `int16` | `int32` | `uint8` | `uint16` | `uint32`

### `gpuarrayI` — Input imagegpuArray

Input image, specified as a gpuArray.

## Output Arguments

collapse all

### `Low_High` — Lower and upper limits for contrast stretchingtwo-element vector of pixel values

Lower and upper limits for contrast stretching, returned as a two-element vector of pixel values

expand all

### Code Generation

This function supports the generation of C code using MATLAB® Coder™. Note that if you choose the generic `MATLAB Host Computer` target platform, the function generates code that uses a precompiled, platform-specific shared library. Use of a shared library preserves performance optimizations but limits the target platforms for which code can be generated. For more information, see Understanding Code Generation with Image Processing Toolbox.

### MATLAB Function Block

You can use this function in the MATLAB Function Block in Simulink.