#include "highgui.h"
#include "stdio.h"
#include "stdlib.h"
void fft2shift(IplImage* src, IplImage* dst)
{
IplImage *i, *r;
int height, width, k, j, cx, cy;
double temp1, temp2;
double min, max;
double base1, base2;
i = cvCreateImage(cvGetSize(src), IPL_DEPTH_64F, 1);
r = cvCreateImage(cvGetSize(src), IPL_DEPTH_64F, 1);
cvSplit(src, r, i, NULL, NULL);
cvPow(i, i, 2);
cvPow(r, r, 2);
cvAdd(r, i, r);
cvAddS(r, cvScalar(1.0), r); //对数变换扩展图像的动态范围
cvLog(r,r);
height = src->height;
width = src->width;
cx = width / 2;
cy = height / 2;
for (j = 0; j < cy;j++) //中心化
for (k = 0; k < cx; k++)
{
temp1 = CV_IMAGE_ELEM(r, double, j, k);
CV_IMAGE_ELEM(r, double, j, k) = CV_IMAGE_ELEM(r, double, j+cy, k+cx);
CV_IMAGE_ELEM(r, double, j + cy, k + cx) = temp1;
temp2 = CV_IMAGE_ELEM(r, double, j, k+cx);
CV_IMAGE_ELEM(r, double, j, k+cx) = CV_IMAGE_ELEM(r, double, j + cy, k);
CV_IMAGE_ELEM(r, double, j + cy, k ) = temp2;
}
cvMinMaxLoc(r, &min, &max); //归一化
base1 = 255 / (max - min);
base2 = -min*base1;
cvConvertScale(r, dst, base1, base2);
cvReleaseImage(&r);
cvReleaseImage(&i);
}
int main()
{
IplImage *src, *temp, *Fourier,*InverseFourier,*i,*r,*F1,*F2,*F3;
temp = cvLoadImage("D:\\lena1.jpg", CV_LOAD_IMAGE_GRAYSCALE);
cvNamedWindow("原始图像", CV_WINDOW_AUTOSIZE);
cvNamedWindow("傅里叶变换图像", CV_WINDOW_AUTOSIZE);
cvNamedWindow("中心化傅里叶变化图像", CV_WINDOW_AUTOSIZE);
cvNamedWindow("傅里叶逆变换图像", CV_WINDOW_AUTOSIZE);
cvShowImage("原始图像",temp);
src=cvCreateImage(cvGetSize(temp),IPL_DEPTH_64F, 2);
i = cvCreateImage(cvGetSize(temp), IPL_DEPTH_64F, 1);
r = cvCreateImage(cvGetSize(temp), IPL_DEPTH_64F, 1);
Fourier = cvCreateImage(cvGetSize(temp), IPL_DEPTH_64F, 2);
InverseFourier = cvCreateImage(cvGetSize(temp), IPL_DEPTH_64F, 2);
F1 = cvCreateImage(cvGetSize(temp), temp->depth, 1);
F2 = cvCreateImage(cvGetSize(temp), temp->depth, 1);
F3 = cvCreateImage(cvGetSize(temp), temp->depth, 1);
cvConvertScale(temp, r); //保留实部
cvZero(i); //去除虚部
cvMerge(r, i, NULL, NULL, src); //重新合并成只保留原图像实部的图像
cvDFT(src, Fourier, CV_DXT_FORWARD);
cvSplit(Fourier, r, i, NULL, NULL);
cvConvertScale(r, F1);
cvShowImage("傅里叶变换图像", F1);
cvDFT(Fourier, InverseFourier, CV_DXT_INV_SCALE);
cvSplit(InverseFourier, r, i, NULL, NULL);
cvConvertScale(r, F3);
cvShowImage("傅里叶逆变换图像", F3);
fft2shift(Fourier, F2);
cvShowImage("中心化傅里叶变化图像", F2);
cvWaitKey(0);
*src, *temp, *Fourier, *InverseFourier, *i, *r, *F1, *F2, *F3;
cvReleaseImage(&src);
cvReleaseImage(&temp);
cvReleaseImage(&Fourier);
cvReleaseImage(&InverseFourier);
cvReleaseImage(&i);
cvReleaseImage(&r);
cvReleaseImage(&F1);
cvReleaseImage(&F2);
cvReleaseImage(&F3);
cvDestroyAllWindows();
}