opencv c++ 实现傅里叶变换

opencv c++ 实现傅里叶变换

Mat src = imread("D:/pictures/photoman.png",0);
	int row = src.rows;
	int col = src.cols;
	//计算该图像傅里叶变换的最适合尺寸
	int m = getOptimalDFTSize(src.rows);
	int n = getOptimalDFTSize(src.cols);
	imshow("input", src);
	Mat padded;
	//填充边界至最适合尺寸
    copyMakeBorder(src, padded, 0, m - row, 0, n - col, BORDER_CONSTANT, Scalar::all(0));
	
	cout << padded.size() << padded.channels() << endl;

	Mat planne[] = { Mat_(padded),Mat::zeros(padded.size(),CV_32F) };

	Mat addImg;
	//merge合并通道
	merge(planne, 2, addImg);
	cout << addImg.size() << planne->size() << endl;

	dft(addImg, addImg);
	split(addImg, planne);
	//magnitude计算二维矢量的幅值
	magnitude(planne[0], planne[1], planne[0]);


	Mat magnImg = planne[0];
	//对数尺度便于展示
	magnImg += Scalar::all(1);
	log(magnImg, magnImg);


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

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

	Mat q0(magnImg, Rect(0,0,cx,cy));
	Mat q1(magnImg, Rect(cx, 0, cx, cy));
	Mat q2(magnImg, Rect(0, cy, cx, cy));
	Mat q3(magnImg, 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);

	normalize(magnImg, magnImg, 0, 1, NORM_MINMAX);
	imshow("频谱幅值", magnImg);

你可能感兴趣的:(c++,qt,opencv,opencv,c++)