opencv dft例程

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

#include "stdafx.h"
#include <opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;


//imshow("1", src);
//
//int main()
//{
//	//扩大图像为适合尺寸
//	int m = getOptimalDFTSize(src.rows);
//	int n = getOptimalDFTSize(src.cols);
//	//为此增添的元素用0初始化
//	Mat padded;
//	copyMakeBorder(src, padded, 0, m - src.rows, 0, n - src.cols, BORDER_CONSTANT, Scalar::all(0);
//	//为傅里叶变换的结果(实部和虚部)分配空间
//	Mat planes[] = { Mat_<float>(padded), Mat::zeros(padded.size(),CV_32F) };
//	Mat complexI;
//	merge(planes, 2, complexI);  //planes数组合并为一个多通道的数组
//	//此处为就地计算模式(in-place),即输入 输出为同一图像
//	dft(complexI, complexI);
//	//将复数化为幅值
//	split(complexI, planes);  //将多通道complexI分解为多个单通道
//	planes[0] = Re(DFT(I), planes[1] = Im(DFT(I)), magnitude(planes[0] ,planes[1], planes[0]))
//}
//
//	waitKey(0);
//    return 0;
//}
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

#include <stdio.h>

using namespace cv;
using namespace std;
static void help()
{
	printf("\nThis program demonstrated the use of the discrete Fourier transform (dft)\n"
		"The dft of an image is taken and it's power spectrum is displayed.\n"
		"Usage:\n"
		"./dft [image_name -- default lena.jpg]\n");
}

const char* keys =
{
	"{1| |lena.jpg|input image file}"
};

int main(int argc, const char ** argv)
{
	help();
	CommandLineParser parser(argc, argv, keys);        // opencv中用来处理命令行参数的类
	string filename = "G:\\picture\\又壁纸\\57.jpg";

	Mat img = imread(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);    //以灰度图像读入
	if (img.empty())
	{
		help();
		printf("Cannot read image file: %s\n", filename.c_str());
		return -1;
	}
	int M = getOptimalDFTSize(img.rows);                               // 获得最佳DFT尺寸,为2的次方
	int N = getOptimalDFTSize(img.cols);                                 //同上
	Mat padded;
	copyMakeBorder(img, padded, 0, M - img.rows, 0, N - img.cols, BORDER_CONSTANT, Scalar::all(0));   // opencv中的边界扩展函数,提供多种方式扩展

	Mat planes[] = { Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F) };          // Mat 数组,第一个为扩展后的图像,一个为空图像,
	Mat complexImg;
	merge(planes, 2, complexImg);                                                                              // 合并成一个Mat

	dft(complexImg, complexImg);                                                                              // FFT变换, dft需要一个2通道的Mat

																											  // compute log(1 + sqrt(Re(DFT(img))**2 + Im(DFT(img))**2))
	split(complexImg, planes);                                                                                     //分离通道, planes[0] 为实数部分,planes[1]为虚数部分
	magnitude(planes[0], planes[1], planes[0]);                                                          // 求模
	Mat mag = planes[0];
	mag += Scalar::all(1);
	log(mag, mag);                                                                                                      // 模的对数

																														// crop the spectrum, if it has an odd number of rows or columns
	mag = mag(Rect(0, 0, mag.cols & -2, mag.rows & -2));                                        //保证偶数的边长

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

	// rearrange the quadrants of Fourier image                                                        //对傅立叶变换的图像进行重排,4个区块,从左到右,从上到下 分别为q0, q1, q2, q3
	// so that the origin is at the image center                                                          //  对调q0和q3, q1和q2
	Mat tmp;
	Mat q0(mag, Rect(0, 0, cx, cy));
	Mat q1(mag, Rect(cx, 0, cx, cy));
	Mat q2(mag, Rect(0, cy, cx, cy));
	Mat q3(mag, Rect(cx, cy, cx, cy));

	q0.copyTo(tmp);
	q3.copyTo(q0);
	tmp.copyTo(q3);

	q1.copyTo(tmp);
	q2.copyTo(q1);
	tmp.copyTo(q2);

	normalize(mag, mag, 0, 1, CV_MINMAX);                                                           // 规范化值到 0~1 显示图片的需要

	imshow("spectrum magnitude", mag);
	waitKey();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值