这个链接里关于傅里叶讲的非常易懂:https://zhuanlan.zhihu.com/p/19763358(知乎)
傅立叶变换将图像分解成其正弦和余弦组件。换句话说,它将图像从其空间域转换为其频率域。其思想是,任何函数都可以与无限正弦和余弦函数的总和精确近似。傅立叶转型是一种如何做到这一点的方法。从数学上讲,傅立叶变换的二维图像是:
此处 f 是其空间域中的图像值,其频率域中的 F 是 F。转换的结果是复数。可通过真实图像和复杂图像或通过量级和相位图像显示。然而,在整个图像处理算法中,只有量级图像是有趣的,因为它包含了我们需要的关于图像几何结构的所有信息。但是,如果您打算对这些窗体中的图像进行一些修改,然后需要对其进行重新转换,则需要保留这两种图像。
在本示例中,我将演示如何计算和显示傅立叶变换的幅度图像。在数字图像的情况下是离散的。这意味着它们可能会从给定的域值中获取值。例如,在基本灰度图像值中,通常介于 0 和 255 之间。因此,傅立叶变换也需要是离散类型,从而产生离散傅立叶变换 (DFT)。每当需要从几何角度确定图像的结构时,都需要使用它。下面是要执行的步骤(在灰度输入图像 I 的情况下):
1、扩展图像到最佳尺寸
DFT 的性能取决于图像大小。对于数字 2、3 和 5 的倍数,它往往是最快的。因此,要实现最大性能,通常最好将边框值填充到图像上,以获得具有此类特征的大小。getOptimalDFTSize() 返回此最佳大小,我们可以使用 copyMakeBorder() 函数扩展图像的边框(附加像素初始化为零):
Mat padded; //expand input image to optimal size
int m = getOptimalDFTSize( I.rows );
int n = getOptimalDFTSize( I.cols ); // on the border add zero values
copyMakeBorder(I, padded, 0, m - I.rows, 0, n - I.cols, BORDER_CONSTANT, Scalar::all(0));
2、为复杂值和实际值腾出位置
傅立叶变换的结果是复数。这意味着每个图像值的结果为两个图像值(每个组成部分一个,实数部分、复数部分)。此外,频率域范围远远大于其空间对应范围。因此,我们通常至少以float格式存储这些格式。因此,我们将输入图像转换为此类型,并用另一个通道展开它以保存复数值:
Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};
Mat complexI;
merge(planes, 2, complexI); // Add to the expanded another plane with zeros
3、离散傅里叶变换
可以就地计算(与输出相同的输入):
dft(complexI, complexI); // this way the result may fit in the source matrix
4、求实部和虚部的大小
复数具有实数(Re)和复数(想象 - Im)部分。DFT 的结果是复数。DFT 的大小为:
转换成opencv代码:
split(complexI, planes); // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
magnitude(planes[0], planes[1], planes[0]);// planes[0] = magnitude
Mat magI = planes[0];
5、转换到对数值
事实证明,傅立叶系数的动态范围太大,无法显示在屏幕上。我们有一些小而高的变化值,我们不能这样观察。因此,高值将全部变为白点,而小值为黑色。要使用灰度值进行可视化,我们可以将线性比例转换为对数比例:
翻译成opencv代码:
magI += Scalar::all(1); // switch to logarithmic scale
log(magI, magI);
6、剪切和重组
记住,在第一步,我们扩展了图像?好了,是时候扔掉新引入的值了。出于可视化目的,我们还可以重新排列结果的象限,以便原点(零、零)与图像中心相对应。
// crop the spectrum, if it has an odd number of rows or columns
magI = magI(Rect(0, 0, magI.cols & -2, magI.rows & -2));
// rearrange the quadrants of Fourier image so that the origin is at the image center
int cx = magI.cols/2;
int cy = magI.rows/2;
Mat q0(magI, Rect(0, 0, cx, cy)); // Top-Left - Create a ROI per quadrant
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
Mat tmp; // swap quadrants (Top-Left with Bottom-Right)
q0.copyTo(tmp);
q3.copyTo(q0);
tmp.copyTo(q3);
q1.copyTo(tmp); // swap quadrant (Top-Right with Bottom-Left)
q2.copyTo(q1);
tmp.copyTo(q2);
7、规范化
再次执行此项以进行可视化。我们现在有量级,但是这仍然在我们的图像显示范围为零到 1。我们使用 cv::normalize() 函数将值规范化到此范围。
normalize(magI, magI, 0, 1, NORM_MINMAX); // Transform the matrix with float values into a
// viewable image form (float between values 0 and 1).
8、结论
傅里叶变换的一个应用想法是确定图像中的几何方向。例如,让我们了解文本是否水平?看一些文字,你会注意到文本行的形式也是水平线,字母形成一些垂直线。在傅立叶转换的情况下,也可以看到文本片段的这两个主要组成部分。让我们使用关于文本的水平和旋转图像:
水平文本案例:
旋转文本案例:
您可以看到频域中最有影响力的组件(幅度图像上最亮的点)遵循图像上对象的几何旋转。 由此我们可以计算偏移并执行图像旋转以校正最终的错失对齐。
完整源码:
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
static void help(void)
{
cout << endl
<< "This program demonstrated the use of the discrete Fourier transform (DFT). " << endl
<< "The dft of an image is taken and it's power spectrum is displayed." << endl
<< "Usage:" << endl
<< "./discrete_fourier_transform [image_name -- default ../data/lena.jpg]" << endl;
}
int main(int argc, char ** argv)
{
help();
const char* filename = argc >=2 ? argv[1] : "../data/lena.jpg";
Mat I = imread(filename, IMREAD_GRAYSCALE);
if( I.empty()){
cout << "Error opening image" << endl;
return -1;
}
Mat padded; //expand input image to optimal size
int m = getOptimalDFTSize( I.rows );
int n = getOptimalDFTSize( I.cols ); // on the border add zero values
copyMakeBorder(I, padded, 0, m - I.rows, 0, n - I.cols, BORDER_CONSTANT, Scalar::all(0));
Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};
Mat complexI;
merge(planes, 2, complexI); // Add to the expanded another plane with zeros
dft(complexI, complexI); // this way the result may fit in the source matrix
// compute the magnitude and switch to logarithmic scale
// => log(1 + sqrt(Re(DFT(I))^2 + Im(DFT(I))^2))
split(complexI, planes); // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
magnitude(planes[0], planes[1], planes[0]);// planes[0] = magnitude
Mat magI = planes[0];
magI += Scalar::all(1); // switch to logarithmic scale
log(magI, magI);
// crop the spectrum, if it has an odd number of rows or columns
magI = magI(Rect(0, 0, magI.cols & -2, magI.rows & -2));
// rearrange the quadrants of Fourier image so that the origin is at the image center
int cx = magI.cols/2;
int cy = magI.rows/2;
Mat q0(magI, Rect(0, 0, cx, cy)); // Top-Left - Create a ROI per quadrant
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
Mat tmp; // swap quadrants (Top-Left with Bottom-Right)
q0.copyTo(tmp);
q3.copyTo(q0);
tmp.copyTo(q3);
q1.copyTo(tmp); // swap quadrant (Top-Right with Bottom-Left)
q2.copyTo(q1);
tmp.copyTo(q2);
normalize(magI, magI, 0, 1, NORM_MINMAX); // Transform the matrix with float values into a
// viewable image form (float between values 0 and 1).
imshow("Input Image" , I ); // Show the result
imshow("spectrum magnitude", magI);
waitKey();
return 0;
}