傅立叶变换与逆变换 上

在opencv官网上有傅立叶变换的实例,但其没写怎么变换回来,所以这里将其逆表示出来

代码如下:

// opencv_dft.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <OpenCV245.h>

using namespace std;
using namespace cv;


int _tmain(int argc, _TCHAR* argv[])
{
	Mat src;
	Mat Image = imread("C:\\Users\\sony\\Desktop\\111.png", 0);
	if (Image.empty())
	  return -1;

	Mat padded;
	int m = getOptimalDFTSize(Image.rows);
	int n = getOptimalDFTSize(Image.cols);
	copyMakeBorder(Image, padded, 0, m - Image.rows, 0, n - Image.cols, BORDER_CONSTANT, Scalar::all(0));

	Mat planes[] = {Mat_<float>(padded),  Mat::zeros(padded.size(), CV_32F)};
	Mat iplanes[] = {Mat_<float>(padded),  Mat::zeros(padded.size(), CV_32F)};
	Mat complexI;
	merge(planes, 2, complexI);

	dft(complexI, complexI);
	dft(complexI, src, DFT_SCALE|DFT_INVERSE);
	
	
	split(complexI, planes);
	magnitude(planes[0], planes[1], planes[0]);
	Mat magI = planes[0];

	split(src, iplanes);
	magnitude(iplanes[0], iplanes[1], iplanes[0]);
	Mat imagI = iplanes[0];

	magI += Scalar::all(1);
	log(magI, magI);

	magI = magI(Rect(0, 0, magI.cols & -2, magI.rows & -2));

	

	Mat O = magI.clone();

	int cx = magI.cols/2;
	int cy = magI.rows/2;

	Mat q0(magI, Rect(0, 0, cx, cy));   // Top-Left - Create a ROI per quadrant 
	Mat q1(magI, Rect(cx, 0, cx, cy));  // Top-Right
	Mat q2(magI, Rect(0, cy, cx, cy));  // Bottom-Left
	Mat q3(magI, Rect(cx, cy, cx, cy)); // Bottom-Right

	Mat tmp;                           // swap quadrants (Top-Left with Bottom-Right)
	q0.copyTo(tmp);
	q3.copyTo(q0);
	tmp.copyTo(q3);

	q1.copyTo(tmp);                    // swap quadrant (Top-Right with Bottom-Left)
	q2.copyTo(q1);
	tmp.copyTo(q2);

	normalize(magI, magI, 0, 1, CV_MINMAX);
	normalize(O, O, 0, 1, CV_MINMAX);
	normalize(imagI, imagI, 0, 1, CV_MINMAX);
	

	imshow("input Image", Image);
	imshow("spectrum magnitude", magI);
	imshow("0", O);
	/*cout<<src<<endl;*/
	imshow("src",imagI);
	waitKey(0);


	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值