我是OpenCV的新手。目前,我使用的是3.1版。
我尝试使用OpenCV doc for DFT中编写的确切代码来实现DFT,但我在Mat_<float>(padded)
部分收到错误:
我用于测试的图像是JPEG,3通道,8位。
如果我有任何错误,请告诉我。
#include "opencv2\core\core.hpp"
#include "opencv2\highgui\highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat rawImg = imread("E:/sample.jpg", CV_LOAD_IMAGE_COLOR); // 50x50 jpeg
Mat padded; //expand input image to optimal size
int m = getOptimalDFTSize(rawImg.rows);
int n = getOptimalDFTSize(rawImg.cols);
cout << "rows :" << m << endl;
cout << "cols :" << n << endl;
copyMakeBorder(rawImg, padded, 0, m - rawImg.rows, 0, n - rawImg.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
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));
/** As my input image is 50x50 it has no extra pixel to delete I have ommited this part from the original code
**/
normalize(magI, magI, 0, 1, CV_MINMAX); // Transform the matrix with float values into a
// viewable image form (float between values 0 and 1).
imshow("Input Image", rawImg); // Show the result
imshow("spectrum magnitude", magI);
waitKey(0);
return 0;
}