为什么我做傅立叶变换后 变回来的总是黑色

解决方案 »

  1.   

    这是我的代码 网上找的改的因为看不懂离散算法 this->m_pDesDib->m_lpDIB[j][i].r
    ...............................g
    ...............................b
    是以左下角位0,0 点的位置  为 j行 i 列的 像素分别的r,g,b  指针BOOL CFourier:: PowerOf2(int n, int* bit)
    {
    if(n <= 0) return FALSE;
    int temp = n;
    int count = 0;
    *bit = 0;
    for(int i=0; i<sizeof(int)*8; i++) {
    if(temp&0x00000001) {
    count++;
    *bit = i;
    }
    temp = temp>>1;
    }
    if(count > 1) return FALSE;
    return TRUE;
    }
    BOOL CFourier::HistogramEqualize(){
    return CBmpProcess::HistogramEqualize();
    }/*-------------------------------------------------------------------------
    This computes an in-place complex-to-complex FFT
    x and y are the real and imaginary arrays of 2^m points.
    dir =  1 gives forward transform
    dir = -1 gives reverse transform
    Formula: forward
      N-1
      ---
      1   \          - j k 2 pi n / N
    X(n) = ---   >   x(k) e                    = forward transform
      N   /                                n=0..N-1
      ---
      k=0
    Formula: reverse
      N-1
      ---
      \          j k 2 pi n / N
    X(n) =       >   x(k) e                    = forward transform
      /                                n=0..N-1
      ---
      k=0
    --------------------------------------------------------------------------*/
    BOOL CFourier:: FFT(BOOL dir,int m,double *x,double *y)
    {
    long nn,i,i1,j,k,i2,l,l1,l2;
    double c1,c2,tx,ty,t1,t2,u1,u2,z;
    /* Calculate the number of points */
    nn = 1;
    for (i=0;i<m;i++)
    nn *= 2;
    /* Do the bit reversal */
    i2 = nn >> 1;
    j = 0;
    for (i=0;i<nn-1;i++) {
    if (i < j) {
    tx = x[i];
    ty = y[i];
    x[i] = x[j];
    y[i] = y[j];
    x[j] = tx;
    y[j] = ty;
    }
    k = i2;
    while (k <= j) {
    j -= k;
    k >>= 1;
    }
    j += k;
    }
    /* Compute the FFT */
    c1 = -1.0;
    c2 = 0.0;
    l2 = 1;
    for (l=0;l<m;l++) {
    l1 = l2;
    l2 <<= 1;
    u1 = 1.0;
    u2 = 0.0;
    for (j=0;j<l1;j++) {
    for (i=j;i<nn;i+=l2) {
    i1 = i + l1;
    t1 = u1 * x[i1] - u2 * y[i1];
    t2 = u1 * y[i1] + u2 * x[i1];
    x[i1] = x[i] - t1;
    y[i1] = y[i] - t2;
    x[i] += t1;
    y[i] += t2;
    }
    z =  u1 * c1 - u2 * c2;
    u2 = u1 * c2 + u2 * c1;
    u1 = z;
    }
    c2 = sqrt((1.0 - c1) / 2.0);
    if (dir == TRUE)
    c2 = -c2;
    c1 = sqrt((1.0 + c1) / 2.0);
    }
    /* Scaling for reverse forward transform */
    if (dir == FALSE) {
    for (i=0;i<nn;i++) {
    x[i] /= (double)nn;
    y[i] /= (double)nn;
    }
    }
    return(TRUE);
    }BOOL CFourier::FFT2D(  BOOL dir)
    {
    int i,j;
    int m;
    double *real,*imag;
    int nx=this->m_pDesDib->m_lpBMIH->biWidth;
    int ny=this->m_pDesDib->m_lpBMIH->biHeight;
    /* Transform the rows */
    real = (double *)malloc(nx*sizeof(double));
    imag = (double *)malloc(nx*sizeof(double));
    if (real == NULL || imag == NULL)
    return(FALSE);
    if(!PowerOf2(nx, &m)) return FALSE;
    //b
    for (j=0;j<ny;j++) {

    for (i=0;i<nx;i++) {
    real[i] = *(this->m_pDesDib->m_lpDIB[j][i].r);
    imag[i] = 0;
    }
    FFT(dir,m,real,imag);
    for (i=0;i<nx;i++) {
    *(this->m_pDesDib->m_lpDIB[j][i].r) =min((BYTE) sqrt(real[i]*real[i]+imag[i]*imag[i]),255);
     //*(this->m_pDesDib->m_lpDIB[j][i].r) =real[i];
    }
    }//end for j
    /*
    //g
    for (j=0;j<ny;j++) {

    for (i=0;i<nx;i++) {
    real[i] = *(this->m_pDesDib->m_lpDIB[j][i].g);
    imag[i] = 0;
    }
    FFT(dir,m,real,imag);
    for (i=0;i<nx;i++) {
    *(this->m_pDesDib->m_lpDIB[j][i].g) =(BYTE) sqrt(real[i]*real[i]+imag[i]*imag[i]);
     
    }
    }
    //r
    for (j=0;j<ny;j++) {

    for (i=0;i<nx;i++) {
    real[i] = *(this->m_pDesDib->m_lpDIB[j][i].r);
    imag[i] = 0;
    }
    FFT(dir,m,real,imag);
    for (i=0;i<nx;i++) {
    *(this->m_pDesDib->m_lpDIB[j][i].r) =(BYTE) sqrt(real[i]*real[i]+imag[i]*imag[i]);
     
    }
    }*/
    free(real);
    free(imag);
    /* Transform the columns */
    real = (double *)malloc(ny * sizeof(double));
    imag = (double *)malloc(ny * sizeof(double));
    if (real == NULL || imag == NULL)
    return(FALSE);
    if(!PowerOf2(ny, &m)) return FALSE;//b
    for (i=0;i<nx;i++) {
    for (j=0;j<ny;j++) {
    real[j] = *(this->m_pDesDib->m_lpDIB[j][i].b);
    imag[j] = 0;
    }
    FFT(dir,m,real,imag);
    for (j=0;j<ny;j++) {
    *(this->m_pDesDib->m_lpDIB[j][i].b) =min((BYTE) sqrt(real[j]*real[j]+imag[j]*imag[j]),255);
    //*(this->m_pDesDib->m_lpDIB[j][i].b) =real[j];
    }
    }//end for j//gfor (i=0;i<nx;i++) {
    for (j=0;j<ny;j++) {
    real[j] = *(this->m_pDesDib->m_lpDIB[j][i].g);
    imag[j] = 0;
    }
    FFT(dir,m,real,imag);
    for (j=0;j<ny;j++) {
    *(this->m_pDesDib->m_lpDIB[j][i].g) =min((BYTE) sqrt(real[j]*real[j]+imag[j]*imag[j]),255);}
    }
    //r
    for (i=0;i<nx;i++) {
    for (j=0;j<ny;j++) {
    real[j] = *(this->m_pDesDib->m_lpDIB[j][i].r);
    imag[j] = 0;
    }
    FFT(dir,m,real,imag);
    for (j=0;j<ny;j++) {
    *(this->m_pDesDib->m_lpDIB[j][i].r) =min((BYTE) sqrt(real[j]*real[j]+imag[j]*imag[j]),255);}
    }
    free(real);
    free(imag);
    return(TRUE);
    }