#include <stdio.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
void fft2(IplImage *src, IplImage *dst)
{
IplImage *image_Re = 0, *image_Im = 0, *Fourier = 0;
image_Re = cvCreateImage(cvGetSize(src), IPL_DEPTH_64F, 1);
image_Im = cvCreateImage(cvGetSize(src), IPL_DEPTH_64F, 1);
Fourier = cvCreateImage(cvGetSize(src), IPL_DEPTH_64F, 2);
cvConvertScale(src, image_Re, 1, 0);
cvZero(image_Im);
cvMerge(image_Re, image_Im, 0, 0, Fourier);
cvDFT(Fourier, dst, CV_DXT_FORWARD);
cvReleaseImage(&image_Re);
cvReleaseImage(&image_Im);
cvReleaseImage(&Fourier);
}
void fft2shift(IplImage *src, IplImage *dst)
{
IplImage *image_Re = 0, *image_Im = 0;
int nRow, nCol, i, j, cy, cx;
double scale, shift, tmp13, tmp24;
image_Re = cvCreateImage(cvGetSize(src), IPL_DEPTH_64F, 1);
image_Im = cvCreateImage(cvGetSize(src), IPL_DEPTH_64F, 1);
cvSplit( src, image_Re, image_Im, 0, 0 );
cvPow( image_Re, image_Re, 2.0);
cvPow( image_Im, image_Im, 2.0);
cvAdd( image_Re, image_Im, image_Re);
cvPow( image_Re, image_Re, 0.5 );
cvAddS( image_Re, cvScalar(1.0), image_Re );
cvLog( image_Re, image_Re );
nRow = src->height;
nCol = src->width;
cy = nRow/2;
cx = nCol/2;
for( j = 0; j < cy; j++ ){
for( i = 0; i < cx; i++ ){
tmp13 = CV_IMAGE_ELEM( image_Re, double, j, i);
CV_IMAGE_ELEM( image_Re, double, j, i) = CV_IMAGE_ELEM(
image_Re, double, j+cy, i+cx);
CV_IMAGE_ELEM( image_Re, double, j+cy, i+cx) = tmp13;
tmp24 = CV_IMAGE_ELEM( image_Re, double, j, i+cx);
CV_IMAGE_ELEM( image_Re, double, j, i+cx) =
CV_IMAGE_ELEM( image_Re, double, j+cy, i);
CV_IMAGE_ELEM( image_Re, double, j+cy, i) = tmp24;
}
}
double minVal = 0, maxVal = 0;
cvMinMaxLoc( image_Re, &minVal, &maxVal );
scale = 255/(maxVal - minVal);
shift = -minVal * scale;
cvConvertScale(image_Re, dst, scale, shift);
cvReleaseImage(&image_Re);
cvReleaseImage(&image_Im);
}
int main()
{
IplImage *src;
IplImage *Fourier;
IplImage *dst ;
IplImage *ImageRe;
IplImage *ImageIm;
IplImage *Image;
IplImage *ImageDst;
double m,M;
double scale;
double shift;
src = cvLoadImage("D:\main.jpg",0);
Fourier = cvCreateImage(cvGetSize(src),IPL_DEPTH_64F,2);
dst = cvCreateImage(cvGetSize(src),IPL_DEPTH_64F,2);
ImageRe = cvCreateImage(cvGetSize(src),IPL_DEPTH_64F,1);
ImageIm = cvCreateImage(cvGetSize(src),IPL_DEPTH_64F,1);
Image = cvCreateImage(cvGetSize(src),src->depth,src->nChannels);
ImageDst = cvCreateImage(cvGetSize(src),src->depth,src->nChannels);
fft2(src,Fourier);
fft2shift(Fourier, Image);
cvDFT(Fourier,dst,CV_DXT_INV_SCALE);
cvSplit(dst,ImageRe,ImageIm,0,0);
cvNamedWindow("源图像",0);
cvShowImage("源图像",src);
cvPow(ImageRe,ImageRe,2);
cvPow(ImageIm,ImageIm,2);
cvAdd(ImageRe,ImageIm,ImageRe,NULL);
cvPow(ImageRe,ImageRe,0.5);
cvMinMaxLoc(ImageRe,&m,&M,NULL,NULL);
scale = 255/(M - m);
shift = -m * scale;
cvConvertScale(ImageRe,ImageDst,scale,shift);
cvNamedWindow("傅里叶谱",0);
cvShowImage("傅里叶谱",Image);
cvNamedWindow("傅里叶逆变换",0);
cvShowImage("傅里叶逆变换",ImageDst);
cvWaitKey(10000);
cvReleaseImage(&src);
cvReleaseImage(&Image);
cvReleaseImage(&ImageIm);
cvReleaseImage(&ImageRe);
cvReleaseImage(&Fourier);
cvReleaseImage(&dst);
cvReleaseImage(&ImageDst);
return 0;
}
转载:http://blog.csdn.net/abcjennifer/article/details/7359952