Click here to Skip to main content
Licence CPOL
First Posted 19 Nov 2009
Views 36,342
Downloads 1,577
Bookmarked 33 times

2D FFT of an Image in C#

By Dr. Vinayak Ashok Bharadi | 20 Nov 2009
Two dimensional Fast Fourier Transform of an image in C#.

1
1 vote, 7.7%
2

3
1 vote, 7.7%
4
11 votes, 84.6%
5
4.94/5 - 13 votes
1 removed
μ 4.67, σa 1.55 [?]

Introduction

A Fast Fourier transform (FFT) is an efficient algorithm to compute the discrete Fourier transform (DFT) and its inverse. There are many distinct FFT algorithms involving a wide range of mathematics, from simple complex-number arithmetic to group theory and number theory; this article gives an overview of the available techniques and some of their general properties, while the specific algorithms are described in subsidiary articles linked below.

A DFT decomposes a sequence of values into components of different frequencies. This operation is useful in many fields (see discrete Fourier transform for properties and applications of the transform), but computing it directly from the definition is often too slow to be practical. An FFT is a way to compute the same result more quickly: computing a DFT of N points in the obvious way, using the definition, takes O(N 2) arithmetical operations, while an FFT can compute the same result in only O(N log N) operations. The difference in speed can be substantial, especially for long data sets where N may be in the thousands or millions—in practice, the computation time can be reduced by several orders of magnitude in such cases, and the improvement is roughly proportional to N/log(N). This huge improvement made many DFT-based algorithms practical; FFTs are of great importance to a wide variety of applications, from digital signal processing and solving partial differential equations to algorithms for quick multiplication of large integers.

Cooley–Tukey algorithm

By far the most common FFT is the Cooley–Tukey algorithm. This is a divide and conquer algorithm that recursively breaks down a DFT of any composite size N = N1N2 into many smaller DFTs of sizes N1 and N2, along with O(N) multiplications by complex roots of unity traditionally called twiddle factors.

This method (and the general idea of an FFT) was popularized by a publication of J. W. Cooley and J. W. Tukey in 1965, but it was later discovered (Heideman & Burrus, 1984) that those two authors had independently re-invented an algorithm known to Carl Friedrich Gauss around 1805 (and subsequently rediscovered several times in limited forms).

The most well-known use of the Cooley–Tukey algorithm is to divide the transform into two pieces of size N / 2 at each step, and is therefore limited to power-of-two sizes, but any factorization can be used in general (as was known to both Gauss and Cooley/Tukey). These are called the radix-2 and mixed-radix cases, respectively (and other variants such as the split-radix FFT have their own names as well). Although the basic idea is recursive, most traditional implementations rearrange the algorithm to avoid explicit recursion. Also, because the Cooley–Tukey algorithm breaks the DFT into smaller DFTs, it can be combined arbitrarily with any other algorithm for the DFT, such as those described below.

Using the code

First, we read the selected image into a 2D array of integers; we are using a pointer based image reading for the same. The code is as follows:

private void ReadImage()
{
    int i, j;
    GreyImage = new int[Width, Height];  //[Row,Column]
    Bitmap image = Obj;
    BitmapData bitmapData1 = 
       image.LockBits(new Rectangle(0, 0, image.Width, image.Height),
       ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
    unsafe
    {
        byte* imagePointer1 = (byte*)bitmapData1.Scan0;

        for (i = 0; i < bitmapData1.Height; i++)
        {
            for (j = 0; j < bitmapData1.Width; j++)
            {
                GreyImage[j, i] = (int)((imagePointer1[0] + 
                   imagePointer1[1] + imagePointer1[2]) / 3.0);
                //4 bytes per pixel
                imagePointer1 += 4;
            }//end for j
            //4 bytes per pixel
            imagePointer1 += bitmapData1.Stride - (bitmapData1.Width * 4);
        }//end for i
    }//end unsafe
    image.UnlockBits(bitmapData1);
    return;
}

FFT of an image will be a complex array; we need to store this thing, so we define a complex structure for the same.

struct COMPLEX
{
    public double real, imag;
    public COMPLEX(double x, double y)
    {
        real = x;
        imag = y;
    }
    public float Magnitude()
    {
        return ((float)Math.Sqrt(real * real + imag * imag));
    }
    public float Phase()
    {            
         return ((float)Math.Atan(imag / real));
    }
}

The FFT is implemented using the separability property of a Fourier transform. We find the FFT of the rows of an image and then the columns.

Points of interest

The Fourier plot is generated, frequency shifting is performed on the complex Fourier coefficients array, and using dynamic range compression, we generate the Fourier plot.

History

This is my first article on FFT, any suggestions and modifications are welcome.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Dr. Vinayak Ashok Bharadi

Instructor / Trainer
Thakur College of Engineering Technology
India India

Member
I have completed PhD in Engineering from NMIMS University in DEC 2012, M E Electronics & telecommunications in Nov 2007. .
 
I am working on Multimodal Biometrics Technology My rsearch guide is Respected Dr. H B Kekre. (Prof MPSTME).

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThanks PinmemberCaspilar19:51 28 Apr '11  
Generalshifting th image Pinmemberhooper19549:28 16 Mar '11  
Generalfft and ifft PinmemberN. krishnam4:03 19 Jul '10  
GeneralError [fix] PinmemberDadsy13:43 3 Mar '10  
GeneralGreat article Pinmemberaldo hexosa16:32 18 Feb '10  
GeneralInverse FFT issue PinmemberCollin Jasnoch9:29 23 Dec '09  
GeneralRe: Inverse FFT issue PinmemberVinayak Bharadi18:01 23 Dec '09  
General[My vote of 2] Simple but lacking PinmemberCollin Jasnoch11:13 15 Dec '09  
GeneralRe: [My vote of 2] Simple but lacking PinmemberCollin Jasnoch12:16 15 Dec '09  
GeneralRe: [My vote of 2] Simple but lacking PinmemberCollin Jasnoch12:21 15 Dec '09  
GeneralRe: [My vote of 2] Simple but lacking PinmemberVinayak Bharadi21:36 18 Dec '09  
Questiononly on gray images? Pinmembercifftsifir4:27 2 Dec '09  
AnswerRe: only on gray images? PinmemberVinayak Bharadi17:49 2 Dec '09  
GeneralRe: only on gray images? Pinmembercifftsifir20:49 2 Dec '09  
GeneralRe: only on gray images? Pinmembercifftsifir3:59 4 Dec '09  
GeneralRe: only on gray images? PinmemberVinayak Bharadi17:35 4 Dec '09  
GeneralRe: only on gray images? PingroupAmarnath S4:27 11 Dec '09  
GeneralComplex angle Pinmembersyutzy4:19 20 Nov '09  
GeneralRe: Complex angle PinmemberVinayak Bharadi17:52 20 Nov '09  
GeneralNeeds More PinmvpJohn Simmons / outlaw programmer0:36 20 Nov '09  
What is a "fourier transform"? Why is one better than the other? What's are fourier transforms used/useful for?
 
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

GeneralRe: Needs More Pinmemberf r i s c h5:44 20 Nov '09  
GeneralRe: Needs More PinmemberAndre Luiz V Sanches7:26 20 Nov '09  
GeneralRe: Needs More PinmemberVinayak Bharadi17:50 20 Nov '09  
GeneralRe: Needs More PinmemberVinayak Bharadi17:50 20 Nov '09  
GeneralRe: Needs More PinmvpJohn Simmons / outlaw programmer2:21 21 Nov '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120210.1 | Last Updated 20 Nov 2009
Article Copyright 2009 by Dr. Vinayak Ashok Bharadi
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid