Sie sind auf Seite 1von 22

LAB #11

Homomorphic Filtering

Objective:

1. Enhance image using Homomorphic Filtering

Description:
Homomorphic filtering is a generalized technique for signal and image processing,
involving a nonlinear mapping to a different domain in which linear filter techniques are applied,
followed by mapping back to the original domain.
Image enhancement

Homomorphic filter is sometimes used for image enhancement. It simultaneously


normalizes the brightness across an image and increases contrast. Here homomorphic filtering is
used to remove multiplicative noise. Illumination and reflectance are not separable, but their
approximate locations in the frequency domain may be located. Since illumination and
reflectance combine multiplicatively, the components are made additive by taking
the logarithm of the image intensity, so that these multiplicative components of the image can be
separated linearly in the frequency domain. Illumination variations can be thought of as a
multiplicative noise, and can be reduced by filtering in the log domain.

Operation

Homomorphic Filtering can be used for improving the appearance of a grayscale image
by simultaneous intensity range compression (illumination) and contrast enhancement
(reflection).

Where,

m = image,

i = illumination,

r = reflectance

We have to transform the equation into frequency domain in order to apply high pass filter.
However, it's very difficult to do calculation after applying Fourier transformation to this
equation because it's not a product equation anymore. Therefore, we use 'log' to help solving this
problem.
Then, applying Fourier transformation

Or
Next, applying high-pass filter to the image. To make the illumination of an image more even,
the high-frequency components are increased and low-frequency components are decrease.

Where

H = any high-pass filter

N = filtered image in frequency domain

Afterward, returning frequency domain back to the spatial domain by using inverse Fourier
transform.

Task 1:
Enhance an image by applying Homorphic Filtering in Matlab.

Code
clear all
%read input image
dim=imread('football.jpg');
cim=double(dim);
[r,c]=size(dim);
cim=cim+1;
% add 1 to pixels to remove 0 values which would result in undefined log
values
% natural log
lim=log(cim);
%2D fft
fim=fft2(lim);
lowg=.9; %(lower gamma threshold, must be lowg < 1)
highg=1.1; %(higher gamma threshold, must be highg > 1)
% make sure the the values are symmetrically differenced
% function call
him=homomorph(fim,lowg,highg);
%inverse 2D fft
ifim=ifft2(him);

%exponent of result
eim=exp(ifim);

figure;
subplot(4,3,1);imshow(dim);title('Origional image');
subplot(4,3,2);imshow(lim);title('Natural Logarithm');
subplot(4,3,3);imshow(uint8(fim));title('Fourier transform');
subplot(4,3,4);imshow(him);title('Homomorphic filter');

Output
Filtering with Frequency Domain filters

a) Lowpass filters
Ideal Lowpass filter
Code
I=imread('cameraman.tif');

[m,n,h]=size(I);

if h~=1

I=rgb2gray(I);

end

subplot(1,3,1)
imshow(I);title('Original Image');
I_o=I;

subplot(1,3,2)
imshow(I);title('noisy Image');

I=imnoise(I,'gaussian',0,0.03);

[M N]=size(I);
a=im2double(I);
F1=fft2(a); %Obtain the Fourier transform
% Set up range of variables.
u = 0:(M-1); %0-255
v = 0:(N-1);%0-255
% center (u,v) = (M/2,N/2)
% Compute the indices for use in meshgrid
idx = find(u > M/2);% indices 130-255
u(idx) = u(idx) - M;
idy = find(v > N/2);
v(idy) = v(idy) - N;
%set up the meshgrid arrays needed for
% computing the required distances.
[U, V] = meshgrid(u, v);
% Compute the distances D(U, V).
D = sqrt(U.^2 + V.^2);
disp('IDEAL LOW PASS FILTERING IN FREQUENCY DOMAIN');
D0=input('Enter the cutoff distance==>');
% Begin filter computations.
H = double(D <= D0);
G=H.*F1; %Multiply
G=ifft2(G);
G=real(G);
ff=abs(fftshift(H));
subplot(133),imshow(G),title('IDEAL LPF Filtered Image')
Accuracy=Acc(I_o,G);

disp(['Accuracy :- ' ,num2str(Accuracy)])

Output

Accuracy :- 0.18727

Gaussian Lowpass filter


Code
I=imread('eight.tif');

[m,n,h]=size(I);

if h~=1

I=rgb2gray(I);

end
subplot(1,3,1)
imshow(I);title('Original Image');
I_o=I;

I=imnoise(I,'salt & pepper',0.03);


subplot(1,3,2)
imshow(I);title('noisy Image');

%I=imnoise(I,'gaussian',0,0.03);

I=im2double(I);
FI=fft2(I); %Obtain the Fourier transform
LP=fspecial('gaussian',[11 11],1.3); %Generate a Low-Pass filter
FLP=fft2(LP,size(I,1),size(I,2)); % Filter padding
LP_OUT=FLP.*FI; %Multiply the transform by the filter
I_OUT_LP=ifft2(LP_OUT); %inverse DFT
I_OUT_LP=real(I_OUT_LP); %Obtain the real part(Output)
%%%%spectrum%%%%
FLP_S=abs(fftshift(FLP));%Filter spectrum
LP_OUT_S=abs(fftshift(LP_OUT));%output spectrum

subplot(1,3,3),imshow(I_OUT_LP),title('LowPass gaussian Filtered ')

Accuracy=Acc(I_o,medianFilteredImage);

disp(['Accuracy :- ' ,num2str(Accuracy)])

Output
Accuracy :- 0.97416

Butterworth Lowpass filter


Code
I=imread('football.jpg');

%Convert to grayscale
footBall=rgb2gray(I);

[m,n,h]=size(I);

if h~=1

I=rgb2gray(I);

end

subplot(1,3,1)
imshow(I);title('Original Image');
I_o=I;

I=imnoise(I,'salt & pepper',0.03);


subplot(1,3,2)
imshow(I);title('noisy Image');

%I=imnoise(I,'gaussian',0,0.03);
%Determine good padding for Fourier transform
PQ = paddedsize(size(footBall));

%Create a Gaussian Lowpass filter 5% the width of the Fourier


transform
D0 = 0.05*PQ(1);
H = lpfilter('btw', PQ(1), PQ(2), D0);

% Calculate the discrete Fourier transform of the image


F=fft2(double(footBall),size(H,1),size(H,2));

% Apply the highpass filter to the Fourier spectrum of the image


LPFS_football = H.*F;

% convert the result to the spacial domain.


LPF_football=real(ifft2(LPFS_football));

% Crop the image to undo padding


LPF_football=LPF_football(1:size(footBall,1), 1:size(footBall,2));

%Display the blurred image


figure, imshow(LPF_football, [])

% Display the Fourier Spectrum


% Move the origin of the transform to the center of the frequency
rectangle.
Fc=fftshift(F);
Fcf=fftshift(LPFS_football);
% use abs to compute the magnitude and use log to brighten display
S1=log(1+abs(Fc));
S2=log(1+abs(Fcf));
figure, imshow(S1,[])
figure, imshow(S2,[])

Accuracy=Acc(I_o,S2);

disp(['Accuracy :- ' ,num2str(Accuracy)])

Output
Accuracy :- 0.0052368

b) Highpass filters
Ideal Highpass filters
Code
I=imread('cameraman.tif');

[m,n,h]=size(I);

if h~=1
I=rgb2gray(I);

end

subplot(1,3,1)
imshow(I);title('Original Image');
I_o=I;

%I=imnoise(I,'salt & pepper',0.03);


subplot(1,3,2)
imshow(I);title('noisy Image');

I=imnoise(I,'gaussian',0,0.03);

[M N]=size(a)
a=im2double(a);
F1=fft2(a); %Obtain the Fourier transform
[F,G ]=size(F1)
% Set up range of variables.
u = 0:(M-1); %0-255
v = 0:(N-1);%0-255
% center (u,v) = (M/2,N/2)
% Compute the indices for use in meshgrid
idx = find(u > M/2);% indices 130-255
u(idx) = u(idx) - M;
idy = find(v > N/2);
v(idy) = v(idy) - N;
%set up the meshgrid arrays needed for
% computing the required distances.
[U, V] = meshgrid(u, v);
% Compute the distances D(U, V).
D = sqrt(U.^2 + V.^2);
disp('IDEAL LOW PASS FILTERING IN FREQUENCY DOMAIN');
D0=15;
% Begin filter computations.
H = double(D > D0);
[T,L]=size(H)
G=H.*F1; %Multiply
G=ifft2(G);
G=real(G);
ff=abs(fftshift(H));

subplot(133),imshow(G),title('IDEAL HPF Filtered Image')

Accuracy=Acc(I_o,G);

disp(['Accuracy :- ' ,num2str(Accuracy)])


Output

Accuracy :- 0.064072

Gaussian Highpass filters


Code
I=imread('eight.tif');

[m,n,h]=size(I);

if h~=1

I=rgb2gray(I);
end

subplot(1,3,1)
imshow(I);title('Original Image');
I_o=I;

I=imnoise(I,'salt & pepper',0.03);


subplot(1,3,2)
imshow(I);title('noisy Image');

%I=imnoise(I,'gaussian',0,0.03);

I=im2double(I);
[n,c]=size(I)
FI=fft2(I);
LP=fspecial('gaussian',1);
FLP=fft2(LP,n,c);
[A,B]=size(FLP)
LP_OUT=FLP.*FI ;
I_OUT_LP=ifft2(LP_OUT);
I_OUT_LP=real(I_OUT_LP);
FLP_S=abs(fftshift(FLP));
LP_OUT_S=abs(fftshift(LP_OUT));

subplot(133),imagesc(0.5*log(1+FLP_S)),title('gaussian HighPass
Filter')

Accuracy=Acc(I_o,LP_OUT_S);

disp(['Accuracy :- ' ,num2str(Accuracy)])

Output
Accuracy :- 0.0028443

Butterworth Highpass filters


Code
I=imread('eight.tif');

[m,n,h]=size(I);

if h~=1

I=rgb2gray(I);

end

subplot(1,3,1)
imshow(I);title('Original Image');
I_o=I;

I=imnoise(I,'salt & pepper',0.03);


subplot(1,3,2)
imshow(I);title('noisy Image');

%I=imnoise(I,'gaussian',0,0.03);

I=im2double(I);
[n,c]=size(I)
FI=fft2(I);
LP=fspecial('gaussian',1);
FLP=fft2(LP,n,c);
[A,B]=size(FLP)
LP_OUT=FLP.*FI ;
I_OUT_LP=ifft2(LP_OUT);
I_OUT_LP=real(I_OUT_LP);
FLP_S=abs(fftshift(FLP));
LP_OUT_S=abs(fftshift(LP_OUT));

subplot(133),imagesc(0.5*log(1+FLP_S)),title('gaussian HighPass
Filter')

Accuracy=Acc(I_o,LP_OUT_S);

disp(['Accuracy :- ' ,num2str(Accuracy)])

Output
Accuracy :- 0.0028

Das könnte Ihnen auch gefallen