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





4.00/5 (4 votes)
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:
- Image Analysis
- Image Filtering
- Image Reconstruction
- 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
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:
- Reading BMP 24bpp color image
- Separating RGB bands as individual NxN sized images
- Performing DFT computation on each of these 3 R,G, and B NxN sized images
- 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.
GUI
Result
History
This is my first post. Any suggestions are welcome.