15,173,746 members
Articles / Multimedia / GDI+
Article
Posted 19 Nov 2009

263.3K views
74 bookmarked

# 2D FFT of an Image in C#

Rate me:
Two dimensional Fast Fourier Transform of an image in C#.

## 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:

C#
```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),
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.

C#
```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.

Original C++ code used for Reference can be found Here (Thank to Paul Bourke)

http://local.wasp.uwa.edu.au/~pbourke/miscellaneous/dft/

## History

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

## Share

 Re: 2D FFT final return chamara547-Aug-12 20:43 chamara54 7-Aug-12 20:43
 My vote of 5 Manoj Kumar Choubey28-Mar-12 1:35 Manoj Kumar Choubey 28-Mar-12 1:35
 Thanks Caspilar28-Apr-11 19:51 Caspilar 28-Apr-11 19:51
 shifting th image hooper195416-Mar-11 9:28 hooper1954 16-Mar-11 9:28
 Re: shifting th image QuantumDJ20-May-13 3:41 QuantumDJ 20-May-13 3:41
 fft and ifft N. krishnam19-Jul-10 4:03 N. krishnam 19-Jul-10 4:03
 Great article aldo hexosa18-Feb-10 16:32 aldo hexosa 18-Feb-10 16:32
 Thanks man. i have been searching FFT article in c#. Would you give me some example comparing 2 images using FFT?
 Message Removed 23-Dec-09 9:29 N_tro_P 23-Dec-09 9:29
 Re: Inverse FFT issue Dr. Vinayak Ashok Bharadi23-Dec-09 18:01 Dr. Vinayak Ashok Bharadi 23-Dec-09 18:01
 Message Removed 15-Dec-09 11:13 N_tro_P 15-Dec-09 11:13
 Message Removed 15-Dec-09 12:16 N_tro_P 15-Dec-09 12:16
 Message Removed 15-Dec-09 12:21 N_tro_P 15-Dec-09 12:21
 Re: [My vote of 2] Simple but lacking Dr. Vinayak Ashok Bharadi18-Dec-09 21:36 Dr. Vinayak Ashok Bharadi 18-Dec-09 21:36
 only on gray images? cifftsifir2-Dec-09 4:27 cifftsifir 2-Dec-09 4:27
 Re: only on gray images? Dr. Vinayak Ashok Bharadi2-Dec-09 17:49 Dr. Vinayak Ashok Bharadi 2-Dec-09 17:49
 Re: only on gray images? cifftsifir2-Dec-09 20:49 cifftsifir 2-Dec-09 20:49
 Re: only on gray images? cifftsifir4-Dec-09 3:59 cifftsifir 4-Dec-09 3:59
 Re: only on gray images? Dr. Vinayak Ashok Bharadi4-Dec-09 17:35 Dr. Vinayak Ashok Bharadi 4-Dec-09 17:35
 Re: only on gray images? Amarnath S11-Dec-09 4:27 Amarnath S 11-Dec-09 4:27
 Complex angle syutzy20-Nov-09 4:19 syutzy 20-Nov-09 4:19
 Re: Complex angle Dr. Vinayak Ashok Bharadi20-Nov-09 17:52 Dr. Vinayak Ashok Bharadi 20-Nov-09 17:52
 Needs More #realJSOP20-Nov-09 0:36 #realJSOP 20-Nov-09 0:36
 Re: Needs More f r i s c h20-Nov-09 5:44 f r i s c h 20-Nov-09 5:44
 Re: Needs More Andre Sanches (alvs)20-Nov-09 7:26 Andre Sanches (alvs) 20-Nov-09 7:26
 Last Visit: 31-Dec-99 19:00     Last Update: 22-Jan-22 14:57 Refresh ᐊ Prev123 Next ᐅ