Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

imhistmatch

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

Syntax

B = imhistmatch(A,ref)
B = imhistmatch(A,ref,nbins)
[B,hgram] = imhistmatch(___)

Description

example

B = imhistmatch(A,ref) transforms the 2-D grayscale or truecolor image A returning output image B whose histogram approximately matches the histogram of the reference image ref.

  • 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,nbins) uses nbins equally spaced bins within the appropriate range for the given image data type. The returned image B has no more than nbins discrete levels.

  • 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-by-nbins (when ref is grayscale) or a 3-by-nbins (when ref is truecolor) matrix, where nbins is the number of histogram bins. Each row in hgram stores the histogram of a single color channel of ref.

Examples

collapse all

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.

Load an RGB image and a reference grayscale image.

A = imread('concordaerial.png');
Ref = imread('concordorthophoto.png');

Get the size of A.

size(A)
ans = 

        2036        3060           3

Get the size of Ref.

size(Ref)
ans = 

        2215        2956

Note that image A and 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. 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 the same as the number of bins in the histogram built from grayscale image Ref. In this example, the histogram of Ref and B have the default number of bins, 64.

B = imhistmatch(A,Ref);

Display the RGB image A, the reference image Ref, and the histogram matched RGB image B. The images are resized before display.

figure
imshow(imresize(A, 0.25))
title('RGB Image with Color Cast')

figure
imshow(imresize(Ref, 0.25))
title('Reference Grayscale Image')

figure
imshow(imresize(B, 0.25))
title('Histogram Matched RGB Image')

In this example, you will see the effect on output image B of varying the number of equally spaced bins in the target 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 nbins: 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 nbins equally spaced bins. As a result, nbins 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 nbins=[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 nbins=[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 nbins=[64 128 256] are 57 for output image B64, 101 for output image B128, and 134 for output image B256. Note that as nbins increases, the number of levels in each RGB channel of output image B also increases.

This example shows how to perform histogram matching with different numbers of bins.

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: ',num2str(length(LevelsK)),' distinct levels']);
image K: 448 distinct levels
disp(['max level = ' num2str( max(LevelsK) )]);
max level = 473
disp(['min level = ' num2str( min(LevelsK) )]);
min level = 0

All 448 discrete values are at low code values, which causes the image to look dark. To rectify this, scale the image data to span the entire 16-bit range of [0, 65535].

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 Ref to create an image A that can be used in the histogram matching operation.

%Build concave bow-shaped curve for darkening |Ref|.
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 a lookup table (LUT) to darken the image.
A = intlut(Ref,Lut16bit);

View the reference image Ref and the darkened image A. Note that they have the same number of discrete code values, but differ in overall brightness.

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

Generate histogram-matched output images using histograms with different number of bins. First use the default number of bins, 64. Then use the number of values present in image A, 448 bins.

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

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: 64 bins')
subplot(1,2,2)
imshow(Ref)
title(['B16bitUniq: ',num2str(N),' bins'])

Input Arguments

collapse all

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

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

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

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

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

Data Types: double

Output Arguments

collapse all

Output image, returned as a 2-D 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 nbins equally spaced bins. Image B is of the same size and data type as input image A. Input argument nbins represents the upper limit of the number of discrete levels contained in image B.

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

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

Data Types: double

Algorithms

The objective of imhistmatch is to transform image A such that the histogram of image B matches the histogram derived from image ref. It consists of nbins equally spaced bins which span the full range of the image data type. A consequence of matching histograms in this way is that nbins 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 nbins 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 nbins represents a trade-off between more output levels (larger values of nbins) while minimizing peak fluctuations in the histogram (smaller values of nbins).

Introduced in R2012b

Was this topic helpful?