opencv實現快速傅立葉變換和逆變換
原文: http://www.tuicool.com/articles/VB3UNjf
說實話覺得網上很多人轉載的文章的挺坑的,全部是OpenCV文檔程序的翻譯,看來看去都是那一
篇,真的沒啥意思。 文檔的地址。
本來opencv實現dft就是一個函數的事情,但是很少有關于逆變換使用的資料。我這幾天在翻譯
matlab版本的L0Smooth到opencv上面,就碰到這樣一件很坑爹的事情。
首先,很少有人說清楚這個函數的使用方法。還有,根據教程,dft之前最好擴充原矩陣到合適的尺
寸(2,3,5的倍數),再調用dft會加快速度。那么,idft的時候了?如何恢復原有的尺寸?
在我的L0Smooth代碼里,就碰到這樣的事情了。如果,圖片尺寸是2,3,5的倍數,那么能夠得到
正確結果。否則得到是全黑的圖片。如果,我不擴張矩陣,那么就能正確處理。
所以,到這里,我不推薦調用dft之前先擴充矩陣了。因為,我找了很久也沒找到解決辦法。
我數學水平有限,也分析不出原因,也沒有時間去系統的學習這些了。
這里提供兩個例子,說明dft和idft的使用。
例子一:類似于opencv官方文檔的例子
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include
#ifdef _DEBUG
#pragma comment(lib, "opencv_core247d.lib")
#pragma comment(lib, "opencv_imgproc247d.lib")
#pragma comment(lib, "opencv_highgui247d.lib")
#else
#pragma comment(lib, "opencv_core247.lib")
#pragma comment(lib, "opencv_imgproc247.lib")
#pragma comment(lib, "opencv_highgui247.lib")
#endif // DEBUG
int main()
{
// Read image from file
// Make sure that the image is in grayscale
cv::Mat img = cv::imread("lena.JPG",0);
cv::Mat planes[] = {cv::Mat_
cv::Mat complexI; //Complex plane to contain the DFT coefficients {[0]-Real,[1]-Img}
cv::merge(planes, 2, complexI);
cv::dft(complexI, complexI); // Applying DFT
//這里可以對復數矩陣comlexI進行處理
// Reconstructing original imae from the DFT coefficients
cv::Mat invDFT, invDFTcvt;
cv::idft(complexI, invDFT, cv::DFT_SCALE | cv::DFT_REAL_OUTPUT ); // Applying IDFT
cv::invDFT.convertTo(invDFTcvt, CV_8U);
cv::imshow("Output", invDFTcvt);
//show the image
cv::imshow("Original Image", img);
// Wait until user press some key
cv::waitKey(0);
return 0;
}
代碼意思很簡單,dft之后再idft,注意參數額,必須有DFT_SCALE。代碼中,先merge了個
復數矩陣,在例子2中可以看到,其實這一步可以去掉。
例子2:
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include
#ifdef _DEBUG
#pragma comment(lib, "opencv_core247d.lib")
#pragma comment(lib, "opencv_imgproc247d.lib")
#pragma comment(lib, "opencv_highgui247d.lib")
#else
#pragma comment(lib, "opencv_core247.lib")
#pragma comment(lib, "opencv_imgproc247.lib")
#pragma comment(lib, "opencv_highgui247.lib")
#endif // DEBUG
int main()
{
// Read image from file
// Make sure that the image is in grayscale
cv:;Mat img = cv::imread("lena.JPG",0);
cv::Mat dftInput1, dftImage1, inverseDFT, inverseDFTconverted;
cv::img.convertTo(dftInput1, CV_32F);
cv::dft(dftInput1, dftImage1, cv::DFT_COMPLEX_OUTPUT); // Applying DFT
// Reconstructing original imae from the DFT coefficients
cv::idft(dftImage1, inverseDFT, cv::DFT_SCALE | cv::DFT_REAL_OUTPUT ); // Applying IDFT
cv::inverseDFT.convertTo(inverseDFTconverted, CV_8U);
cv::imshow("Output", inverseDFTconverted);
//show the image
cv::imshow("Original Image", img);
// Wait until user press some key
waitKey(0);
return 0;
}
從代碼中可以看到,dft時候添加參數DFT_COMPLEX_OUTPUT,就可以自動得到復數矩陣了,代碼更加簡潔。注意,必須先將圖片對應的uchar矩陣轉換為float矩陣,再進行dft,idft,最后再轉換回來。
OpenCV
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。