dft

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <sstream>

using namespace std;
using namespace cv;


int main(int argc, char* argv[]) {

	const char* filename = "tiger.jpg";
	Mat I = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
	if (I.empty()) {

		return -1;
	}


	// 将图像延扩到最佳尺寸-----------------------------------------------------------------------
	Mat padded;
	int m = getOptimalDFTSize(I.rows);
	int n = getOptimalDFTSize(I.cols);

	
	copyMakeBorder(I, padded, 0, m - I.rows, 0, I.cols,   // on the border add zero values
		BORDER_CONSTANT, Scalar::all(0));

	Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};  // Add to the expanded another plane with zeros
    Mat complexI;
	merge(planes, 2,  complexI);



    // 进行离散傅立叶变换---------------------------------------------------------------------------
	dft(complexI, complexI);



	// 将复数转换为幅度-----------------------------------------------------------------------------
	split(complexI, planes);                    // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))	
	magnitude(planes[0], planes[1], planes[0]); // *=> log(1 + sqrt(Re(DFT(I))^2 + Im(DFT(I))^2))
    Mat magI = planes[0];                        	

    

	// 对数尺度缩放----------------------------------------------------------------------------------
	magI += Scalar::all(1);
	log(magI, magI);


 
	// 剪切和重分布幅度图象限------------------------------------------------------------------------
    magI = magI(Rect(0, 0, magI.cols & -2, magI.rows & -2));

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

	Mat q0(magI, Rect(0, 0, cx, cy));   // Top-Left -为每一个象限创建ROI
	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

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

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




	// 为了显示,归一化,float[0, 1]--------------------------------------------------------------------
	normalize(magI, magI, 0, 1, CV_MINMAX);



	imshow("Input Image", I);
	imshow("spectrum magnitude", magI);
 
	waitKey(0);

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值