% This program is for a simple image enhancemjust method. Run this program.
% It will show the original image and enhanced image. It will also show the
% histogram of oroginal and enhanced image.
clear all, %close all

% a=imread('pen.png');
a=imread('lena512.bmp');
x0=a;

L=256;

[m,n]=size(x0); 
len=m*n;
mn=round(mean(mean(x0)));
x=reshape(x0,len,1);
x3=find(x0<=mn);      %    mn=110
x4=find(x0>mn);
xpdf=hist(x,[0:L-1]); % pdf, 1 x L
xpdf1=xpdf(1:mn+1);
Pl=xpdf1/length(x3);
cL=cumsum(Pl);
xpdf2=xpdf(mn+2:L);
Pu=xpdf2/length(x4);
pp(1:mn+1)=0;
pp(mn+2:L)=Pu;
cU=cumsum(pp);

flx=110*cL;
fux=110+145*cU;
y0=zeros(m,n);
for i=1:m
    for j=1:n
        if x0(i,j)<mn+1
            y0(i,j)=flx(x0(i,j));
        elseif x0(i,j)>mn
            y0(i,j)=fux(x0(i,j));
        end
    end
end

ypdf=hist(reshape(y0,len,1),[0:L-1]); % pdf of y, 1 x L

figure(1),subplot(211),stem([0:L-1],xpdf),title('histogram, original')
axis([0 256 0 500])

subplot(212),stem([0:L-1],ypdf),title('histogram, equalized'),axis([0 256 0 500])

% figure(2),subplot(121),image(x0),colormap('gray')
% subplot(122),image(y0),colormap('gray')
figure(2),subplot(121),imshow(uint8(x0)),title('Original image')
subplot(122),imshow(uint8(y0)),title('Enhanced image')





