# imhist

Histogram of image data

## Syntax

• `imhist(I)` example
• `imhist(I,n)`
• `imhist(X,map)`
• ```[counts,binLocations] = imhist(I)```
• ```[counts,binLocations] = imhist(gpuarrayI,___)``` example

## Description

example

````imhist(I)` calculates the histogram for the intensity image `I` and displays a plot of the histogram. The number of bins in the histogram is determined by the image type.This function supports code generation (see Tips).```
````imhist(I,n)` calculates the histogram, where `n` specifies the number of bins used in the histogram. `n` also specifies the length of the colorbar displayed at the bottom of the histogram plot.```
````imhist(X,map)` displays a histogram for the indexed image `X`. This histogram shows the distribution of pixel values above a colorbar of the colormap `map`. The colormap must be at least as long as the largest index in `X`. The histogram has one bin for each entry in the colormap.```
``````[counts,binLocations] = imhist(I)``` returns the histogram counts in `counts` and the bin locations in `binLocations` so that `stem(binLocations,counts)` shows the histogram. For indexed images, `imhist` returns the histogram counts for each colormap entry. The length of `counts` is the same as the length of the colormap.```

example

``````[counts,binLocations] = imhist(gpuarrayI,___)``` performs the histogram calculation on a GPU. The input image and the return values are gpuArrays. This syntax requires the Parallel Computing Toolbox™. When the input image is a gpuArray, `imhist` does not automatically display the histogram. To display the histogram, use `stem(binLocations,counts)`.```

## Examples

collapse all

### Calculate Histogram

```I = imread('pout.tif'); imhist(I) ```

### Calculate Histogram on a GPU

Create array of class `uint16`.

`I = gpuArray(imread('pout.tif'));`

Calculate histogram. Because `imhist` does not automatically display the plot of the histogram when run on a GPU, this example uses `stem` to plot the histogram.

```[counts,x] = imhist(I); stem(x,counts); ```

## Input Arguments

collapse all

### `I` — Input intensity imagenumeric array

Input intensity image, specified as a numeric array.

Example: `I = imread('cameraman.tif');`

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

### `n` — Number of bins256 (for grayscale images) (default) | numeric scalar

Number of bins, specified as a numeric scalar. If `I` is a grayscale image, `imhist` uses a default value of 256 bins. If `I` is a binary image, `imhist` uses two bins.

Example: `[counts,x] = imhist(I,50);`

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

### `X` — Input indexed imagenumeric array

Input indexed image, specified as a numeric array.

Example: `[X,map] = imread('trees.tif');`

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

### `map` — Colormap associated with indexed imagep-by-3 array

Colormap associated with indexed image, specified as a p-by-3 array.

Example: `[X,map] = imread('trees.tif');`

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

### `gpuarrayI` — Input imagegpuArray

Input image, specified as a gpuArray.

Example: `gpuarrayI = gpuArray(imread('cameraman.tif'));`

## Output Arguments

collapse all

### `counts` — histogram countsnumeric array

Histogram counts, returned as a numeric array.

### `binLocations` — Bin locationsnumeric array

Bin locations, returned as a numeric array.

collapse all

### Tips

• 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.

When generating code, note:

• If the first input is a binary image, then `n` must be a scalar constant of value 2 at compile time.

• Nonprogrammatic syntaxes are not supported. For example, the syntax `imhist(I)`, where `imhist` displays the histogram, is not supported.

• For intensity images, the `n` bins of the histogram are each half-open intervals of width A/(n−1). In particular, for intensity images that are not `int16`, the pth bin is the half-open interval

$\frac{A\left(p-1.5\right)}{\left(n-1\right)}\le x<\frac{A\left(p-0.5\right)}{\left(n-1\right)},$

where x is the intensity value. For `int16` intensity images, the pth bin is the half-open interval

$\frac{A\left(p-1.5\right)}{\left(n-1\right)}-32768\le x<\frac{A\left(p-0.5\right)}{\left(n-1\right)}-32768,$

where x is the intensity value. The scale factor A depends on the image class. If the intensity image is `double` or `single`, A is 1. If the intensity image is `uint8`, A is 255. If the intensity image is `uint16` or `int16`, A is 65535.