離散傅立葉變換(DFT)和快速傅立葉變換(FFT)原理與實現
目錄
1、影象變換
2、離散傅立葉變換(Discrete Fourier Transform)
3、DFT性質
4、DFT與數字影象處理
5、FFT-快速傅立葉變換
6、DFT與FFT的演算法實現
1. 影象變換
— —數學領域中有很多種變換,如傅立葉變換、拉普拉斯變換、Z變換等。但是這些數學變換的用途大致相同,即利用某種變換使得遇到的問題更方便的觀測或解決。數字影象處理技術(Digital Image Processing)是一門應用學科,它是建立在一定的數學理論基礎之上的科學,因此,在解決數字影象處理的具體問題時,作為解決方案或技巧,就必須要用到各種數學變換,稱為影象變換。
– 在數字影象處理中,影象增強、影象復原、影象編碼和壓縮、影象分析與描述等每一種處理手段和方法都要用到影象變換,例如:在對影象進行去噪或銳化操作時,low-pass filter、high-pass filter 可以藉助傅立葉變換把要在空間域中解決的卷積運算問題轉換並對映到頻率域中的乘積運算。
2. 離散傅立葉變換(DFT)
回顧:連續訊號的傅立葉變換
2.1 一維離散傅立葉變換
為了在計算機上實現傅立葉變換計算,必須把連續函式離散化,離散函式的額傅立葉變換稱為離散傅立葉變換。
連續訊號離散化過程如下:
- 建立連續時間訊號x(t)
- 設定取樣間隔△t
- 對連續訊號做時域等間隔取樣,得到離散序列x(k△t), (k=0,1,2,N-1)
- 可得到有N個元素的離散函式序列x(n)
因此,一維離散傅立葉變換(DFT)和一維離散傅立葉反變換
(Inverse DFT)可表示為:
一維離散傅立葉變換具有周期性,證明如下:
尤拉方程(Euler theorem):
2.2 二維離散傅立葉變換
對於一個具有MxN尺度的二維離散函式f(x,y), (x=0,1,2,M-1; y=0,1,2,N-1),其離散傅立葉變換為:
在運用計算機進行二維離散傅立葉變換時,可先對每一行或者每一列進行一維的離散傅立葉變換,然後對上一步的計算結果再進行列或者行的一維離散傅立葉變換,即可得到二維的離散傅立葉變換結果。具體步驟如下圖所示:
3. DFT Properties
二維離散傅立葉變換具有許多數字影象處理中非常實用的性質,主要包括:可分離性、平移性、線性特性、比例特性、週期性和共軛對稱性、微分特性、旋轉特性等。
3-1. 可分離性
由二維傅立葉變換不難看出可以把變換方程寫成如下形式:
這個性質稱為二維傅立葉變換的可分離性,利用這個性質,可以通過進行兩次一維離散傅立葉變換(或反變換)來實現一個二維傅立葉變換(反變換)。以正變換為例,具體演算法步驟如下:
3-2. 平移性
這一性質表明,當用exp[j2π(ux/M+vy/N)]乘以f(x,y),求乘積的傅立葉變換,可以使得空間頻率域u-v平面座標原點從(0,0)平移到(u,v)的位置。同樣,當用exp[-j2π(ux/M+vy/N)]乘以F(x,y),並求此乘積的離散傅立葉反變換,可以使空間x-y平面座標系的原點從(0,0)平移到(x,y)的位置。
3-3. 線性特性
如果二維離散函式f1(x,y)和f2(x,y)的傅立葉變換分別為F1(x,y)和F1(x,y),則存在以下線性特性。
f1(x,y)+ f2(x,y)<=>F1(x,y)+ F1(x,y)
這一性質可使得我們節約求傅立葉變換的時間,如果已經得到了F1(x,y)和F1(x,y),則f1(x,y)+ f2(x,y)的傅立葉變換結果只需將F1(x,y)和F1(x,y)相加求和即可。
需要注意的是,如果在顯示器上顯示|F1(x,y)+ F1(x,y)|的結果,則往往由於其最大值超過了數字影象處理裝置所允許的最大灰度造成資訊丟失現象。因此,在顯示|F1(x,y)+ F1(x,y)|的結果時應先檢查其最大值,若超過了裝置所允許的最大灰度值,則應當先做線性對比度壓縮處理。
3-4. 比例特性
3-5. 週期性和共軛對稱性
從二維離散傅立葉變換對的表示式可以看出,離散傅立葉變換和它的反變換具有周期性,即:
3- 6. 旋轉特性
二維傅立葉變換具有如下旋轉特性:
f(r,a+b) <=> F(w,φ+φ0)
該性質表明,如果f(x,y)在空間域旋轉一個角度a,則對應的傅立葉變換也在頻率域旋轉同樣的角度。反之,亦然。
3-7. 微分特性
在模式識別技術中,經常用到Laplace運算元。
3- 8. 平均值特性
4. DFT與數字影象處理
接下來用Lena.jpg標準圖來做二維傅立葉變換:
Mac OS 下使用OpenCV和C++整合開發環境做傅立葉變換
程式碼如下:
//
// dft_t.cpp
// OpenCV_demo1
//
// Created by Hao Wang on 2018/11/2.
// Copyright © 2018年 Hao Wang. All rights reserved.
//
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat Im_read = imread("/Users/haowang/Desktop/Workspace_C++/Opencv_demo/OpenCV_demo1/Standard Pictures/Lena.jpg", IMREAD_GRAYSCALE); //讀入影象灰度圖
//判斷影象是否載入成功
if (Im_read.empty())
{
cout << "影象載入失敗!" << endl;
return -1; // read error,return -1
}
else
cout << "影象載入成功!" << endl;
Mat padded; //以0填充輸入影象矩陣
int m = getOptimalDFTSize(Im_read.rows);
int n = getOptimalDFTSize(Im_read.cols);
//填充輸入影象I,輸入矩陣為padded,上方和左方不做填充處理
copyMakeBorder(Im_read, padded, 0, m - Im_read.rows, 0, n - Im_read.cols, BORDER_CONSTANT, Scalar::all(0));
Mat planes[] = { Mat_<float>(padded), Mat::zeros(padded.size(),CV_32F) };
Mat complexI;
merge(planes, 2, complexI); //將planes融合合併成一個多通道陣列complexI
dft(complexI, complexI); //進行傅立葉變換
//計算幅值,轉換到對數尺度(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))
//即planes[0]為實部,planes[1]為虛部
magnitude(planes[0], planes[1], planes[0]); //planes[0] = magnitude
Mat magI = planes[0];
magI += Scalar::all(1);
log(magI, magI); //轉換到對數尺度(logarithmic scale)
//如果有奇數行或列,則對頻譜進行裁剪
magI = magI(Rect(0, 0, magI.cols&-2, magI.rows&-2));
//重新排列傅立葉影象中的象限,使得原點位於影象中心
int cx = magI.cols / 2;
int cy = magI.rows / 2;
Mat q0(magI, Rect(0, 0, cx, cy)); //左上角影象劃定ROI區域
Mat q1(magI, Rect(cx, 0, cx, cy)); //右上角影象
Mat q2(magI, Rect(0, cy, cx, cy)); //左下角影象
Mat q3(magI, Rect(cx, cy, cx, cy)); //右下角影象
//變換左上角和右下角象限
Mat tmp;
q0.copyTo(tmp);
q3.copyTo(q0);
tmp.copyTo(q3);
//變換右上角和左下角象限
q1.copyTo(tmp);
q2.copyTo(q1);
tmp.copyTo(q2);
//歸一化處理,用0-1之間的浮點數將矩陣變換為可視的影象格式
normalize(magI, magI, 0, 1, CV_MINMAX);
imshow("輸入影象", Im_read);
imshow("頻譜圖", magI);
waitKey(0);
return 0;
}
計算後得到Lena圖的傅立葉變換結果如下:
5. FFT(快速傅立葉變換)
6. FFT演算法實現
6.1 一維離散傅立葉變換MATLAB實現
function w = dwt(x, J, af)
% Discrete 1-D Wavelet Transform
%
% USAGE:
% w = dwt(x, J, af)
% INPUT:
% x - N-point vector, where
% 1) N is divisible by 2^J
% 2) N >= 2^(J-1)*length(af)
% J - number of stages
% af - analysis filters
% af(:, 1) - lowpass filter (even length)
% af(:, 2) - highpass filter (evenlength)
% OUTPUT:
% w{j}, j = 1..J+1 - DWT coefficients
% EXAMPLE:
% [af, sf] = farras;
% x = rand(1,64);
% w = dwt(x,3,af);
% y = idwt(w,3,sf);
% err = x - y;
% max(abs(err))
%
% WAVELET SOFTWARE AT POLYTECHNIC UNIVERSITY, BROOKLYN, NY
% http://taco.poly.edu/WaveletSoftware/
for k = 1:J
[x w{k}] = afb(x, af);
end
w{J+1} = x;
6.2 二維離散傅立葉變換MATLAB實現
function w = dwt2D(x, J, af)
% discrete 2-D wavelet transform
%
% USAGE:
% w = dwt2D(x, stages, af)
% INPUT:
% x - N by M matrix
% 1) M, N are both even
% 2) min(M,N) >= 2^(J-1)*length(af)
% J - number of stages
% af - analysis filters
% OUPUT:
% w - cell array of wavelet coefficients
% EXAMPLE:
% [af, sf] = farras;
% x = rand(128,64);
% J = 3;
% w = dwt2D(x,J,af);
% y = idwt2D(w,J,sf);
% err = x - y;
% max(max(abs(err)))
%
% WAVELET SOFTWARE AT POLYTECHNIC UNIVERSITY, BROOKLYN, NY
for k = 1:J
[x w{k}] = afb2D(x, af, af);
end
w{J+1} = x;
6.3 OpenCV(C++)實現
#include <cxcore.h>
#include <cv.h>
#include <highgui.h>
// Rearrange the quadrants of Fourier image so that the origin is at
// the image center
// src & dst arrays of equal size & type
void cvShiftDFT(CvArr * src_arr, CvArr * dst_arr )
{
CvMat * tmp;
CvMat q1stub, q2stub;
CvMat q3stub, q4stub;
CvMat d1stub, d2stub;
CvMat d3stub, d4stub;
CvMat * q1, * q2, * q3, * q4;
CvMat * d1, * d2, * d3, * d4;
CvSize size = cvGetSize(src_arr);
CvSize dst_size = cvGetSize(dst_arr);
int cx, cy;
if(dst_size.width != size.width ||
dst_size.height != size.height){
cvError( CV_StsUnmatchedSizes, "cvShiftDFT", "Source and Destination arrays must have equal sizes", __FILE__, __LINE__ );
}
if(src_arr==dst_arr){
tmp = cvCreateMat(size.height/2, size.width/2, cvGetElemType(src_arr));
}
cx = size.width/2;
cy = size.height/2; // image center
q1 = cvGetSubRect( src_arr, &q1stub, cvRect(0,0,cx, cy) );
q2 = cvGetSubRect( src_arr, &q2stub, cvRect(cx,0,cx,cy) );
q3 = cvGetSubRect( src_arr, &q3stub, cvRect(cx,cy,cx,cy) );
q4 = cvGetSubRect( src_arr, &q4stub, cvRect(0,cy,cx,cy) );
d1 = cvGetSubRect( dst_arr, &d1stub, cvRect(0,0,cx,cy) );
d2 = cvGetSubRect( dst_arr, &d2stub, cvRect(cx,0,cx,cy) );
d3 = cvGetSubRect( dst_arr, &d3stub, cvRect(cx,cy,cx,cy) );
d4 = cvGetSubRect( dst_arr, &d4stub, cvRect(0,cy,cx,cy) );
if(src_arr!=dst_arr){
if( !CV_ARE_TYPES_EQ( q1, d1 )){
cvError( CV_StsUnmatchedFormats, "cvShiftDFT", "Source and Destination arrays must have the same format", __FILE__, __LINE__ );
}
cvCopy(q3, d1, 0);
cvCopy(q4, d2, 0);
cvCopy(q1, d3, 0);
cvCopy(q2, d4, 0);
}
else{
cvCopy(q3, tmp, 0);
cvCopy(q1, q3, 0);
cvCopy(tmp, q1, 0);
cvCopy(q4, tmp, 0);
cvCopy(q2, q4, 0);
cvCopy(tmp, q2, 0);
}
}
int main(int argc, char ** argv)
{
const char* filename = argc >=2 ? argv[1] : "lena.jpg";
IplImage * im;
IplImage * realInput;
IplImage * imaginaryInput;
IplImage * complexInput;
int dft_M, dft_N;
CvMat* dft_A, tmp;
IplImage * image_Re;
IplImage * image_Im;
double m, M;
im = cvLoadImage( filename, CV_LOAD_IMAGE_GRAYSCALE );
if( !im )
return -1;
realInput = cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 1);
imaginaryInput = cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 1);
complexInput = cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 2);
cvScale(im, realInput, 1.0, 0.0);
cvZero(imaginaryInput);
cvMerge(realInput, imaginaryInput, NULL, NULL, complexInput);
dft_M = cvGetOptimalDFTSize( im->height - 1 );
dft_N = cvGetOptimalDFTSize( im->width - 1 );
dft_A = cvCreateMat( dft_M, dft_N, CV_64FC2 );
image_Re = cvCreateImage( cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1);
image_Im = cvCreateImage( cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1);
// copy A to dft_A and pad dft_A with zeros
cvGetSubRect( dft_A, &tmp, cvRect(0,0, im->width, im->height));
cvCopy( complexInput, &tmp, NULL );
if( dft_A->cols > im->width )
{
cvGetSubRect( dft_A, &tmp, cvRect(im->width,0, dft_A->cols - im->width, im->height));
cvZero( &tmp );
}
// no need to pad bottom part of dft_A with zeros because of
// use nonzero_rows parameter in cvDFT() call below
cvDFT( dft_A, dft_A, CV_DXT_FORWARD, complexInput->height );
cvNamedWindow("win", 0);
cvNamedWindow("magnitude", 0);
cvShowImage("win", im);
// Split Fourier in real and imaginary parts
cvSplit( dft_A, image_Re, image_Im, 0, 0 );
// Compute the magnitude of the spectrum Mag = sqrt(Re^2 + Im^2)
cvPow( image_Re, image_Re, 2.0);
cvPow( image_Im, image_Im, 2.0);
cvAdd( image_Re, image_Im, image_Re, NULL);
cvPow( image_Re, image_Re, 0.5 );
// Compute log(1 + Mag)
cvAddS( image_Re, cvScalarAll(1.0), image_Re, NULL ); // 1 + Mag
cvLog( image_Re, image_Re ); // log(1 + Mag)
// Rearrange the quadrants of Fourier image so that the origin is at
// the image center
cvShiftDFT( image_Re, image_Re );
cvMinMaxLoc(image_Re, &m, &M, NULL, NULL, NULL);
cvScale(image_Re, image_Re, 1.0/(M-m), 1.0*(-m)/(M-m));
cvShowImage("magnitude", image_Re);
cvWaitKey(-1);
return 0;
}