Algorithm For Implementing Gaussian High Pass

 Algorithm For Implementing Gaussian High Pass:

  •  This is Matlab code to implement Gaussian High pass filter previous i posted about Low pass filter.
  • Gaussian High Pass filter is used sharpening of an image which is inverse of smoothing.
  • First image is read and then it’s shown as a figure just below in screen shots.
  • Calculate size of image.
  • Shift time domain of image into fourier or frequency domain by this command fft2(input-image).
  • Shift the origin of this transform into Center by this command fftshift(f_transform).
  • And then apply the equation of High pass filter that is 1 – exp(-(distance)^2/(2*(d0^2))) or 1 – lowPass.
  • After taking transform again shift and convert the image back into the time domain this is done my taking inverse fourier by this command ifftshift();
  • abs function is used to convert the negative values into the postive values.

Matlab Code :

function GaussianHighpass
a=imread(‘cameraman.tif’);
figure(1)
imshow(a)
[m n]=size(a);
f_transform=fft2(a);
f_shift=fftshift(f_transform);
p=m/2;
q=n/2;
d0=70;
for i=1:m
for j=1:n
distance=sqrt((i-p)^2+(j-q)^2);
low_filter(i,j)=1-exp(-(distance)^2/(2*(d0^2)));
end
end
filter_apply=f_shift.*low_filter;
image_orignal=ifftshift(filter_apply);
image_filter_apply=abs(ifft2(image_orignal));
figure(2)
imshow(image_filter_apply,[])

Advertisement

 

Input image :

Input image

Output Image (Sharpening):

 

sharp

Advertisement

26 Responses

  1. Hamzah

    great work, thanks
    I have one question Sir, how to display the filter(ex. Gaussian filter) in the time domain?

    Reply
    • ammadkhan

      In time domain first you will have to derive gaussian mask and then multiply that mask with the image by traversing the mask all over the image.This is manual method if u want inbuilt function i will search for this i will inform you soon keep visiting or subscribe to us you will get notification in your email

      Reply
  2. Ottavio Crisafulli

    Hi. THis work is really good. I have done even butterworth filters based in your script. I have a problem and i hope you can help me quickly: if have i to use a cuteoff frequency normalizzated in [0 1] how may i change this script. I have tryed but my work is not correct. May you send a modificated version with a d0 normalizated from 0 to 1, where 1 is the max cutoff frequency possible? I hope you may help me quickly.
    Thank you
    Best Regards
    Ottavio

    Reply
    • ammadkhan

      High pass filter basically blocks DC terms if you will choose cut off frequency near to 0 then u are allowing DC terms in output so result is not filtered or partially filtered so but if you really want to do this you can do but i don’t think its a logical question can you elaborate more
      try this code i did some changing in it
      function GaussianHighpass
      a=imread(‘cameraman.tif’);
      figure(1)
      imshow(a)
      [m n]=size(a);
      f_transform=fft2(a);
      f_shift=fftshift(f_transform);
      p=m/2;
      q=n/2;
      d0=0.5;
      for i=1:m
      for j=1:n
      distance=sqrt((i-p)^2+(j-q)^2);
      high_filter(i,j)=1-exp(-(distance)^2/(2*(50*d0^2)));
      end
      end
      filter_apply=f_shift.*high_filter;
      image_orignal=ifftshift(filter_apply);
      image_filter_apply=abs(ifft2(image_orignal));
      figure(2)
      imshow(image_filter_apply,[])

      Reply
  3. Ottavio Crisafulli

    Sorry Sir,
    my problem is another, i want to provide normalized values of d0, named d0norm, where 0 is lowest value of cutoff filter and 1 is max value of cutoff frequency, values has been normalizated to max distance. So i have modified your algorithm, but filtering doesn’t do nothing. I list you function for low filter, but it is the same for hight.
    I hope you may find what is problem and correct where i have done mistakes.
    this is my function:

    function [filter_apply] = LowBfilter2(imagex,order,dnorm)
    f_transform=fft2(imagex);
    f_shift=fftshift(f_transform);
    [m n]=size(imagex);
    p=m/2;
    q=n/2;
    for i=1:m
    for j=1:n
    distancex=sqrt((i-p).^2+(j-q).^2);
    end
    end
    distancex;
    d0=distancex*dnorm;
    for i=1:m
    for j=1:n
    distance=sqrt((i-p)^2+(j-q)^2);
    low_filter(i,j)=1/(1+(distance./d0)^2*order);
    end
    end
    filter_apply=f_shift.*low_filter
    return

    Thank you for your care and attention
    Best regards
    Ottavio

    Reply
      • Ottavio Crisafulli

        I have done a matlab script where i call that function.

        [filter_applylowbutt]=LowBfilter2(out,1,0.8);% Filtro passa basso Butterworth
        image_original3=ifftshift(filter_applylowbutt);
        image_filter_apply3=abs(ifft2(image_original3));
        figure

        So if i apply function without normalization so it all ok and i see blurring, otherwhile it doesn’t do nothing effect

      • ammadkhan

        what are the values you are using when you are testing your code which is not working LowBfilter2(imagex,order,dnorm)
        please specify your test values

      • ammadkhan

        Remember brother when ever in this situation when you will move your normalize do towards its max value 1 then your result will be less blurry and when you will reach at 1 in output you will see no blur and if you move normalize value tomwards 0 you will see image more blurry but u can’t make normalize value 0 cuz i every thing multiplied by 0 is 0 now your should call your function like this LowBfilter2(x,2,0.1); after calling this from command do more test like calling it with different values LowBfilter2(x,2,0.2); , LowBfilter2(x,2,0.3); , LowBfilter2(x,2,0.4);, LowBfilter2(x,2,0.5); , LowBfilter2(x,2,0.6); , LowBfilter2(x,2,0.7); ,LowBfilter2(x,2,0.8);, LowBfilter2(x,2,0.9);, LowBfilter2(x,2,1);

        Use This Function
        function [filter_apply] = LowBfilter2(imagex,order,dnorm)
        f_transform=fft2(imagex);
        f_shift=fftshift(f_transform);
        [m n]=size(imagex);
        p=m/2;
        q=n/2;
        for i=1:m
        for j=1:n
        distancex=sqrt((i-p).^2+(j-q).^2);
        end
        end
        distancex;
        d0=distancex*dnorm;
        %d0=1;
        for i=1:m
        for j=1:n
        distance=sqrt((i-p)^2+(j-q)^2);
        low_filter(i,j)=exp(-(distance)^2/(2*(d0^2)));
        %low_filter(i,j)=1/(1+(distance./d0)^2*order);
        end
        end
        filter_apply=f_shift.*low_filter;
        image_orignal=ifftshift(filter_apply);
        image_filter_apply=abs(ifft2(image_orignal));
        figure(1)
        imshow(imagex)
        figure(2)
        imshow(image_filter_apply,[])

      • Ottavio Crisafulli

        If i apply this:
        [filter_applylowbutt]=LowBfilter2(w,10,0.9) % 0.9 is d0norm, since 1 is max value , i should see a very blurried %image, instead it doesn’t apply nothing blurring

        Matrix result is a matrix like this:
        -0.0000 + 0.0000i 0.0000 – 0.0000i -0.0000 – 0.0000i
        0.0000 + 0.0000i -0.0000 + 0.0000i -0.0000 – 0.0000i
        -0.0000 – 0.0000i 0.0000 + 0.0000i -0.0000 – 0.0000i
        -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
        0.0000 – 0.0000i 0.0000 + 0.0000i 0.0000 – 0.0000i
        -0.0000 + 0.0000i -0.0000 – 0.0000i -0.0000 + 0.0000i
        0.0000 – 0.0000i -0.0000 + 0.0000i -0.0000 + 0.0000i
        -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 – 0.0000i
        0.0000 + 0.0000i -0.0000 – 0.0000i -0.0000 – 0.0000i
        -0.0000 – 0.0000i -0.0000 + 0.0000i -0.0000 + 0.0000i
        0.0000 + 0.0000i 0.0000 – 0.0000i 0.0000 – 0.0000i
        0.0000 – 0.0000i -0.0000 + 0.0000i -0.0000 – 0.0000i

      • ammadkhan

        as you move towards max values you will see less blurry images try using lower values for donorm use this function LowBfilter2(w,2,0.1) and apply order 2

      • ammadkhan

        And Use this function
        function [filter_apply] = LowBfilter2(imagex,order,dnorm)
        f_transform=fft2(imagex);
        f_shift=fftshift(f_transform);
        [m n]=size(imagex);
        p=m/2;
        q=n/2;
        for i=1:m
        for j=1:n
        distancex=sqrt((i-p).^2+(j-q).^2);
        end
        end
        distancex;
        d0=distancex*dnorm;
        %d0=1;
        for i=1:m
        for j=1:n
        distance=sqrt((i-p)^2+(j-q)^2);
        low_filter(i,j)=exp(-(distance)^2/(2*(d0^2)));
        %low_filter(i,j)=1/(1+(distance./d0)^2*order);
        end
        end
        filter_apply=f_shift.*low_filter;
        image_orignal=ifftshift(filter_apply);
        image_filter_apply=abs(ifft2(image_orignal));
        figure(1)
        imshow(imagex)
        figure(2)
        imshow(image_filter_apply,[])

      • Ottavio Crisafulli

        Sorry but function you have showed it isn’t butterworth but gaussian. You have putted a comment in low filter with butterwoth. Infact order variable is not defined in this way. May you give me a correct function.
        Thank you for support

      • ammadkhan

        Try This function

        function [filter_apply] = LowBfilter2(imagex,order,dnorm)
        f_transform=fft2(imagex);
        f_shift=fftshift(f_transform);
        [m n]=size(imagex);
        p=m/2;
        q=n/2;
        for i=1:m
        for j=1:n
        distancex=sqrt((i-p).^2+(j-q).^2);
        end
        end
        distancex;
        d0=distancex*dnorm;
        for i=1:m
        for j=1:n
        distance=sqrt((i-p)^2+(j-q)^2);
        low_filter(i,j)=1/(1+(distance./d0)^2*order);
        end
        end
        filter_apply=f_shift.*low_filter;
        image_orignal=ifftshift(filter_apply);
        image_filter_apply=abs(ifft2(image_orignal));
        figure(1)
        imshow(imagex)
        figure(2)
        imshow(image_filter_apply,[])

      • Ottavio Crisafulli

        Dear friend,
        you are really great. It is ok. I don’ know what you have modified but after a week i have founded several differents setting, now it is in function.
        For low values of d0norm image is blurred, instead fr hight value like for example 0.9 blur is low.
        Does it right function of this filter, right?

      • ammadkhan

        This is because when you choose values closer to 1 you are including all sharp values that’why image looks less or no blurry yes this function is right but i think you are mixing the concepts regarding sharpening and burring i would suggest you to google about sharpening blurring or sharpening

  4. sug13

    this commnt is inappropriate here,
    bt i needed matlab code for circle detection.
    May be using Circular Hough transform.
    Thanks in advance.

    Reply
  5. elfrida

    i have problem when i run my program. I have image of backhand vein with ekt.jpg. can you help me to repair my program?

    a=imread(’01-IMG-01.jpg’);
    figure(1)
    imshow(a)
    [m n]=size(a);
    f_transform=fft2(a);
    f_shift=fftshift(f_transform);
    p=m/2;
    q=n/2;
    d0=70;
    for i=1:m
    for j=1:n
    distance=sqrt((i-p)^2+(j-q)^2);
    low_filter(i,j)=1-exp(-(distance)^2/(2*(d0^2)));
    end
    end
    filter_apply=f_shift.*low_filter;
    image_orignal=ifftshift(filter_apply);
    image_filter_apply=abs(ifft2(image_orignal));
    figure(2)
    imshow(image_filter_apply,[])

    this the command when I run my program

    ??? Error using ==> times
    Matrix dimensions must agree.

    Error in ==> GaussianHighpasscoba at 20
    filter_apply=f_shift.*low_filter;

    Reply
    • Adnan Jilani

      In the first line change this line ( a=imread(’01-IMG-01.jpg’); ) to a=imread(‘ekt.jpg’);

      Reply
  6. kareem

    excuse me , could u explain why do you put p =m/2 , q = n/2 …not p= 2m and q =2n ??

    Reply

Leave a Reply