http://wenku.baidu.com/view/f8c8d5c62cc58bd63186bda0.html
/*< xmlnamespace prefix ="o" ns ="urn:schemas-microsoft-com:office:office" />
opencv的傅裡葉變換及逆變換實現。傅裡葉變換需要將數據表示為復數形式,通過一個兩通道矩陣
來記錄復數的實部和虛部,然後通過cvDFT來實現變換。對於圖片變換也是一樣,只是矩陣換成IplImage
格式,用兩個單通道圖片來分別表示實部和虛部,用兩通道圖片來存放變換結果。
*/
#include<iostream>
#include<cv.h>
#include<cxcore.h>
#include<highgui.h>
usingnamespace std;
int main(int argc,char*argv[])
{
float r[] = {23,23,214,214,32,53};
float i[] = {22,32,33,42,31,12};
CvMat *re = cvCreateMat(1,6,CV_32FC1);//實部
CvMat *im = cvCreateMat(1,6,CV_32FC1);//虛部
CvMat *re_dst = cvCreateMat(1,6,CV_32FC1);//變換後實部
CvMat *im_dst = cvCreateMat(1,6,CV_32FC1);//變換後虛部
CvMat *re_ = cvCreateMat(1,6,CV_32FC1);//反變換後實部
CvMat *im_ = cvCreateMat(1,6,CV_32FC1);//反變換後虛部
cvInitMatHeader(re, 1, 6, CV_32FC1, r); //初始化實部
cvInitMatHeader(im, 1, 6, CV_32FC1, i); //初始化虛部
CvMat *sum_src = cvCreateMat(1,6,CV_32FC2);//實部虛部結合體
CvMat *sum_dst = cvCreateMat(1,6,CV_32FC2);//變換後實部虛部結合體
CvMat *sum_ = cvCreateMat(1,6,CV_32FC2);//反變換後實部虛部結合體
cout<<"原始數據:"<<endl;
cout<<"////////////實部/////////////"<<endl;
for(int j = 0; j < re->cols; j++)
{
cout << cvmGet(re,0,j)<<endl;
}
cout<<"////////////虛部/////////////"<<endl;
for(int i = 0; i < im->cols; i++)
{
cout<<cvmGet(im,0,i)<<endl;
}
cvMerge(re,im,0,0,sum_src);
cvDFT(sum_src,sum_dst,CV_DXT_FORWARD,0);
cvSplit(sum_dst,re_dst,im_dst,0,0);
cout<<"////////////實部/////////////"<<endl;
for(int a = 0; a < re_dst->cols; a++)
{
cout<<cvmGet(re_dst,0,a)<<endl;
}
cout<<"////////////虛部/////////////"<<endl;
for(int b = 0; b < im_dst->cols; b++)
{
cout<<cvmGet(im_dst,0,b)<<endl;
}
cvDFT(sum_dst,sum_,CV_DXT_INV_SCALE,0);
cvSplit(sum_,re_,im_,0,0);
cout<<endl;
cout<<"反變換:"<<endl;
cout<<"////////////實部/////////////"<<endl;
for(int c = 0; c < re_->cols; c++)
{
cout<<cvmGet(re_,0,c)<<endl;
}
cout<<"////////////虛部/////////////"<<endl;
for(int d = 0; d < im_->cols; d++)
{
cout<<cvmGet(im_,0,d)<<endl;
}
}