Image Processing Fundamentals (3) DFT and IDFT Transformation

Fourier Transform (DFT)

Let's first look at the formula
DCT transform formula(1) of the Fourier (DFT) transform

FP=\frac {1}{N}\sum_{x=0}^{N-1}\sum_{y=0}^{N-1}P_{x,y}\exp(-j(\frac{2 \pi}{N})(ux+vy))

Amplitude
(2)

w=\sqrt{u^2+v^2}

Where
** u, v represent the spatial frequency, that is, the grayscale gradient, the gradient is a vector derived from the coordinates and the gray value **
** w represents the amplitude amplitude **
** N represents the total number of gray pixels **
** Pxy Represents the grayscale of the current pixel **
** x, y represents the grayscale value of the current pixel**

When looking at image processing, you may be very confused. How can there be a Fourier transform in the Fourier transform of waveform analysis? In the Fourier transform of an image, what is T, and why is there a time domain? With this in mind, let’s first understand the following paragraph.

The frequency of an image is an index that characterizes the intensity of grayscale changes in the image, and is the gradient of grayscale in plane space. The physical meaning of Fourier transform is to transform the gray distribution function of the image into the frequency distribution function of the image, and the inverse Fourier transform is to transform the frequency distribution function of the image into the gray distribution function.

It can be seen that there is no event change in the image, only the pixel changes on the x and y axes, which correspond to the time domain, are called the spatial domain of the image.
Spatial domain (spatial domain) Interpretation: Also known as image space (image space). The space composed of image cells. Directly processing pixel values ​​in image space with length (distance) as the independent variable is called spatial domain processing.
Spatial frequency domain definition: Taking frequency (ie wave number) as the independent variable to describe the characteristics of the image, the spatial change of the pixel value of an image can be decomposed into the linearity of simple oscillatory functions with different amplitudes, spatial frequencies and phases. Superposition, the composition and distribution of various frequency components in the image are called spatial spectrum. This decomposition, processing and analysis of the frequency features of the image is called frequency domain processing or wavenumber domain processing.

initialization definition

#include <stdio.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#define VALUE_MAX 255
#define WIDTH 5
#define HEIGHT 5
#define M_PI 3.1415926535897932
struct Complex_{
    double real;//实部
    double imagin;//虚部
};
typedef struct Complex_ Complex;

random data initialization


int Initdata(double (*src)[WIDTH],int size_w,int size_h){
    srand((int)time(0));
    for(int i=0;i<size_w;i++){
        for(int j=0;j<size_h;j++){
            src[i][j]=rand()%VALUE_MAX;
            printf("%lf ",src[i][j]);
        }
        printf(";\n");
    }
    return 0;
}

2-dimensional Fourier transform function

//2维傅里叶变换函数
int DFT2D(double (*src)[WIDTH],Complex (*dst)[WIDTH],int size_w,int size_h){
    for(int u=0;u<size_w;u++){
        for(int v=0;v<size_h;v++){
            double real=0.0;
            double imagin=0.0;
            for(int i=0;i<size_w;i++){
                for(int j=0;j<size_h;j++){
                    double I=src[i][j];
                    double x=M_PI*2*((double)i*u/(double)size_w+(double)j*v/(double)size_h);
                    real+=cos(x)*I;
                    imagin+=-sin(x)*I;
                }
            }
            dst[u][v].real=real;
            dst[u][v].imagin=imagin;
            if(imagin>=0)
                printf("%lf+%lfj ",real,imagin);
            else
                printf("%lf%lfj ",real,imagin);
        }
        printf(";\n");
    }
    return 0;
}

Inverse Fourier Transform (IDFT)

2-dimensional inverse Fourier transform function

//2维逆傅里叶变换函数
int IDFT2D(Complex (*src)[WIDTH],Complex (*dst)[WIDTH],int size_w,int size_h){
    for(int i=0;i<size_w;i++){
        for(int j=0;j<size_h;j++){
            double real=0.0;
            double imagin=0.0;
            for(int u=0;u<size_w;u++){
                for(int v=0;v<size_h;v++){
                    double R=src[u][v].real;
                    double I=src[u][v].imagin;
                    double x=M_PI*2*((double)i*u/(double)size_w+(double)j*v/(double)size_h);
                    real+=R*cos(x)-I*sin(x);
                    imagin+=I*cos(x)+R*sin(x);
                }
            }
            dst[i][j].real=(1./(size_w*size_h))*real;
            dst[i][j].imagin=(1./(size_w*size_h))*imagin;
            if(imagin>=0)
                printf("%lf+%lfj ",dst[i][j].real,dst[i][j].imagin);
            else
                printf("%lf%lfj ",dst[i][j].real,dst[i][j].imagin);
        }
        printf(";\n");
    }
    return 0;
}

test function

int main() {
    double src[WIDTH][HEIGHT];
    Complex dst[WIDTH][HEIGHT];
    Complex dst_[WIDTH][HEIGHT];
    Initdata(src, WIDTH, HEIGHT);
    printf("\n\n");
    DFT2D(src,dst,WIDTH,HEIGHT);
    printf("\n\n");
    IDFT2D(dst,dst_,WIDTH,HEIGHT);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325049193&siteId=291194637