# Image Filtering Using Fourier Transform

In Machine Vision there is a magical trick that involves Fourier transformation of an image and I would like to share it with you today. I learned this long ago from a professor of mine but I remember back then there was not much experience and information about this on the web. Also there was no program that allowed experimenting with Fourier transformation of Images, until I developed an Android application titled Image Transformer, using OpenCV and Qt, which allows you to do just that.

Normally, to filter or change an image you would apply some kind of filter to the image and observe the changes in the resulting image. There is a second and much more interesting method which is done by using a Fourier transformation on the image, applying a filter (anything possible) and then converting back to the original image (this is also called reverse Fourier transform). Picture below explains it all:

The power behind this phenomenon might not be obvious at first but if you experiment with it more and more, you will notice it. For example in the picture above what I have done is actually eliminating horizontal lines in an image by applying a mask to the Fourier transformation of the image. Normally it would sound impossible for someone working with machine vision but using Fourier transform you can see that it is quite easy.

Below are the original pictures I used to created the picture above. Checkout and see for yourself that even by applying a rough and not precise mask I was able to get rid of horizontal lines. There are many hidden aspects about this, yet to be found, and you can start experimenting for yourself by downloading and using Image Transformer.

Please also let me know if you find any issues with the app or if you come up with any useful ideas that you think might help other researchers benefit from this app.

## 9 Replies to “Image Filtering Using Fourier Transform”

1. Gujjar says:

Hello Amin,
Thanks for ur quick response. Actually I want to apply notch filter to remove specific pattern in image just like (line ,circle ,etc..) . I want to remove some specific frequency of line or circle using notch filter or any other way of removing. Can you guide me how can i remove specific repetitive pattern from image just like circle or line. if you give some example code it help me a lot. because currently I can not understand how I apply filter or notch to remove certain repetitive pattern form Image. Waiting to hear from you soon. Thank you.

1. Although it sounds extremely interesting and I just added it to my reading list, I can’t help you with the filter itself.
I would be happy to help you with applying it if you know your filter. (Applying it shouldn’t be too hard anyway 🙂 )
Can you send me an example noch filter you want to apply, you can send something hand-drawn, just to give me the idea.

2. Pedro Gabriel says:

Hello, I’m trying to use OpenCV to apply the dft algorithm into images capture by the camera phone but I’m having no success. Have you used this algorithm:
http://docs.opencv.org/2.4/doc/tutorials/core/discrete_fourier_transform/discrete_fourier_transform.html

Mine is crashing at:

If you are interested in helping me, there is a post in stack overflow:
http://stackoverflow.com/questions/38174269/android-real-time-dft-in-camera-view-using-opencv?noredirect=1#comment63776021_38174269

thank you.

1. Hi Pedro,
I’d love to help you but I am not using Java that much. Most of what you see on my website is about Qt and OpenCV (both in C++) The link to Frourier function that you provided is also C++ version of it which I use.
With that being said, you can search use the menu at the top to get to Android or OpenCV tutorials section.
Also you can search for “JNI” using the search box at the top right. Here it is: http://amin-ahmadi.com/?s=jni
Let me know if this helped you.

1. Pedro Pinto says:

Hello amin,
Thank you for the quick answer. I also tried using jni and my app still crashing. I’ll read your tutorials and try again with. Jni!
First, do you think it is possible to apply on real time capture? (that was the first thing I tried to do).

Thank you again.

hello Amin, I read you post in fft using opencv. Can you provide me opencv (c++) code how do you remove the horizontal line in the given image. I also try to do same thing . please guide me I want to remove the repetitive structure in my image in Fourier domain so that remaining just i got defect . thanks waiting to hear from you soon.

Assuming that you don’t have any problems with Fourier Transform itself and the dft function of OpenCV, then you can simply use the following:

void inverseFourier()
{
cv::Mat newImg, planes[2];
complex_image.copyTo(newImg);
cv::dft(complex_image, newImg, DFT_REAL_OUTPUT);
cv::split(newImg, planes);
cv::normalize(planes[0], planes[0], 0, 255, CV_MINMAX);
cv::flip(planes[0], planes[0], -1);
planes[0].convertTo(image, CV_8UC1);
cvtColor(image, image, CV_GRAY2RGB);
displayImage(image);
}

Note that complex_image is obtained using a previous call to dft (without any additional parameters for inverse transform)
Then set the relevant elements in the Mat to zero. (Note that this is just a trick and you need to set the elements over and above the center to zero almost like it is seen in the pictures. Please also note that this is NOT a complete solution for removing horizontal lines or nowhere near it. Just an experiment with the DFT)

1. Gujjar says:

Hello Amin,
First of all thank you for ur quick response. Actually I want to apply notch filter in Fourier domain to remove all repetitive pattern just line line or circle . By using notch filter I want to remove all frequency of repetitive pattern in image(circle or line etc) . I Can you guide me how can i use notch filter to remove specific pattern. or you give me code example how do that? Thanks again

This site uses Akismet to reduce spam. Learn how your comment data is processed.