Opencv C++ 離散傅里葉變換

Opencv C++ 離散傅里葉變換

輸入圖片下載地址:
鏈接: https://pan.baidu.com/s/1KAVNvCRuR8KY2dseU5eaBQ 提取碼: 1axt

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

using namespace std;
using namespace cv;

int main()
{
	//1.讀取並顯示圖片
	Mat img_src = imread("../Data/5_5_image.jpg", IMREAD_GRAYSCALE);
	imshow("img_src", img_src);

	//2.輸入圖片擴充尺寸
	int m = getOptimalDFTSize(img_src.rows);
	int n = getOptimalDFTSize(img_src.cols);

	Mat img_padded;
	copyMakeBorder(img_src, img_padded, 0, m - img_src.rows, 0, n - img_src.cols, BORDER_CONSTANT, Scalar::all(0));
	cout << "擴充圖片 高度:  " << img_padded.rows << "   寬度:" << img_padded.cols;

	//3.爲傅里葉變換結果分配存儲空間
	Mat planes[] = { Mat_<float>(img_padded), Mat::zeros(img_padded.size(), CV_32F) };
	Mat complexI;
	merge(planes, 2, complexI);

	//4.執行傅里葉變換
	dft(complexI, complexI);

	//5.將複數轉換爲幅值
	split(complexI, planes);
	magnitude(planes[0], planes[1], planes[0]);
	Mat magnitudeImage = planes[0];

	//6.進行對數尺度縮放
	magnitudeImage += Scalar::all(1);
	log(magnitudeImage, magnitudeImage);  //自然對數

	//7.重新分佈幅度圖像象限
	magnitudeImage = magnitudeImage(Rect(0, 0, magnitudeImage.cols & -2, magnitudeImage.rows & -2));
	int cx = magnitudeImage.cols / 2;
	int cy = magnitudeImage.rows / 2;

	//象限分割
	Mat q0(magnitudeImage, Rect(0, 0, cx, cy));
	Mat q1(magnitudeImage, Rect(cx, 0, cx, cy));
	Mat q2(magnitudeImage, Rect(0, cy, cx, cy));
	Mat q3(magnitudeImage, Rect(cx, cy, cx, cy));

	//象限調整
	Mat temp;
	q0.copyTo(temp);
	q3.copyTo(q0);
	temp.copyTo(q3);

	q1.copyTo(temp);
	q2.copyTo(q1);
	temp.copyTo(q2);

	//8.歸一化顯示
	normalize(magnitudeImage, magnitudeImage, 0, 1, NORM_MINMAX);

	//9.顯示頻譜圖片
	imshow("頻譜幅值", magnitudeImage);

	waitKey();
	destroyAllWindows();
	
	return 0;
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章