Thursday, March 12, 2009

Histogram equalization

% histogram equalization
% Read a gray scale image and 1. compute teh cumulative probability
% function and plot it. 2. scale the cumulative probability function 0 - 1 to
% 0 - 256. 3. Map each histogram values H(s)to probability density value
% Pf(s). 4. compute the cumulative probability value and plot
clear all; close all; clc;
a = imread('cameraman.tif');
%a = imnoise(a,'salt & pepper',0.1);
[m n]=size(a);
figure,imshow(a);
figure,imhist(a);
a = double(a);
gmin = min(min(a));
gmax = max(max(a));
% Cumulative probability function Pf.
Pf = zeros(1,256);
for i=1:m
for j=1:n
f = a(i,j) +1;
for x = f:256
Pf(x) = Pf(x) +1;
end
end
end
Pf = Pf / (m*n);
figure,plot(Pf);
% histogram equalization
g1 = (gmax - gmin) * Pf + gmin;
figure,plot(g1);
B1 = zeros(m,n);
for i=1:m
for j=1:n
B1(i,j)=g1(a(i,j)+1);
end
end
figure,imshow(uint8(B1));
figure,imhist(uint8(B1));
% Cumulative probability function Pf
Pf = zeros(1,256);
for i=1:m
for j=1:n
f = round(B1(i,j))+1;
for x = f:256
Pf(x) = Pf(x) +1;
end
end
end
Pf = Pf / (m*n);
figure,plot(Pf);

No comments: