-1

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

user366312
  • 16,949
  • 65
  • 235
  • 452

1 Answers1

1

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

Tasos Papastylianou
  • 21,371
  • 2
  • 28
  • 57
  • I am facing a continuous trouble with these two things: (1) `double` <--> `uint8`, (2) `imshow(I, [])`. I just can't understand which is required when. For instance, butterworth filters require I convert double to uint8, but some images do not show up. Now, I am stuck with this. – user366312 Jul 22 '17 at 01:47
  • uint8 images take values of type uint8 from 0 to 255. double images take values of type double from 0 to 1. To properly convert a uint8 image to a double one in the right range, use `im2double`. – Tasos Papastylianou Jul 22 '17 at 01:50
  • 1
    the `[]` in imshow simply uses the minimum and maximum range of the matrix as the black and white (i.e. it scales the contrast to match that range). If you don't give a range, then imshow will rely on the type (uint8 / double) to show the appropriate range. – Tasos Papastylianou Jul 22 '17 at 01:52
  • Yes I know. But, the problem is, I am not understanding which is needed when. – user366312 Jul 22 '17 at 01:52
  • 1
    I would work with double precision whenever possible. If a function explicitly requires uint8 convert to that, but be careful when dividing as you might get roundoff. But, in general, as long as you are treating the range you expect an image to be, in general both forms are fine. – Tasos Papastylianou Jul 22 '17 at 01:58