Accelerating the pace of engineering and science

# imhist

Histogram of image data

## Syntax

imhist(I)
imhist(I,n)
imhist(X,map)
[counts,x] = imhist(___)
[___] = imhist(gpuarrayA,___)

## Description

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.

• If I is a grayscale image, imhist uses a default value of 256 bins.

• If I is a binary image, imhist uses two bins.

imhist(I,n) displays a histogram for the intensity image I, 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. If I is a binary image, n can only have the value 2.

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,x] = imhist(___) returns the histogram counts in counts and the bin locations in x so that stem(x,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.

[___] = imhist(gpuarrayA,___) 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(x,counts).

 Note:   The maximum value on the y-axis may be automatically reduced, so outlier spikes do not dominate. To show the full range of y-axis values, call imhist with the following syntax:`[counts,x] = imhist(...)`Then call stem:`stem(x,counts)`

## Code Generation

imhist supports the generation of efficient, production-quality C/C++ code from MATLAB. When generating code, the optional second input argument, n, must be a compile-time constant. In addition, nonprogrammatic syntaxes are not supported. For example, the syntax imhist(I), where imhist displays the histogram, is not supported. Generated code for this function uses a precompiled platform-specific shared library. To see a complete list of toolbox functions that support code generation, see List of Supported Functions with Usage Notes.

## Class Support

An input intensity image can be of class uint8, int8, uint16, int16, uint32, int32, single, double, or logical. An input indexed image can be of class uint8, uint16, single, double, or logical.

An input gpuArray intensity image can be of class uint8, int8, uint16, int16, uint32, int32, single, double, or logical. An input indexed image can be of class uint8, uint16, single, double, or logical.

## Examples

### Calculate Histogram

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

### Calculate Histogram on a GPU

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

```I = gpuArray(imread('pout.tif'));
[counts,x] = imhist(I);
stem(x,counts);```

expand all

### Tips

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. A is 1 if the intensity image is double or single, A is 255 if the intensity image is uint8, and A is 65535 if the intensity image is uint16 or int16.