//进行频谱的中心化
void fftshift(Mat source, Mat &dest)
{
int row = source.rows;
int col = source.cols;
int halfRow = row / 2;
int halfCol = col / 2;
int temp = 0;
for (size_t j = 0; j < row; j++)
{
temp = (j + halfRow) % row;
for (size_t i = 0; i < col; i++)
{
*dest.ptr<float>(temp, i) = *source.ptr<float>(j, i);
}
}
source = dest.clone();
for (size_t i = 0; i < col; i++)
{
temp = (i + halfCol) % col;
for (size_t j = 0; j < row; j++)
{
*dest.ptr<float>(j, temp) = *source.ptr<float>(j, i);
}
}
}
void test()
{
int i;
fftw_complex*din, *out;
fftw_plan p,backp;
Mat image = imread("C:\\Users\\Tony\\Desktop\\Image\\lenaCopy.bmp", CV_LOAD_IMAGE_GRAYSCALE);
din = (fftw_complex *)fftw_malloc(sizeof(fftw_complex)*image.cols*image.rows);
out = (fftw_complex *)fftw_malloc(sizeof(fftw_complex)*image.cols*image.rows);
Mat floatImage(image.size(), CV_32FC1);
image.convertTo(floatImage, CV_32FC1);
for (size_t j = 0; j < image.rows; j++)
{
for (size_t i = 0; i < image.cols; i++)
{
din[i + j*image.cols][0] = *floatImage.ptr<float>(j, i);
din[i + j*image.cols][1] = 0;
}
}
//forward fft
p = fftw_plan_dft_2d(image.rows, image.cols, din, out, FFTW_FORWARD, FFTW_ESTIMATE);
fftw_execute(p);
Mat Resource(image.size(), CV_32FC1);
Mat Imsource(image.size(), CV_32FC1);
for (size_t j = 0; j < image.rows; j++)
{
for (size_t i = 0; i < image.cols; i++)
{
*Resource.ptr<float>(j, i) = out[i + j*image.cols][0];//实部
*Imsource.ptr<float>(j, i) = out[i + j*image.cols][1];//虚部
}
}
Mat Redest(image.size(), CV_32FC1);
Mat Imdest(image.size(), CV_32FC1);
fftshift(Resource, Redest);
fftshift(Imsource, Imdest);
Mat absImage(image.size(), CV_32FC1);
for (size_t j = 0; j < image.rows; j++)
{
for (size_t i = 0; i < image.cols; i++)
{
*absImage.ptr<float>(j, i) = sqrt(pow(*Redest.ptr<float>(j, i), 2) + pow(*Imdest.ptr<float>(j, i), 2));
}
}
absImage += Scalar::all(1);
Mat logImage(image.size(), CV_32FC1);
log(absImage, logImage);
//SaveData(logImage);
double min = 0.0, max = 0.0;
minMaxLoc(logImage, &min, &max);
double scale = 255/(max-min);
double shift = -min*scale;
Mat myImage;
convertScaleAbs(logImage, myImage, scale, shift);
//imshow("123", myImage);
imwrite("C:\\Users\\Tony\\Desktop\\lenaCopy.bmp", myImage);
}