Main Content

imhistmatch

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

Description

example

J= imhistmatch(I,ref)transforms the 2-D grayscale or truecolor imageIreturning output imageJwhose histogram approximately matches the histogram of the reference imageref.

  • If bothIandrefare truecolor images, thenimhistmatchmatches each color channel ofIindependently to the corresponding color channel ofref.

  • IfIis a truecolor RGB image andrefis a grayscale image, thenimhistmatchmatches each channel ofIagainst the single histogram derived fromref.

  • IfIis a grayscale image, thenrefmust also be a grayscale image.

ImagesIandrefcan be any of the permissible data types and need not be equal in size.

example

J= imhistmatch(I,ref,nbins)usesnbinsequally spaced bins within the appropriate range for the given image data type. The returned imageJhas no more thannbinsdiscrete levels.

  • If the data type of the image is eithersingleordoublethen the histogram range is [0, 1].

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

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

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

example

J= imhistmatch(___,Name,Value)uses name-value pairs to change the behavior of the histogram matching algorithm.

example

[J,hgram] = imhistmatch(___)returns the histogram of the reference imagerefused for matching inhgram.hgramis a 1-by-nbins(whenrefis grayscale) or a 3-by-nbins(whenrefis truecolor) matrix, wherenbinsis the number of histogram bins. Each row inhgramstores the histogram of a single color channel ofref.

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 imagesAandRefcan 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 ofA.

size(A)
ans =1×32036 3060 3

Get the size ofRef.

size(Ref)
ans =1×22215 2956

Note that imageAandRefare different in size and type. ImageAis a truecolor RGB image, while imageRefis a grayscale image. Both images are of data typeuint8.

Generate the histogram matched output image. The example matches each channel ofAagainst the single histogram ofRef. Output imageBtakes on the characteristics of imageA- it is an RGB image whose size and data type is the same as imageA. The number of distinct levels present in each RGB channel of imageBis the same as the number of bins in the histogram built from grayscale imageRef. In this example, the histogram ofRefandBhave the default number of bins, 64.

B = imhistmatch(A,Ref);

Display the RGB imageAthe reference imageRef, and the histogram matched RGB imageB. The images are resized before display.

imshow(A) title('RGB Image with Color Cast')

Figure contains an axes object. The axes object with title RGB Image with Color Cast contains an object of type image.

imshow(Ref) title('Reference Grayscale Image')

Figure contains an axes object. The axes object with title Reference Grayscale Image contains an object of type image.

imshow(B) title('Histogram Matched RGB Image')

Figure contains an axes object. The axes object with title Histogram Matched RGB Image contains an object of type image.

Read a color image and a reference image. To demonstrate the polynomial method, assign the reference image to be the darker of the two images.

I = imread('office_4.jpg');ref = imread('office_2.jpg');montage({I,ref}) title('Input Image (Left) vs Reference Image (Right)');

Figure contains an axes object. The axes object with title Input Image (Left) vs Reference Image (Right) contains an object of type image.

Use the polynomial method to adjust the intensity of imageIso that it matches the histogram of reference imageref. For comparison, also adjust the intensity of imageIusing the uniform method.

J = imhistmatch(I,ref,'method','polynomial');K = imhistmatch(I,ref,'method','uniform');montage({J,K}) title('Histogram-Matched Image Using Polynomial Method (Left) vs Uniform Method (Right)');

Figure contains an axes object. The axes object with title Histogram-Matched Image Using Polynomial Method (Left) vs Uniform Method (Right) contains an object of type image.

The histogram-matched image using the uniform method introduces false colors in the sky and road. The histogram-matched image using the polynomial method does not exhibit this artifact.

This example shows how you can vary the number of bins in the target histogram to improve histogram equalization.

加载两个图像的数据类型uint8into the workspace. The images were taken with a digital camera and represent two different exposures of the same scene.Ais an underexposed image and appears dark.refis a reference image with good exposure and brightness.

A = imread('office_2.jpg');ref = imread('office_4.jpg');

Display the images in a montage.

montage({A,ref}) title('Dark Image (Left) and Reference Image (Right)')

Figure contains an axes object. The axes object with title Dark Image (Left) and Reference Image (Right) contains an object of type image.

Display the histogram of each color channel using 256 bins. You can use the helper function,displayHistogramChannelsthat is included with the example.

displayHistogramChannels(A,ref)

Figure contains 6 axes objects. Axes object 1 with title Histograms of Input Image contains an object of type histogram. Axes object 2 with title Histograms of Reference Image contains an object of type histogram. Axes object 3 contains an object of type histogram. Axes object 4 contains an object of type histogram. Axes object 5 contains an object of type histogram. Axes object 6 contains an object of type histogram.

ImageA, being the darker image, has most of its pixels in the lower bins. The reference image, ref, fully populates all 256 bins values in all three RGB channels.

Count the number of unique 8-bit level values for each color channel of the dark and reference image. You can use the helper function,countUniqueValuesthat is included with the example.

numVals = countUniqueValues(A,ref); table(numVals(:,1),numVals(:,2),numVals(:,3),...'VariableNames',["Red""Green""Blue"],...'RowNames',["A""ref"])
ans=2×3 table红绿蓝___ _____ ____ A 205 193 224 ref 256 256 256

Equalize the histogram of the dark image using three different values ofnbins: 64, 128 and 256. 64 is the default number of bins and 256 is the maximum number of bins foruint8pixel data.

[B64,hgram64] = imhistmatch(A,ref,64); [B128,hgram128] = imhistmatch(A,ref,128); [B256,hgram256] = imhistmatch(A,ref,256); figure montage({B64,B128,B256},'Size',[1 3]) title('Output Image B64 | Output Image B128 | Output Image B256')

Figure contains an axes object. The axes object with title Output Image B64 | Output Image B128 | Output Image B256 contains an object of type image.

Display the histogram of each color channel using 256 bins. You can use the helper function,displayThreeHistogramChannelsthat is included with the example.

displayThreeHistogramChannels(B64,B128,B256)

Figure contains 9 axes objects. Axes object 1 with title Histograms of Output Image B64 contains an object of type histogram. Axes object 2 with title Histograms of Output Image B128 contains an object of type histogram. Axes object 3 with title Histograms of Output Image B256 contains an object of type histogram. Axes object 4 contains an object of type histogram. Axes object 5 contains an object of type histogram. Axes object 6 contains an object of type histogram. Axes object 7 contains an object of type histogram. Axes object 8 contains an object of type histogram. Axes object 9 contains an object of type histogram.

Count the number of unique 8-bit level values for each color channel of the three histogram equalized images. Asnbinsincreases, the number of levels in each RGB channel of output imageBalso increases.

numVals = countUniqueValues(B64,B128,B256); table(numVals(:,1),numVals(:,2),numVals(:,3),...'VariableNames',["Red""Green""Blue"],...'RowNames',["B64""B128""B256"])
ans=3×3 table红绿蓝___ _____ ____ B64 57 60 58 B128 101 104 104 B256 134 135 136

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 imageLevelsK = unique(K(:));% determine number of unique code valuesdisp(['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 doublekmult = 65535/(max(max(Kdouble(:))));% full range multiplierRef = uint16 (kmult * Kdouble);% full range 16-bit reference image

Darken the reference imageRefto create an imageAthat 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 imageRefand the darkened imageA. 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');

Figure contains 2 axes objects. Axes object 1 with title Ref: Reference Image contains an object of type image. Axes object 2 with title A: Darkened Image contains an object of type 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 imageA, 448 bins.

B16bit64 = imhistmatch(A(:,:,1),Ref(:,:,1));% default: 64 binsN = 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'])

Figure contains 2 axes objects. Axes object 1 with title B16bit64: 64 bins contains an object of type image. Axes object 2 with title B16bitUniq: 448 bins contains an object of type image.

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 spacednbinsbin reference histogram which output imageJis 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 imageref,nbinsalso represents the upper limit of the number of discrete data levels present in output imageJ.

Data Types:double

Name-Value Arguments

Specify optional comma-separated pairs ofName,Valuearguments.Nameis the argument name andValueis the corresponding value.Namemust appear inside quotes. You can specify several name and value pair arguments in any order asName1,Value1,...,NameN,ValueN.

Example:imhistmatch(I,ref,'Method','polynomial')matches the histogram of imageIto that of reference imagerefusing the polynomial mapping technique.

用于地图的直方图映射技术refto imageI, specified as the comma-separated pair consisting of'Method'and one of these values:

  • 'uniform'— Use a histogram-based intensity function and histogram equalization.

  • 'polynomial'— Calculate a cubic Hermite polynomial mapping function from the cumulative histograms of the source and reference images. The polynomial method is useful when the reference image is darker than the input image. In this situation, the polynomial method gives a smoother color transition than the uniform method.

Output Arguments

collapse all

Output image, returned as a 2-D truecolor or grayscale image. The output image is derived from imageIwhose histogram is an approximate match to the histogram of input imagerefbuilt withnbinsequally-spaced bins. ImageJis of the same size and data type as input imageI. Input argumentnbinsrepresents the upper limit of the number of discrete levels contained in imageJ.

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

Histogram counts derived from reference imageref, specified as a vector or matrix. Whenrefis a truecolor image,hgramis a 3-by-nbinsmatrix. Whenrefis a grayscale image,hgramis a 1-by-nbinsvector.

Data Types:double

Algorithms

The objective ofimhistmatchis to transform imageIsuch that the histogram of imageJmatches the histogram derived from imageref. It consists ofnbinsequally spaced bins which span the full range of the image data type. A consequence of matching histograms in this way is thatnbinsalso represents the upper limit of the number of discrete data levels present in imageJ.

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

An optimal value fornbins代表着更多的产出水平之间的权衡(larger values ofnbins) while minimizing peak fluctuations in the histogram (smaller values ofnbins).

Introduced in R2012b