我想用C ++语言实现DFT来处理图像。 当我研究这个理论时,我知道,我可以将2D DFT分成两个一维DFT部分。首先,对于我执行1D DFT的每一行,然后我为每列执行此操作。当然,我应该对复杂的数字进行操作。 这里出现了一些问题,因为我不确定在哪里使用真实,以及虚数部分的复数。我找到了某个地方,输入图像像素的值我应该视为实部,而虚部设置为0。 我做了一个实现,但我想结果图像是不正确的。 如果有人可以帮助我,我将不胜感激。 为了阅读和保存图像,我使用CImg库。
void DFT (CImg<unsigned char> image)
{
int w=512;
int h=512;
int rgb=3;
complex <double> ***obrazek=new complex <double>**[w];
for (int b=0;b<w;b++) //making 3-dimensional table to store DFT values
{
obrazek[b]=new complex <double>*[h];
for (int a=0;a<h;a++)
{
obrazek[b][a]=new complex <double>[rgb];
}
}
CImg<unsigned char> kopia(image.width(),image.height(),1,3,0);
complex<double> sum=0;
complex<double> sum2=0;
double pi = 3.14;
for (int i=0; i<512; i++){
for (int j=0; j<512; j++){
for (int c=0; c<3; c++){
complex<double> cplx(image(i,j,c), 0);
obrazek[i][j][c]=cplx;
}}}
for (int c=0; c<3; c++) //for rows
{
for (int y=0; y<512; y++)
{
sum=0;
for (int x=0; x<512; x++)
{
sum+=(obrazek[x][y][c].real())*cos((2*pi*x*y)/512)-(obrazek[x][y][c].imag())*sin((2*pi*x*y)/512);
obrazek[x][y][c]=sum;
}
}
}
for (int c=0; c<3; c++) //for columns
{
for (int y=0; y<512; y++)//r
{
sum2=0;
for (int x=0; x<512; x++)
{
sum2+=(obrazek[y][x][c].real())*cos((2*pi*x*y)/512)-(obrazek[y][x][c].imag())*sin((2*pi*x*y)/512);
obrazek[y][x][c]=sum2;
}
}
}
for (int i=0; i<512; i++){
for (int j=0; j<512; j++){
for (int c=0; c<3; c++){
kopia(i,j,c)=obrazek[i][j][c].real();
}}}
CImgDisplay image_disp(kopia,"dft");
while (!image_disp.is_closed() )
{
image_disp.wait();
}
saving(kopia);
}