Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
10 May 2017 1  
2D DFT for Color Image - GUI implementation.

 

Introduction

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

Background

Descrete fourier transform (DFT) is not less known for any students who study Image Processing subject at thier Diploma or Graduation level. A number of refrences available on the internet to understand the working of DFT. Few of the main advantages of Descrete fourier transform in Image Processing application are in  a) Image Analysis b) Image Filtering c) Image Reconstruction c) Image compression etc.

When we compare Fast Fourior Transform (FFT) and Descrete fourier transform (DFT), FFT will consideres all the freequencies to form an image on the other hand DFT is just simplest version of FFT and not cosider the entire range of freequencies. But only a set of samples which is decent enough to describe image in spacial domain.

In spacial domain the number of pixes analogous to the number of freequencies in freequency domain. Hence the size of the image file considered for analysis are of same size in both the domain.

Suppose the size of the imahe 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 seperates header, raw data is stored in an arry

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;
    }
  ...
   //Seperatelly 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 seperate 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

1. Reading BMP 24bpp color image.

2.  Seperating RGB bands as individual NxN sized images.

3. Performing DFT computaion on each of these 3 R,G, and B NxN sized images.

4. finally displaying dft.bmp color image by combinin all 3 individualy computed Images

DFT2GUI Application:

1D DFT is computed Row and then Column wise for NxN input image (Color BMP 24bpp), and there after shifting low freequency 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 implemenation is just for study purpose and not suited for any real time applications.

GUI:

Result:

Points of Interest

My name is Raghavendra Prasad Hosad. I am having 8+ years of IT experience. In my free time Interested in coding  software applications.

History

As this is my first post. Any suggessions are welcome.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here