Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

# imhistmatch

Adjust histogram of image to match N-bin histogram of reference image

## Description

example

B = imhistmatch(A,ref) transforms the grayscale or truecolor image A so that the histogram of the output image B approximately matches the histogram of the reference image ref, when the same number of bins are used for both histograms.

• If both A and ref are truecolor RGB images, imhistmatch matches each color channel of A independently to the corresponding color channel of ref.

• If A is a truecolor RGB image and ref is a grayscale image, imhistmatch matches each channel of A against the single histogram derived from ref.

• If A is a grayscale image, ref must also be a grayscale image.

Images A and ref can be any of the permissible data types and need not be equal in size.

example

B = imhistmatch(A,ref,N) uses N equally spaced bins within the appropriate range for the given image data type. The returned image B has no more than N discrete levels. The default value for N is 64.

• If the data type of the image is either single or double, the histogram range is [0, 1].

• If the data type of the image is uint8, the histogram range is [0, 255]

• If the data type of the image is uint16, the histogram range is [0, 65535]

• If the data type of the image is int16, the histogram range is [-32768, 32767]

example

[B,hgram] = imhistmatch(___) returns the histogram of the reference image ref

used for matching in HGRAM. HGRAM is a 1 x N (when REF is grayscale) or a 3 x N (when REF is truecolor) matrix, where N is the number of histogram bins. Each row in HGRAM stores the histogram of a single color channel of REF.

## Examples

expand all

### Histogram matching of aerial images

These aerial images, taken at different times, represent overlapping views of the same terrain in Concord, Massachusetts. This example demonstrates that input images A and Ref can be of different sizes and image types.

```A = imread('concordaerial.png');

Get the size of image A.

```size(A)
```
```ans =

2036        3060           3```

Get the size of the reference image Ref.

```size(Ref)
```
```ans =

2215        2956```

Note that image A and image Ref are different in size and type. Image A is a truecolor RGB image, while image Ref is a grayscale image. Both images are of data type uint8.

Generate the histogram matched output image. The example matches each channel of A against the single histogram of ref built with 64 (default value) equally spaced bins. Output image B takes on the characteristics of image A—it is an RGB image whose size and data type is the same as image A. The number of distinct levels present in each RGB channel of image B is determined by the number of bins in the single aim histogram built from grayscale image Ref which in this case is 64.

```B = imhistmatch(A,Ref);
```

### Multiple N values applied to RGB Images

In this example, you will see the effect on output image B of varying N, the number of equally spaced bins in the aim histogram of image Ref, from its default value 64 to the maximum value of 256 for uint8 pixel data.

The following images were taken with a digital camera and represent two different exposures of the same scene.

```    A   = imread('office_2.jpg');   % Dark Image
Ref = imread('office_4.jpg');   % Reference image```

Image A, being the darker image, has a preponderance of its pixels in the lower bins. The reference image, Ref, is a properly exposed image and fully populates all of the available bins values in all three RGB channels: as shown in the table below, all three channels have 256 unique levels for 8–bit pixel values.

The unique 8-bit level values for the red channel is 205 for A and 256 for ref. The unique 8-bit level values for the green channel is 193 for A and 256 for ref. The unique 8-bit level values for the blue channel is 224 for A and 256 for ref.

The example generates the output image B using three different values of N: 64, 128 and 256. The objective of function imhistmatch is to transform image A such that the histogram of output image B is a match to the histogram of Ref built with N equally spaced bins. As a result, N represents the upper limit of the number of discrete data levels present in image B.

```[B64,  hgram] = imhistmatch(A, Ref,  64);
[B128, hgram] = imhistmatch(A, Ref, 128);
[B256, hgram] = imhistmatch(A, Ref, 256);```

The unique 8-bit level values for the red channel for N=[64 128 256] are 57 for output image B64, 101 for output image B128, and 134 for output image B256. The unique 8-bit level values for the green channel for N=[64 128 256] are 57 for output image B64, 101 for output image B128, and 134 for output image B256. The unique 8-bit level values for the blue channel for N=[64 128 256] are 57 for output image B64, 101 for output image B128, and 134 for output image B256. Note that as N increases, the number of levels in each RGB channel of output image B also increases.

### Histogram matching a 16-bit grayscale MRI image

Load a 16-bit grayscale MRI image, darken it for use in this example, and then perform histogram matching at two values of N.

Load a 16-bit DICOM image of a knee imaged via MRI.

```K = dicomread('knee1.dcm');   % read in original 16-bit image
LevelsK = unique(K(:));       % determine number of unique code values
disp(['image K: # levels: ' num2str(length(LevelsK))]);
disp(['max level = ' num2str( max(LevelsK) )]);
disp(['min level = ' num2str( min(LevelsK) )]);```
```image K: # levels = 448
max level = 473
min level = 0```

Since it appears that all 448 discrete values are at low code values (darker), scale the image data to span the entire 16-bit range of [0 65535]

```% Scale it to full 16-bit range
Kdouble = double(K);                  % cast uint16 to double
kmult = 65535/(max(max(Kdouble(:)))); % full range multiplier
Ref = uint16(kmult*Kdouble);   % full range 16-bit reference image```

Darken the reference image to create an image (A) that can be used in the histogram matching operation.

```% build concave bow-shaped curve for darkening Reference image
ramp = [0:65535]/65535;
ppconcave = spline([0 .1 .50  .72 .87 1],[0 .025 .25 .5 .75 1]);
Ybuf = ppval( ppconcave, ramp);
Lut16bit = uint16( round( 65535*Ybuf ) );

% pass image Ref through LUT to darken image
A = intlut(Ref,Lut16bit);```

View the two images and note that they have the same number of discrete code values, but differ in overall brightness.

```subplot(1,2,1), imshow(A)
title('A: Darkened Image');
subplot(1,2,2), imshow(Ref)
title('Ref: Reference Image')```

Generate histogram-matched output images at two values of N. The first is the default value of 64, the second is the number of values present in image A of 448.

```B16bit64 = imhistmatch(A(:,:,1),Ref(:,:,1));  % default # bins: N = 64

N = length(LevelsK);      % number of unique 16-bit code values in image A
B16bitUniq = imhistmatch(A(:,:,1),Ref(:,:,1),N);```

View the results of the two histogram matching operations.

```figure;
subplot(1,2,1), imshow(B16bit64)
title('B16bit64: N = 64');
subplot(1,2,2), imshow(Ref)
title('B16bitUniq: N = 448')```

The following figure shows the 16 bit histograms of all four images; the y-axis scaling is the same for plots.

The unique 16-bit code values in output B images are Levels=63 and N=64, for B16bit64 and Levels=222 and N=448 for B16bitUniq.N also represents the upper limit of discrete levels in the output image which is shown above; the number of levels increases from 63 to 222 when the number of histogram bins increases from 64 to 448. But note, in the above histogram plots, there are rapid fluctuations in adjacent histogram bins for the B image containing 222 levels, especially in the upper portion of the histogram range. By comparison, the 63 level B histogram has a relatively smooth and continuous progression of peaks in this region.

## Input Arguments

expand all

### A — Input imagetruecolor image | grayscale image

Input image to be transformed, specified as a truecolor or grayscale image. The returned image will take the data type class of the input image.

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

### ref — Reference image whose histogram is the reference histogramtruecolor image | grayscale image

Reference image whose histogram is the reference histogram, specified as truecolor or grayscale image. The reference image provides the equally spaced N bin reference histogram which output image B is trying to match.

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

### N — Number of equally spaced bins in reference histogram64 (default) | scalar

Number of equally spaced bins in reference histogram, specified as a scalar value. In addition to specifying the number of equally spaced bins in the histogram for image ref, N also represents the upper limit of the number of discrete data levels present in output image B.

Data Types: double

## Output Arguments

expand all

### B — Output image truecolor RGB image | grayscale image

Output image, returned as a truecolor or grayscale image. The output image is derived from image A whose histogram is an approximate match to the histogram of input image Ref built with N equally spaced bins. Image B is of the same size and data type as input image A. Input argument N represents the upper limit of the number of discrete levels contained in image B.

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

### hgram — Histogram counts derived from reference image Refvector | matrix

Histogram counts derived from reference image Ref, specified as a vector or matrix. When ref is a truecolor image, hgram is a 3xN matrix. When ref is a grayscale image, hgram is a 1xN vector.

Data Types: double

expand all

### Algorithms

The objective of imhistmatch is to transform image A such the the histogram of image B matches the histogram derived from image Ref. It consists of N equally spaced bins which span the full range of the image data type. A consequence of matching histograms in this way is that N also represents the upper limit of the number of discrete data levels present in image B.

An important behavioral aspect of this algorithm to note is that as N increases in value, the degree of rapid fluctuations between adjacent populated peaks in the histogram of image B tends to increase. This can be seen in the following histogram plots taken from the 16–bit grayscale MRI example.

An optimal value for N represents a trade-off between more output levels (larger values of N) while minimizing peak fluctuations in the histogram (smaller values of N).