没有合适的资源?快使用搜索试试~ 我知道了~
QT实现图像处理-傅立叶变换、傅立叶反变换
4星 · 超过85%的资源 需积分: 41 74 下载量 94 浏览量
2012-03-30
16:52:23
上传
评论 4
收藏 234KB DOC 举报
温馨提示
试读
16页
QT实现图像处理-傅立叶变换、傅立叶反变换。readImage() 从图像中读取数据 writeImage() 往图像中写入数据 fft() 快速傅立叶变换 ifft() 快速傅立叶反变换 adjustImageSize() 调整图像大小
资源推荐
资源详情
资源评论
QT 实现图像处理-傅立叶变换、傅立叶反变换、平滑、锐化与模板匹配
实验环境:
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] + u;
y[k + n / 2] = y0[k] - u;
w = w * wn;
}
delete[] a0;
delete[] a1;
delete[] y0;
delete[] y1;
}
1.1.4 快速傅立叶反变换
//y 为输入,a 为输出,2 的 power 次方为数组的长度
void ImageProcess::ifft(const complex<double> y[], complex<double> a[], int power)
{
int count = 1 << power;
complex<double> *x = new complex<double>[count];
memcpy(x, y, sizeof(complex<double>) * count);
int i;
for(i = 0; i < count; i++)
{
x[i] = complex<double>(x[i].real(), -x[i].imag()); //共轭复数
}
fft(x, a, power); //调用快速傅立叶变换算法
for(i = 0; i < count; i++)
{
a[i] = complex<double>(a[i].real() / count, -a[i].imag() / count); //共轭复数
}
剩余15页未读,继续阅读
资源评论
- 春化雨2014-04-28资源很有参考价值,值得推荐
- 楼阁之束2020-01-02坑人,代码随处都能找到
- q30200254702012-04-18word文档里面只给出了部分关键的函数,不过说明还是比较清楚的。 实验环境:1,Linux操作系统2,QT3编程开发环境3,C++编程语言
- 鱼公子哟西2016-04-26资源提供了部分函数的实现,表述清晰
- bailiju2013-04-28资料还不错,有些参考价值,收藏。
yanzi1225627
- 粉丝: 7768
- 资源: 128
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功