Histogram Matching/Specification in MATLAB: A Practical Tutorial

NeuralNinja
3 min readMar 25, 2023

--

Introduction:

Histogram matching or specification is a method to transform the intensity distribution of an image into a desired distribution. The main aim is to improve the visual quality of an image by changing its contrast, brightness, or color. The technique is useful in applications like medical imaging, remote sensing, and computer vision.

In this blog post, we will discuss the code for histogram matching/specification without using built-in functions. The code is written in MATLAB and consists of three sections: histogram calculation, histogram equalization, and histogram matching. We will explain each section in detail and provide comments wherever necessary.

Code Explanation:

1. Histogram Calculation:

The probability distribution function (pdf) and cumulative distribution function (cdf) of the images are determined using the target and reference images’ histograms, which are calculated in the first portion of the code. Whereas the cdf provides the likelihood that an intensity value will be less than or equal to a specific value, the pdf provides the likelihood that each intensity value in the image will occur.

The histogram bin for each pixel in the image is increased by the code using a loop that iterates through the image’s pixels. By multiplying the number of rows and columns, the total number of pixels in the image is determined. The likelihood of occurrence of each intensity value is also calculated by the code by dividing the histogram bin by the total number of pixels.

2. Histogram Equalization:

The second piece of the code equalizes the histograms of each image. Histogram equalization is a technique for transforming an image’s intensity dispersion into a uniform distribution. The procedure is used to enhance an image’s contrast and brightness.

The cdf of the image is calculated by adding the probability of each intensity value to the preceding probability. To obtain the mapping function, the code scales the cdf to the maximum intensity value of 255 and rounds the result to the closest integer. To obtain the equalized image, the mapping function is applied to each pixel of the image.

3. Histogram Matching:

The final part of the code compares the histograms of the target and reference images. Histogram matching is a technique for transforming image’s intensity distribution into a desired distribution described by the reference image. The procedure is used to change an image’s color balance and tone.

The mapping function between the two images is initially calculated by comparing their cdfs. The algorithm identifies the appropriate intensity value in the reference image with a higher cdf value for each intensity value in the target image. To obtain the matched image, the mapping function is applied to each pixel of the target image.

Lastly, the code uses MATLAB’s subplot function to display the three images and associated histograms.

Conclusion:

We reviewed the code for histogram matching/specification without utilizing built-in functions in this blog article. The code is divided into three sections: histogram computation, equalization, and matching. We have thoroughly explained each aspect and included comments where appropriate. Histogram matching is a helpful approach for enhancing an image’s visual quality by altering its contrast, brightness, or hue. The approach is frequently utilized in medical imaging, remote sensing, and computer vision applications.

%Histogram of img1
target_img=imread('');
[x1, y1]=size(target_img);
himg1=zeros(1,256);
prob1=zeros(1,256);
t=x1*y1;
I=255;
for r=1:x1
for c=1:y1
v=target_img(r,c);
himg1(v+1)=himg1(v+1)+1;
prob1(v+1)=himg1(v+1)/t;
end
end
cprob1=zeros(1,length(himg1));
floor1=zeros(1,256);
for r=2:length(himg1)
cprob1(r)=prob1(r)+cprob1(r-1);
floor1(r)=round(cprob1(r)*I);
end

%Histogram of img2
refrence_img=imread('');
[x2, y2]=size(refrence_img);
himg2=zeros(1,256);
prob2=zeros(1,256);
val=x2*y2;
for a=1:x2
for b=1:y2
z=refrence_img(a,b);
himg2(z+1)=himg2(z+1)+1;
prob2(z+1)=himg2(z+1)/val;
end
end
cprob2=zeros(1,length(himg2));
floor2=zeros(1,256);
for a=2:length(himg2)
cprob2(a)=prob2(a)+cprob2(a-1);
floor2(a)=round(cprob2(a)*I);
end

%Histogram Equalization
hequimg1=zeros(x1,y1,'uint8');
for r=1:x1
for c=1:y1
hequimg1(r,c)=floor1(target_img(r,c)+1);
end
end
hequimg2=zeros(x2,y2,'uint8');
for a=1:x2
for b=1:y2
hequimg2(a,b)=floor2(refrence_img(a,b)+1);
end
end

%Histogram Matching
matching = zeros(1,256);
for n=1:size(target_img)
for m=1:size(refrence_img)
if cprob2(m)>cprob1(n)
macthing(n)=m;
break
end
end
end

%Generating the final output of the given image
matched_img = zeros(x1,y1,'uint8');
for i=1:x1
for j=1:y1
h=(target_img(i,j)+1);
matched_img(i,j)=macthing(h);
end
end

subplot(2,3,1); imshow(uint8(target_img)); title('Target image');
subplot(2,3,2); imshow(uint8(refrence_img)); title('Reference image');
subplot(2,3,3); imshow(uint8(matched_img)); title('Modified image');
subplot(2,3,4); imhist(target_img); title('Histogram of Target Imgae');
subplot(2,3,5); imhist(refrence_img); title('Histogram of Reference Imgae');
subplot(2,3,6); imhist(matched_img); title('Histogram of Modified Imgae');

--

--

No responses yet