Navigation

Related Articles

Back to Latest Articles

How To Use Ideal Highpass Filter – C# Guide


Andraz Krzisnik
How To Use Ideal Highpass Filter – C#...

Ideal highpass filter does exactly the opposite of ideal lowpass filter. It only passes high frequencies and removes low frequencies. So if you imagine how lowpass yielded a circle of frequencies in the middle, highpass filters do the exact opposite – a black hole in the middle.

In case you’re just tunning in, I’m talking about filtering in the frequency domain. So first thing we need to do is convert image into frequency domain using Fourier transform. Then we filter it and use inverse Fourier transform to turn it back into spatial domain.

Ideal highpass filter

ideal highpass filter

When we’re filtering images in frequency domain there is a chance that some unwanted artifacts will appear on the image once we transform it back into spatial domain. We call this kind of effect ringing and it looks like colored blobs on the image.

Ringing appears because of abrupt attenuation of low frequencies beyond the set cut off frequency. We need to set a cut off frequency, which is basically a radius of the “black hole” in the middle of the frequency map.

ideal highpass filter result in frequency domain

You can visualize Fourier transform of an image by normalizing the magnitudes of the frequency map and bring their range down between 0 and 255. Once we get those values down to displayable range, we also need to use logarithmic transform on them.

The purpose of logarithmic transformation is to make values visible. Largest frequency is in the center of the map. Prior to logarithmic transformation, we wouldn’t be able to see much of anything else besides the center.

C# function for filtering with ideal highpass filter

I’ve written a function that filters frequency maps across all color channels. For it to work, we also need to give it 3 frequency maps in a form of 3 dimensional jagged array of complex numbers. And of course we need to give it the cut off frequency – the radius of the circle in the middle.

public static Complex[][][] Ideal_Highpass_Filter(Complex[][][] frequencies, double distance)
         {
             int channels = 3;
             Complex[][][] filtered = frequencies;
             for (int c = 0; c < channels; c++)
             {
                 for (int u = 0; u < Size; u++)
                 {
                     for (int v = 0; v < Size; v++)
                     {
                         double d = Math.Sqrt(Math.Pow(u - Size / 2, 2) + Math.Pow(v - Size / 2, 2));
                         if (d <= distance)
                         {
                             filtered[c][u][v] = new Complex(0, 0);
                         }
                         else
                         {
                             filtered[c][u][v] = frequencies[c][u][v];
                         }
                     }
                 }
             }
             return filtered;     
         }

It’s almost the same as the function for ideal lowpass filter. The only difference between the two is that we swapped which values we keep.

Conclusion

This tutorial is just one in a series where we talked about filtering in the frequency domain. The majority of the work the program does is conversion of the image from spatial domain to frequency domain and back.

You can download the entire project and try it out yourself. Maybe try playing with the values and see how it effects the filtered image as well.

Related Articles

C# Tutorial

C# Tutorial: Contrast Stretching with Normalization

This post is a short revision of Contrast Stretch post we already worked on in the past. Where we talked about histogram equalization, which is a little more complex method than...

Posted on by Andraz Krzisnik
Edge Detection

How To Make Canny Edge Detection Algorithm With C#

Canny edge detection process is an edge detection based segmentation operation in image processing for accurately extracting edges.

Posted on by Andraz Krzisnik