How can I implement a Homomorphic filter in Matlab?

3.8k views Asked by At

I have taken the source code from here and tried to modify it to fit my need. But, the filter is not working as expected. I am not being able to find the issue in my code.

What is the issue with my modified code?

Expected Output

enter image description here

Actual Output

enter image description here


Source Code

main.m

clc
close all
clear all
d=10;
order=2;
im=double(imread('tun.png'));
subplot(121)
imshow(im./255);
[r, c]=size(im);
j = homofil(im,d,order);
imshow(j);

homofil.m

function output = homofil(I, d, n)
    I = double(I);

    H = butter_hp_kernel(I, d, n); 

    alphaL = .0999;
    aplhaH = 1.01;
    H = ((aplhaH-alphaL).*H)+alphaL;
    H = 1-H;

    im_l = log2(1+I);
    im_f = fft2(im_l);
    im_nf = H.*im_f;
    im_n = abs(ifft2(im_nf));
    output = exp(im_n);

butter_hp_kernel.m

function k = butter_hp_kernel(I, Dh, n) 
    Height = size(I,1); 
    Width = size(I,2); 

    [u, v] = meshgrid( ...
                    -floor(Width/2) :floor(Width-1)/2, ...
                    -floor(Height/2): floor(Height-1)/2 ...
                 ); 

    k = butter_hp_f(u, v, Dh, n);

function f = butter_hp_f(u, v, Dh, n)
    uv = u.^2+v.^2;
    Duv = sqrt(uv);
    frac = Dh./Duv;
    %denom = frac.^(2*n);
    A=0.414; denom = A.*(frac.^(2*n));    
    f = 1./(1.+denom);

Input Image

enter image description here

1

There are 1 answers

5
Tasos Papastylianou On BEST ANSWER

I had a more detailed explanation and stackoverflow went into maintenance and I couldn't post. So here's the one-line explanation.

Your j is the wrong range: [1, 1.08] so it shows white.
Do imshow(j, []) or convert j with mat2gray