You are here : matlabImage Processingimhistmatch

imhistmatch() - Image Processing

exampleB = imhistmatch(A,ref) transforms
the grayscale or truecolor image A returning
output image B whose histogram 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.
exampleB = 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-by-N (when ref is grayscale) or a 3-by-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.


Syntax

B = imhistmatch(A,ref) exampleB = imhistmatch(A,ref,N) example[B,hgram]
= imhistmatch(___) example


Example

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.
Load both images.A = imread('concordaerial.png');
Ref = imread('concordorthophoto.png');Get the size of image A.size(A)
ans =

        2036        3060           3Get the size of the reference image Ref. size(Ref)
ans =

       2215        2956Note 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 imageImage 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 = 0Since 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 imageDarken 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.


Output / Return Value


Limitations


Alternatives / See Also


Reference