Clear Filters
Clear Filters

Matching histogram of one image to another

30 views (last 30 days)
Big Ben
Big Ben on 29 Aug 2022
Edited: DGM on 18 Jul 2023
Hi. I am trying to edit an image A so that its histogram matches the histogram of image B. I found 2 possible ways to do so:
1)
result = imhistmatch(A,B)
2)
result = histeq(A, imhist(B))
However, I want to know the difference between the two, and what each one of these are doing.

Answers (1)

Garv Agarwal
Garv Agarwal on 20 Jun 2023
Hi Big Ben,
As per my understanding, you want to know the difference between the working of MATLAB functions imhistmatch and histeq in context of image histogram matching.
The goal of both these functions is to modify the pixel intensities in the input image so that their resulting histogram matches that of the reference image. The difference lies in how each function achieves this transformation.
The "imhistmatch" function modifies the pixel intensities of the input image directly to match the histogram of the reference image. This function calculates the cumulative distribution function (CDF) of both the input and reference images and then maps the pixel values of the input image to the pixel values of the reference image by matching their corresponding CDFs. The resulting transformed image will have a histogram that matches the reference image.
The "histeq" function on the other hand calculates the histogram of the input image and flattens it such that its histogram is more uniformly distributed. This is achieved by applying a transformation function based on the CDF of the input image. The function then maps the pixel values of the input image to new pixel values based on the transformation function. In order to match the histogram of the reference image, the transformation function is based on the CDF of the reference image instead of that of the input image.
The following modified example depicts the difference in results -
% run openExample('images/MatchHistogramOfAerialImagesExample') to open the
% original example
A = imread("westconcordaerial.png");
Ref = imread("westconcordorthophoto.png");
B = imhistmatch(A,Ref);
C = histeq(A, imhist(Ref));
imshow(A)
title("RGB Image with Color Cast")
imshow(Ref)
title("Reference Grayscale Image")
imshow(B)
title("Histogram Matched RGB Image using imhistmatch")
imshow(C)
title("Histogram Matched RGB Image using histeq")
In summary, if you want to improve the contrast of an image, you should use "histeq". If you want to match the histogram of an input image to a reference image, you should use "imhistmatch".
For more details, you can refer to the following documentations-
  1 Comment
DGM
DGM on 18 Jul 2023
Edited: DGM on 18 Jul 2023
For default settings, imhistmatch() internally uses histeq() more or less exactly as the example describes. The only difference is that for RGB inputs, it operates on each channel independently, which is something that histeq() does not do. The output is the same if you bother to apply histeq() appropriately.
A = imread('westconcordaerial.png');
Ref = imread('westconcordorthophoto.png');
% use the convenience tool
B = imhistmatch(A,Ref);
% reinvent the wheel
C = A;
for c = 1:size(A,3)
thishgram = imhist(Ref(:,:,min(c,size(Ref,3))));
C(:,:,c) = histeq(A(:,:,c), thishgram);
end
montage({B C}) % they're the same
If you have imhistmatch() there really isn't any need to reinvent the wheel.

Sign in to comment.

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!