I want to show that phase of an image carries more information than that of its magnitude, so I want to exchange the magnitude of two image and then do the inverse DFT. here is the code:
void main()
{
Mat I1 = imread("lena.jpg", CV_LOAD_IMAGE_GRAYSCALE);
Mat I2 = imread("peppers.png", CV_LOAD_IMAGE_GRAYSCALE);
Mat padded1,padded2;
//expand input image to optimal size
int m1= getOptimalDFTSize( I1.rows );
int n1 = getOptimalDFTSize( I1.cols );
int m2= getOptimalDFTSize( I2.rows );
int n2 = getOptimalDFTSize( I2.cols );
// on the border add zero values
copyMakeBorder(I1, padded1, 0, m1 - I1.rows, 0, n1 - I1.cols, BORDER_CONSTANT, Scalar::all(0));
copyMakeBorder(I2, padded2, 0, m2 - I2.rows, 0, n2 - I2.cols, BORDER_CONSTANT, Scalar::all(0));
Mat planes1[] = {Mat_<float>(padded1), Mat::zeros(padded1.size(), CV_32F)};
Mat planes2[] = {Mat_<float>(padded2), Mat::zeros(padded2.size(), CV_32F)};
Mat complexI, complexII;
// Add to the expanded another plane with zeros
merge(planes1, 2, complexI);
merge(planes2, 2, complexII);
dft(complexI, complexI);
dft(complexII, complexII);
// compute the magnitude and phase then switch to logarithmic scale
// => magnitude:log(1 + sqrt(Re(DFT(I))^2 + Im(DFT(I))^2)), phase:arctan(Im(DFT(I)),Re(DFT(I)))
split(complexI, planes1);// planes[0] = Re(DFT(I)), planes[1] = Im(DFT(I))
Mat ph1, magI1;
phase(planes1[0], planes1[1], ph1);//ph1 = phase
magnitude(planes1[0], planes1[1], magI1);// magI1 = magnitude
magI1 = magI1(Rect(0, 0, magI1.cols & -2, magI1.rows & -2));
ph1 = ph1(Rect(0, 0, ph1.cols & -2, ph1.rows & -2));
split(complexII, planes2);// planes[0] = Re(DFT(I)), planes[1] = Im(DFT(I))
Mat ph2, magI2;
phase(planes2[0], planes2[1], ph2);//ph2 = phase
magnitude(planes2[0], planes2[1], magI2);// mag2 = magnitude
magI2 = magI2(Rect(0, 0, magI2.cols & -2, magI2.rows & -2));
ph2 = ph2(Rect(0, 0, ph2.cols & -2, ph2.rows & -2));
planes1[1] = ph1; planes1[0] = magI2;
planes2[1] = ph2; planes2[0] = magI1;
dft(complexI,complexI,DFT_INVERSE);
dft(complexII,complexII,DFT_INVERSE);
imshow("image", complexI);
waitKey();
}
I just simply merge magnitude and phase together then do the IDFT, seems totally wrong.