实验环境:
1,Linux操作系统
2,QT3编程开发环境
3,C++编程语言
傅立叶变换和傅立叶反 变换
1.1. 主要源代码
readImage() 从图像中读取数据
writeImage() 往图像中写入数据
fft() 快速傅立叶变换
ifft() 快速傅立叶反变换
adjustImageSize() 调整图像大小
fourier() 傅立叶变换
ifourier() 傅立叶反变换
1.1.1 从图像中读取数据
void ImageProcess::readImage(complex<double> data[], const QImage &srcImage)
{
byte *pImageBytes = srcImage.bits(); //数据首地址
int depth = srcImage.depth(); //每个像素的bit数
int lineBytes = srcImage.bytesPerLine(); //每行的字节数
int w = srcImage.width(); //宽
int h = srcImage.height(); //高
byte *pByte;
//遍历读取每个像素,并转换为灰度值
int i, j;
for(i = 0; i < h; i++)
{
for(j = 0; j < w; j++)
{
if(8 == depth) //采用了256色调色板,8位颜色索引
{
pByte = pImageBytes + i * lineBytes + j;
data[i * w + j] = complex<double>( *pByte, 0);
}
else if(32 == depth)//32位表示,数据格式为0xFFBBGGRR或0xAABBGGRR
{
pByte = pImageBytes + i * lineBytes + j * 4;
//根据RGB模式转化成YIQ色彩模式的方式,取Y作为灰 度值
byte pixelValue = (byte)(0.299 * (float)pByte[0] + 0.587 * (float)pByte[1]
+ 0.114 * (float)pByte[2]);
data[i * w + j] = complex<double>( pixelValue, 0);
}
else
{
cout << "invalid format. depth = " << depth << "/n";
return;
}
}
}
}
1.1.2 将数据写入图像
//coef为比例系数,主要用来调整灰度值以便于观察
void ImageProcess::writeImage(QImage &destImage, const complex<double> data[], double coef)
{
int lineBytes = destImage.bytesPerLine();
int depth = destImage.depth();
int w = destImage.width();
int h = destImage.height();
byte *pImageBytes = destImage.bits();
byte *pByte;
for(int i = 0; i < h; i++)
{
for(int j = 0; j < w; j++)
{
double spectral = abs(data[i * w + j]) * coef; //灰度值调整
spectral = spectral > 255 ? 255 : spectral;
//根据图像格式写数据
if(8 == depth)
{
pByte = pImageBytes + i * lineBytes + j;
*pByte = spectral;
}
else if(32 == depth)
{
pByte = pImageBytes + i * lineBytes + j * 4;
pByte[0] = pByte[1] = pByte[2] = spectral;
}
else
{
return;
}
}
}
}
1.1.3 递归形式的快速傅立叶变换
// 数组a为输入,数组y为输出,2的power次方为数组的长度
void ImageProcess::fft(const complex<double> a[], complex<double> y[], int power)
{
if(0 == power)
{
y[0] = a[0];
return;
}
int n = 1 << power;
double angle = 2 * PI / n;
complex<double> wn(cos(angle), sin(angle));
complex<double> w(1, 0);
complex<double> *a0 = new complex<double>[n / 2];
complex<double> *a1 = new complex<double>[n / 2];
complex<double> *y0 = new complex<double>[n / 2];
complex<double> *y1 = new complex<double>[n / 2];
for(int i = 0; i < n / 2; i ++)
{
a0[i] = a[2 * i];
a1[i] = a[2 * i + 1];
}
//分开成两个子fft过程
fft(a0, y0, power - 1);
fft(a1, y1, power - 1);
complex<double> u;
for(int k = 0; k < n / 2; k++) //蝶形算法
{
u = w * y1[k];
y[k] = y0[k] +