Documentation

### This is machine translation

Mouseover text to see original. Click the button below to return to the English version of the page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

# histeq

Enhance contrast using histogram equalization

## Syntax

``J = histeq(I,hgram)``
``J = histeq(I,n)``
``````[J,T] = histeq(I)``````
``newmap = histeq(X,map)``
``newmap = histeq(X,map,hgram)``
``````[newmap,T] = histeq(X,___)``````

## Description

example

````J = histeq(I,hgram)` transforms the grayscale image `I` so that the histogram of the output grayscale image `J` with `length(hgram)` bins approximately matches the target histogram `hgram`.You optionally can perform histogram equalization of grayscale images using a GPU (requires Parallel Computing Toolbox™). For more information, see Image Processing on a GPU.```
````J = histeq(I,n)` transforms the grayscale image `I`, returning in `J` an grayscale image with `n` discrete gray levels. A roughly equal number of pixels is mapped to each of the `n` levels in `J`, so that the histogram of `J` is approximately flat. The histogram of `J` is flatter when `n` is much smaller than the number of discrete levels in `I`. ```
``````[J,T] = histeq(I)``` returns the grayscale transformation `T` that maps gray levels in the image `I` to gray levels in `J`.```
````newmap = histeq(X,map)` transforms the values in the colormap so that the histogram of the gray component of the indexed image `X` is approximately flat. It returns the transformed colormap in `newmap`.This syntax is not supported on a GPU.```
````newmap = histeq(X,map,hgram)` transforms the colormap associated with the indexed image `X` so that the histogram of the gray component of the indexed image (`X`,`newmap`) approximately matches the target histogram `hgram`. The `histeq` function returns the transformed colormap in `newmap`. `length(hgram)` must be the same as `size(map,1)`.This syntax is not supported on a GPU.```
``````[newmap,T] = histeq(X,___)``` returns the grayscale transformation `T` that maps the gray component of `map` to the gray component of `newmap`.This syntax is not supported on a GPU.```

## Examples

collapse all

Read an image into the workspace.

`I = imread('tire.tif');`

Enhance the contrast of an intensity image using histogram equalization.

`J = histeq(I);`

Display the original image and the adjusted image.

```imshowpair(I,J,'montage') axis off``` Display a histogram of the original image.

```figure imhist(I,64)``` Display a histogram of the processed image.

```figure imhist(J,64)``` Load a 3-D dataset.

`load mristack`

Perform histogram equalization.

`enhanced = histeq(mristack);`

Display the first slice of data for the original image and the contrast-enhanced image.

```figure subplot(1,2,1) imshow(mristack(:,:,1)) title('Slice of Original Image') subplot(1,2,2) imshow(enhanced(:,:,1)) title('Slice of Enhanced Image')``` This example performs the same histogram equalization on the GPU.

```I = gpuArray(imread('tire.tif')); J = histeq(I); figure imshow(I) figure imshow(J)```

## Input Arguments

collapse all

Input grayscale image, specified as a numeric array of any dimension.

To perform histogram equalization using a GPU, specify `I` as a `gpuArray` that contains a numeric array of any dimension.

Data Types: `single` | `double` | `int16` | `uint8` | `uint16`

Target histogram, specified as a numeric vector. `hgram` has equally spaced bins with intensity values in the appropriate range:

• [0, 1] for images of class `double` or `single`

• [0, 255] for images of class `uint8`

• [0, 65535] for images of class `uint16`

• [-32768, 32767] for images of class `int16`

`histeq` automatically scales `hgram` so that `sum(hgram)=numel(I)`. The histogram of `J` will better match `hgram` when `length(hgram)` is much smaller than the number of discrete levels in `I`.

If you perform histogram equalization using a GPU, then you optionally can specify `hgram` as a `gpuArray` that contains a numeric vector.

Data Types: `single` | `double`

Number of discrete gray levels, specified as a scalar.

Data Types: `single` | `double`

Indexed image, specified as a numeric array of any dimension. The values in `X` are an index into the colormap `map`.

Data Types: `single` | `double` | `uint8` | `uint16`

Colormap, specified as an c-by-3 array. Each row specifies an RGB color value.

Data Types: `double`

## Output Arguments

collapse all

Output grayscale image, returned as a numeric array of the same size and class as the input image `I`.

If histogram equalization is performed using a GPU, then `J` is returned as a `gpuArray` containing a numeric array.

Grayscale transformation, returned as a numeric vector. The transformation `T` maps gray levels in the image `I` to gray levels in `J`.

If histogram equalization is performed using a GPU, then `T` is returned as a `gpuArray` containing a numeric vector.

Data Types: `double`

Transformed colormap, specified as an n-by-3 array. Each row specifies an RGB color value.

Data Types: `double`

## Algorithms

When you supply a desired histogram `hgram`, `histeq` chooses the grayscale transformation T to minimize

`$|{c}_{1}\left(T\left(k\right)\right)-{c}_{0}\left(k\right)|,$`

where c0 is the cumulative histogram of `A`, c1 is the cumulative sum of `hgram` for all intensities k. This minimization is subject to the constraints that T must be monotonic and c1(T(a)) cannot overshoot c0(a) by more than half the distance between the histogram counts at a. `histeq` uses the transformation b = T(a) to map the gray levels in `X` (or the colormap) to their new values.

If you do not specify `hgram`, then `histeq` creates a flat `hgram`,

`hgram = ones(1,n)*prod(size(A))/n;`

and then applies the previous algorithm.