Code covered by the BSD License  

Highlights from
Histogram Equalization

image thumbnail

Histogram Equalization

by

 

this file implements histogram Equalization on an image

histeq.m
%this program perform histogram equalization on an image
clc;clear all;close all;

org_img=imread('cameraman.tif');
pixel_num=size(org_img,1)*size(org_img,2);
figure
subplot(2,2,1)
imshow(org_img)
title('Original Image');
subplot(2,2,3)

imhist(org_img);
hist_img = uint8(zeros(size(org_img,1),size(org_img,2)));
freq=zeros(256,1);
probf=zeros(256,1);
probc=zeros(256,1);
cum=zeros(256,1);
output=zeros(256,1);

%freq counts the occurrence of each pixel value.

%The probability of each occurrence is calculated by probf.


for i=1:size(org_img,1)
    for j=1:size(org_img,2)
        value=org_img(i,j);
        freq(value+1)=freq(value+1)+1;
        probf(value+1)=freq(value+1)/pixel_num;
    end
end
sum=0;
no_bins=255;
%The cumulative distribution probability is calculated. 

for i=1:size(probf)
   sum=sum+freq(i);
   cum(i)=sum;
   probc(i)=cum(i)/pixel_num;
   output(i)=round(probc(i)*no_bins);

end

for i=1:size(org_img,1)
    for j=1:size(org_img,2)
            hist_img(i,j)=output(org_img(i,j)+1);
    end
end

subplot(2,2,2)
imshow(hist_img);
title('Histogram equalization');
subplot(2,2,4)
imhist(hist_img);

Contact us