opencv/samples/cpp/dft.cpp

88 lines
2.2 KiB
C++
Raw Normal View History

#include "opencv2/core.hpp"
#include "opencv2/core/utility.hpp"
#include "opencv2/imgproc.hpp"
2014-07-04 22:48:15 +08:00
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
2010-11-28 07:15:16 +08:00
2011-08-11 14:46:09 +08:00
#include <stdio.h>
2010-11-28 07:15:16 +08:00
using namespace cv;
using namespace std;
2010-12-04 16:30:39 +08:00
2012-06-08 01:21:29 +08:00
static void help()
2010-12-03 18:38:43 +08:00
{
2012-06-08 01:21:29 +08:00
printf("\nThis program demonstrated the use of the discrete Fourier transform (dft)\n"
"The dft of an image is taken and it's power spectrum is displayed.\n"
"Usage:\n"
2014-09-13 22:28:41 +08:00
"./dft [image_name -- default ../data/lena.jpg]\n");
2010-12-03 18:38:43 +08:00
}
2012-06-08 01:21:29 +08:00
const char* keys =
2011-08-11 14:46:09 +08:00
{
2015-08-01 23:24:23 +08:00
"{help h||}{@image|../data/lena.jpg|input image file}"
2011-08-11 14:46:09 +08:00
};
2010-12-03 18:38:43 +08:00
2011-08-11 14:46:09 +08:00
int main(int argc, const char ** argv)
2010-11-28 07:15:16 +08:00
{
2011-08-11 14:46:09 +08:00
help();
2012-06-08 01:21:29 +08:00
CommandLineParser parser(argc, argv, keys);
2015-08-01 23:24:23 +08:00
if (parser.has("help"))
{
help();
return 0;
}
2013-01-31 16:08:43 +08:00
string filename = parser.get<string>(0);
2015-08-01 23:24:23 +08:00
Mat img = imread(filename, IMREAD_GRAYSCALE);
2010-11-28 07:15:16 +08:00
if( img.empty() )
{
2010-12-03 18:38:43 +08:00
help();
2012-06-08 01:21:29 +08:00
printf("Cannot read image file: %s\n", filename.c_str());
2010-11-28 07:15:16 +08:00
return -1;
}
int M = getOptimalDFTSize( img.rows );
int N = getOptimalDFTSize( img.cols );
Mat padded;
copyMakeBorder(img, padded, 0, M - img.rows, 0, N - img.cols, BORDER_CONSTANT, Scalar::all(0));
2012-06-08 01:21:29 +08:00
2010-11-28 07:15:16 +08:00
Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};
Mat complexImg;
merge(planes, 2, complexImg);
2012-06-08 01:21:29 +08:00
2010-11-28 07:15:16 +08:00
dft(complexImg, complexImg);
2012-06-08 01:21:29 +08:00
2010-11-28 07:15:16 +08:00
// compute log(1 + sqrt(Re(DFT(img))**2 + Im(DFT(img))**2))
split(complexImg, planes);
magnitude(planes[0], planes[1], planes[0]);
Mat mag = planes[0];
mag += Scalar::all(1);
log(mag, mag);
2012-06-08 01:21:29 +08:00
2010-11-28 07:15:16 +08:00
// crop the spectrum, if it has an odd number of rows or columns
mag = mag(Rect(0, 0, mag.cols & -2, mag.rows & -2));
2012-06-08 01:21:29 +08:00
2010-11-28 07:15:16 +08:00
int cx = mag.cols/2;
int cy = mag.rows/2;
2012-06-08 01:21:29 +08:00
2010-11-28 07:15:16 +08:00
// rearrange the quadrants of Fourier image
// so that the origin is at the image center
Mat tmp;
Mat q0(mag, Rect(0, 0, cx, cy));
Mat q1(mag, Rect(cx, 0, cx, cy));
Mat q2(mag, Rect(0, cy, cx, cy));
Mat q3(mag, Rect(cx, cy, cx, cy));
2012-06-08 01:21:29 +08:00
2010-11-28 07:15:16 +08:00
q0.copyTo(tmp);
q3.copyTo(q0);
tmp.copyTo(q3);
2012-06-08 01:21:29 +08:00
2010-11-28 07:15:16 +08:00
q1.copyTo(tmp);
q2.copyTo(q1);
tmp.copyTo(q2);
2012-06-08 01:21:29 +08:00
normalize(mag, mag, 0, 1, NORM_MINMAX);
2012-06-08 01:21:29 +08:00
2010-11-28 07:15:16 +08:00
imshow("spectrum magnitude", mag);
waitKey();
return 0;
}