首页 > 代码库 > 离散傅里叶变换(Discrete Fourier Transform,缩写为DFT)

离散傅里叶变换(Discrete Fourier Transform,缩写为DFT)

核心函数:

cvDFT

程序:

wKiom1PsKPaAobomAAEVHN4ETFs812.jpg

代码:

#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include <iostream>
int DFT(int argc,char** argv)  //离散傅里叶变换(Discrete Fourier Transform,缩写为DFT)
{
IplImage* src=http://www.mamicode.com/cvLoadImage("e:\\picture\\4.jpg",0);
IplImage* Src32F=cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,1);
IplImage* Src32F_one=cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,1);
IplImage* Dst32F=cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,1);
IplImage* Dst32F_one=cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,1);
IplImage* Dst2_32F=cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,1);
IplImage* Dst2_32F_one=cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,1);
IplImage* Src232F=cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,1);
IplImage* dst=cvCreateImage(cvGetSize(src),8,1);
IplImage* src2=cvCreateImage(cvGetSize(src),8,1);
cvZero(Src32F);
cvZero(Dst32F);
//把uchar转换为32F类型,以便于归一化和计算变换
cvConvertScale(src,Src32F);
//归一化处理[0,255]
double MinVal=0;
double MaxVal=0;
cvMinMaxLoc(Src32F,&MinVal,&MaxVal);
double min=MinVal;
double max=MaxVal;
double scale=255/(MaxVal-MinVal);
double shift=-MinVal*scale;
cvConvertScale(Src32F,Src32F_one,scale,shift);
cvMinMaxLoc(Src32F_one,&MinVal,&MaxVal);
std::cout<<MinVal<<MaxVal<<std::endl;
//傅里叶变换
cvDFT(Src32F_one,Dst32F,CV_DXT_FORWARD);
//对傅里叶变换结果进行归一化[0,255]
MinVal=0;
MaxVal=0;
cvMinMaxLoc(Dst32F,&MinVal,&MaxVal);
scale=255/(MaxVal-MinVal);
shift=-MinVal*scale;
cvConvertScale(Dst32F,Dst32F_one,scale,shift);
//把归一化的傅里叶变换结果转换为8位,以便显示
cvConvertScale(Dst32F_one,dst);
cvMinMaxLoc(Dst32F_one,&MinVal,&MaxVal);
std::cout<<"DEF result MinVal:"<<MinVal<<" MaxVal:"<<MaxVal<<std::endl;
//傅里叶逆变换
cvDFT(Dst32F,Dst2_32F,CV_DXT_INVERSE);
//归一化傅里叶逆变换结果[0,255]
MinVal=0;
MaxVal=0;
cvMinMaxLoc(Dst2_32F,&MinVal,&MaxVal);
scale=255/(MaxVal-MinVal);
shift=-MinVal*scale;
cvConvertScale(Dst2_32F,Dst2_32F_one,scale,shift);
//还原源图像,逆归一化
scale=(max-min)/255;         //归一化是先乘后加,还原是先加后乘,所以shift要等于min而不是min*scale
shift=min;
cvConvertScale(Dst2_32F_one,Src232F,scale,shift);
//把还原的图像转换为8位图像
cvConvertScale(Src232F,src2);
cvNamedWindow("src");
cvNamedWindow("dst");
cvNamedWindow("src2");
cvShowImage("src",src);
cvShowImage("dst",dst);
cvShowImage("src2",src2);
cvWaitKey(0);
cvDestroyWindow("src");
cvDestroyWindow("dst");
cvDestroyWindow("src2");
cvReleaseImage(&src);
cvReleaseImage(&dst);
cvReleaseImage(&Src32F);
cvReleaseImage(&Dst32F);
cvReleaseImage(&src2);
cvReleaseImage(&Src32F_one);
cvReleaseImage(&Dst32F_one);
cvReleaseImage(&Dst2_32F_one);
return 0;
}


本文出自 “flyclc” 博客,请务必保留此出处http://flyclc.blog.51cto.com/1385758/1539811