Main Content

imhistmatchn

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

Description

J = imhistmatchn(I,ref) transforms the N-D grayscale image I and returns an image whose histogram approximately matches the histogram of the reference image ref. Both I and ref must be grayscale images, but they do not need to have the same data type, size, or number of dimensions.

example

J = imhistmatchn(I,ref,nbins) uses nbins equally spaced bins within the appropriate range for the given image data type. The returned image has no more than nbins discrete levels.

If the data type of the image is:

  • single or double, the histogram range is [0, 1].

  • uint8, the histogram range is [0, 255].

  • uint16, the histogram range is [0, 65535].

  • int16, the histogram range is [-32768, 32767].

[J,hgram] = imhistmatchn(___) also returns the histogram of the reference image ref used for matching in hgram. hgram is a 1-by-nbins vector, where nbins is the number of histogram bins.

Examples

collapse all

Load an N-D grayscale image into the workspace. Also load a grayscale image to provide a reference histogram.

load mri D
load mristack

Display the original volume as slices.

figure
montage(D,'DisplayRange',[])
title('Original 3-D Image')

Figure contains an axes object. The hidden axes object with title Original 3-D Image contains an object of type image.

Reshape the reference as a stack of grayscale slices for display.

ref = reshape(mristack,[256,256,1,21]);

Display the reference volume as slices. To display correctly on the screen, the reference volume is downsized by a factor of 0.5 using imresize.

ref_downsized = imresize(ref,0.5);
figure
montage(ref_downsized,'DisplayRange',[])
title('Reference 3-D Image')

Figure contains an axes object. The hidden axes object with title Reference 3-D Image contains an object of type image.

Match the histogram of D to the histogram of the fullsize ref.

Dmatched = imhistmatchn(D,ref);

Display the output. Observe that the brightness levels of the output more closely match the reference image than the original image.

figure
montage(Dmatched,'DisplayRange',[])
title('Histogram Matched MRI')

Figure contains an axes object. The hidden axes object with title Histogram Matched MRI contains an object of type image.

Input Arguments

collapse all

Input image to be transformed, specified as an N-D grayscale image. The imhistmatchn function expects images of data type double and single to have values in the range [0, 1]. If I has values outside the range [0, 1], then you can rescale values to the expected range by using the rescale function.

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

Reference image whose histogram is the reference histogram, specified as a grayscale image. The reference image provides the equally spaced nbins bin reference histogram which output image J is trying to match.

The imhistmatchn function expects images of data type double and single to have values in the range [0, 1].

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

Number of equally spaced bins in reference histogram, specified as a positive integer. nbins also represents the upper limit of the number of discrete data levels present in output image J.

Data Types: double

Output Arguments

collapse all

Output image, returned as an N-D grayscale image. The output image is derived from image I whose histogram is an approximate match to the histogram of input image ref built with nbins equally spaced bins. Image J is of the same size and data type as input image I. Input argument nbins represents the upper limit of the number of discrete levels contained in image J.

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

Histogram counts derived from reference image ref, returned as a 1-by-nbins vector.

Data Types: double

Version History

Introduced in R2017a