DFT

#include <iostream>
#include <opencv2/opencv.hpp>
#include <sstream>
using namespace std;
using namespace  cv;
int main()
{
	string filename = "C:/Users/Administrator/Desktop/标准测试图片/dota/big432000.jpg";
	Mat img = imread(filename, IMREAD_GRAYSCALE);
	if (img.empty())
	{
		return -1;
	}
	int cols = getOptimalDFTSize(img.cols);
	int rows = getOptimalDFTSize(img.rows);
	//复制制作边界
	Mat padded;
	copyMakeBorder(img, padded, 0, rows - img.rows, 0, cols - img.cols, BORDER_CONSTANT, Scalar::all(0));
	Mat planes[] = { Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F) };
	//进行傅里叶变换
	Mat ComplexImg;
	merge(planes,2, ComplexImg);
	dft(ComplexImg, ComplexImg);
	//log(x+1) 进行变换
	split(ComplexImg, planes);
	magnitude(planes[0], planes[1], planes[0]);
	Mat mag = planes[0];
	mag += Scalar::all(1);
	log(mag, mag);
	mag = mag(Rect(0, 0, mag.cols&-2, mag.rows&-2));
	int cx = mag.cols / 2;
	int cy = mag.rows / 2;
	Mat temp;
	Mat q1 = mag(Rect(0, 0, cx, cy));
	Mat q2 = mag(Rect(cx, 0, cx, cy));
	Mat q3 = mag(Rect(0, cy, cx, cy));
	Mat q4 = mag(Rect(cx, cy, cx, cy));

	q1.copyTo(temp);
	q4.copyTo(q1);
	temp.copyTo(q4);

	q2.copyTo(temp);
	q3.copyTo(q2);
	temp.copyTo(q3);

	//归一化结果
	normalize(mag,mag, 0, 1, NORM_MINMAX);
	//现在可以看看变换之后的结果
	imshow("DFT", mag);
	waitKey(0);
  	return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值