15,607,633 members
Articles / Web Development / HTML
Article
Posted 10 May 2017

14.1K views
501 downloads
4 bookmarked

# 2D-DFT for Color Image - VC++ GUI Implementation

Rate me:
4.00/5 (4 votes)
10 May 2017CPOL2 min read
2D DFT for Color Image - GUI implementation

## Introduction

GUI based implementation of 2D-DFT (Discrete Fourier transform) of color NxN (N - row and N - column size. for example: 256x256) BMP image. `GUI2DFT` is a simple tool implemented in VC++ that perform `Color` image into 2D-DFT and displays resulted image in RGB color.

## Background

Discrete fourier transform (DFT) is not less known for any students who study Image Processing subject at their Diploma or Graduation level. A number of references are available on the internet to understand the working of DFT. Few of the main advantages of Discrete fourier transform in Image Processing application are in:

1. Image Analysis
2. Image Filtering
3. Image Reconstruction
4. Image compression

etc.

When we compare Fast Fourier Transform (FFT) and Discrete fourier transform (DFT), FFT will consider all the frequencies to form an image, on the other hand DFT is just the simplest version of FFT and not considered the entire range of frequencies. But only a set of samples which is decent enough to describe image in spatial domain.

In spatial domain, the number of pixels analogous to the number of frequencies in frequency domain. Hence, the size of the image file considered for analysis are of same size in both the domains.

Suppose the size of the image is NxM, where N is the number of rows of pixel values and M is the number of Column pixel values, the formula to compute DFT for Image is:

For a NxN size (assuming it is a square sized image and number of rows and columns are one and the same) image, the 2D DFT is:

## Using the Code

C++
```STEP1:
//Method open bmp file separates header, raw data is stored in an array
int bmpfile::BmpfileRead(const char *fname_s)
{
int bpp_check = 0;

fopen_s(&fp_read_bmp,fname_s,"rb");
if (fp_read_bmp == NULL)
{
//cout << "Error opening in file ";
return -1;
}
...
//Separately allocate memory to read Red, Blue, and Green bands.
//Red
image_r = new unsigned char*[iwidth];
for(unsigned int i = 0; i < iheight; ++i)
image_r[i] = new unsigned char[iheight];

if (image_r == NULL)
{
sprintf_s(buflog, 25, "image_t error\n");
return -1;
}

//Green
image_g = new unsigned char*[iwidth];
for(unsigned int i = 0; i < iheight; ++i)
image_g[i] = new unsigned char[iheight];

if (image_g == NULL)
{
sprintf_s(buflog, 25, "image_t error\n");
return -1;
}
//Blue
image_b = new unsigned char*[iwidth];
for(unsigned int i = 0; i < iheight; ++i)
image_b[i] = new unsigned char[iheight];

if (image_b == NULL)
{
sprintf_s(buflog, 25, "image_t error\n");
return -1;
}
...
}

STEP2:
//Below code snippet is used to calculate dft for a single row data from the 2d image.
bool Dft::dft_oneD(int *x, int r)
{
int n,l;
double c1,c2,twedle,sp1=0.0,sp2=0.0;

for(l=0;l<r;l++)
{
for(n=0; n<r; n++){

twedle = 2.0*(3.141592653589)*n*l/r;

c1 = *(x+n)*cos(twedle);
c2 = *(x+n)*sin(-twedle);

sp1 = sp1+c1;
sp2 = sp2+c2;
}

yy[l].r = sp1;
yy[l].img = sp2;

sp1 = 0.0;
sp2 = 0.0;
}

return true;
}
STEP3:
//Perform row vise calculation first and then column vise, save the result in to separate 2d array
...
for(m=0;m<bfp->iwidth;m++)
{
YR[n][m] = yi[m].r;
YI[n][m] = yi[m].img;

if(YR[n][m] == YR[n][m]*(-1)) YR[n][m]*=(-1);
if(YI[n][m] == YI[n][m]*(-1)) YI[n][m]*=(-1);

RY[n][m] =  (int) abs(YR[n][m]*YR[n][m] + YI[n][m]*YI[n][m]);
RY[n][m] = (int)sqrt(RY[n][m]*1.0)/bfp->iwidth;
}
STEP4:
//write to output bmp image file
for(unsigned int j = 0; j != bfp->iheight; ++j)
{
for(unsigned int i = 0; i != bfp->iwidth; ++i)
{
bfp->image_r[j][i] = RY[j][i];
bfp->image_g[j][i] = GY[j][i];
bfp->image_b[j][i] = BY[j][i];
}
}

if(output_file != NULL)
{
bfp->BmpfileWrite(output_file);
}
else
{
//OUTPUT FILE
bfp->BmpfileWrite("dft.bmp");
}

//clean any resources used
for(unsigned int i = 0; i < bfp->iwidth; ++i)
{
delete [] RY[i];
delete [] GY[i];
delete [] BY[i];
}```

## Implementation

Steps involved are listed below:

1. Reading BMP 24bpp color image
2. Separating RGB bands as individual NxN sized images
3. Performing DFT computation on each of these 3 R,G, and B NxN sized images
4. Finally displaying dft.bmp color image by combining all 3 individually computed Images

### DFT2GUI Application

1D DFT is computed Row and then Column wise for NxN input image (Color BMP 24bpp), and thereafter shifting low frequency values to the center of the image. And GUI with few user menu control available.

VC++ source code is available on this post.

### Limitation

Right now, it is slow in computation. As the implementation is just for study purposes and not suited for any real time applications.

## History

This is my first post. Any suggestions are welcome.

## License

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

Written By
India
Raghavendra Prasad Hosad having 8+ years experience in IT as a Software Engineer.

## Comments and Discussions

 First Prev Next
 Can you send code for blind color image watermarking algorithm in spatial domain combining discrete fourier transform Sowmi Ga6-May-22 3:40 Sowmi Ga 6-May-22 3:40
 Interesting code eslipak18-May-17 8:20 eslipak 18-May-17 8:20
 Images are missing Tomas Takac16-May-17 23:43 Tomas Takac 16-May-17 23:43
 [My vote of 2] My vote 2 Gwamoniak14-May-17 21:45 Gwamoniak 14-May-17 21:45
 Last Visit: 31-Dec-99 18:00     Last Update: 21-Mar-23 14:53 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

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