OpenCV学习笔记-\doc\tutorials\core\discrete_fourier_transform

2013.5.26_页面_12013.5.26_页面_22013.5.26_页面_32013.5.26_页面_42013.5.26_页面_52013.5.26_页面_62013.5.26_页面_72013.5.26_页面_8

一些必要的补充说明:

这一节很多函数作者并没有细讲每一个参数的作用和含义,下面作补充说明:

1.copyMakeBorder()

查阅API Reference::improc,可以得到以下信息:

copyMakeBorder

从中可以看到源码中那几个参数的意思,就是图像顶部不补全,底部补全,左边不补全,右边补全,这下就很好理解扩展图像那一段代码了。

2.merge()

查阅API Reference::core,可以得到以下信息:

merge

planes[ ]是一个Mat数组,是输入,2表示有两个通道,comlexI是输出。与其相反,

后面计算幅度值时要分析两个通道,使用split()函数也很好理解了。

3.dft()

这个函数看似使用简单,实际内部很复杂,它实际上有4个参数,输入输出也有多种情况,

后面将幅度图像的四个角点重叠使原点到图像中心,下面的代码示例将显示没有调整的幅度图像和调整过后的幅度图像,从对比中可以看到,具体关于dft()等变换的使用后面还需要深入学习。

和以前一样,我把代码稍作修改,用lena.jpg来演示一下:

outcome

从中可以看出象限调整前和调整后的区别。;-)具体为什么,以后会讲到。

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

using namespace cv;
using namespace std;
int main(int argc, char ** argv)
{
    const char* filename = argc >=2 ? argv[1] : "lena.jpg";

    Mat I = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
    if( I.empty())
        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));
	Mat unrearranged = magI.clone();
    // 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, CV_MINMAX); // Transform the matrix with float values into a
                                            // viewable image form (float between values 0 and 1).
	normalize(unrearranged,unrearranged,0,1,CV_MINMAX);
	namedWindow("Unrearranged");
	imshow("Unrearranged",unrearranged);
	namedWindow("Input Image");
    imshow("Input Image"       , I   );    // Show the result
	namedWindow("spectrum magnitude");
    imshow("spectrum magnitude", magI);
    waitKey(0);

    return 0;
}
posted @ 2013-05-26 12:59  GuanHaoOnceMore2014  阅读(295)  评论(0编辑  收藏  举报
分享到QQ空间