clear all
close all
clc 
%Reading An Image
[fname,fpath]=uigetfile('.*jpg','open');
I=imread(fname,'jpg');
%I=imread('shp.jpg','jpg');
N=size(I);
%I=I+((1.100).*(ones(N)));
I=rgb2hsv(I);
%I=double(I);
LI=I(:,:,3);
pp=I(:,:,3);

[x,y]=find(LI==0);
for li=1:length(x)
   LI(x(li),y(li))=0.001;
 end

I1=log(LI);
      
%I1=log(I(:,:,3));

% Value Value of HSV
%I1=I1(:,:,3);
%I1=logm(I);
ffI=fft2(I1);
ffI=fftshift(ffI);

D0=input('input D0 for the filter:');
gh=input('input gamaH for the filter:');
gl=input('input gamaL for the filter:');
c=input('input C sharpness control of the filter:');

F=zeros(N(1),N(2));
%DEFINATION OF  HIGH PASS FILTER FOR HOMOMORPHIC
s=gh-gl;
for i=1:N(1)
  for j=1:N(2)
     %F(i,j)=1-exp(-((i- (N(1)/2) )^2+(j- (N(2)/2) )^2 )/(D0^2));       
     F(i,j)=s*(1-exp(-c*((i- (N(1)/2) )^2+(j- (N(2)/2) )^2 )/(D0^2)))+gl;       
  end
end

%HOMOMORPHIC HIGHPASSING FILTER
x=F.*ffI;
x=fftshift(x);
y=abs(ifft2(x));
%z=expm(y);
z=exp(y);


%MEDIAN OF BACKGROUND AND GAIN COMPUTATION(Highpass)

aa=median(median(z));
bb=median(median(pp));

gain=aa./bb;

%background detection
gain_image=gain.*pp;
sub_image=gain_image./z;

tr=.5*mean((mean(z,1)),2);
%Thresholding
SS=size(sub_image);
for ti=1:SS(1)
   for tj=1:SS(2)
      if (sub_image(ti,tj)>=tr)
         threshold(ti,tj)=1;
      else
         threshold(ti,tj)=0;
      end
   end
end

%LOW PASS FILTERING
%Homomorphic LOW PASS FILTER

F1=zeros(N(1),N(2));

for i=1:N(1)
  for j=1:N(2)
     F1(i,j)=s*exp(-c*((i- (N(1)/2) )^2+(j- (N(2)/2) )^2 )/(D0^2))+gl;       
  end
end

% Lowbass FILTERING
x1=F1.*ffI;
x1=fftshift(x1);
y1=abs(ifft2(x1));

z1=exp(y1);
%z1=expm(y1);

%Thresholding
SS=size(z1);
thereshold1=zeros(SS(1),SS(2));

T=ceil(mean2(z1)*1.5);
for ti=1:SS(1)
   for tj=1:SS(2)
      if (z1(ti,tj)>T)
         threshold1(ti,tj)=1;
      else
         threshold1(ti,tj)=0;
      end
   end
end



%MEDIAN OF BACKGROUND AND GAIN COMPUTATION(Lowpass)

al=median(median(z1));
bl=median(median(pp));

gainl=al./bl;

%background detection
gainl_image=gainl.*pp;
subl_image=z1-gainl_image;


 
%Result Presenting For HPF
figure;
hold on;
subplot(2,2,1)
imshow(pp,[])
title('original Image')

subplot(2,2,2)
imshow(z.*0.3)
title('results HPF')

subplot(2,2,4)
imshow(sub_image)
title('detection by background equalization')

subplot(2,2,3)
imshow(threshold)
title('Thresholding of shadow after BE')

%Result Presenting For LPF
figure;
hold on;
subplot(2,2,1)
imshow(pp,[])
title('original Image')

subplot(2,2,3)
imshow(threshold1,[])
title('Thresholding of shadow after LPF')

subplot(2,2,2)
imshow(z1,[])
title('Result of LPF')

subplot(2,2,4)
imshow(subl_image)
title('detection by background equalization.')
  



