1. 程式人生 > >openCV 傅立葉逆變換 inverse fourier transform

openCV 傅立葉逆變換 inverse fourier transform

呼叫dft(img, img)便可以得到img的傅立葉變換的值,並替換原img的值。

這時候如果需要逆變換的值,那麼你便需要呼叫idft()或者dft().

這步的結果一直有問題,因為我在呼叫的時候少加了一個DFT_SCALE!!!

正確的方法:

dft(img, img, DFT_SCALE|DFT_INVERSE);

看我上面給的網頁裡面官方說明, DFT_SCALE在求逆變換時一般是同時存在的,要不然得到的是放大了N(N為元素總數)倍。

實在搞不明白OpenCV要搞這麼一出,浪費了我不知道多少個小時,不過這也告訴我,呼叫函式時認真讀API的文件是很有必要的。

下面是我測試結果是否正確的原始碼, 在ubuntu的終端編譯指令為: g++ -o test test.C `pkg-config opencv --cflags --libs`:

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

using namespace std;
using namespace cv;

int main(){
    Mat img(3, 3, CV_64FC2);
    int k = 0;
    for(unsigned i = 0; i < 3; i++){
        for(unsigned j = 0; j < 3; j++){
            img.at<std::complex<double> >(i,j) = k++;
        }
    }
    for(unsigned i = 0; i < 3; i++){
        for(unsigned j = 0; j < 3; j++){
            cout << img.at<std::complex<double> >(i,j) << endl;
        }
    dft(img, img);
    dft(img, img, DFT_SCALE|DFT_INVERSE);
    for(unsigned i = 0; i < 3; i++){
        for(unsigned j = 0; j < 3; j++){
            cout << img.at<std::complex<double> >(i,j) << endl;
        }
    }
}