opencv idft output and calculation of phase

asked 2015-02-26 22:20:13 -0600

updated 2015-02-27 07:25:43 -0600

theodore gravatar image

I have been trying to calculate the phase information of a complex matrix in opencv. As I am new in using opencv I am sure I am failing to look for the correct answer. So, I have this program.

I am sure the matrix invDFT holds complex values.

So what is the easiest way of calculating the phase of the total matrix? And how can I imshow the DFT output for this program? I have used phase and I am not sure if its correct.

Thanks. I already said I am new in opencv. So please pardon if my questions are too basic. Thanks once again.

int main()
    // Read image from file
    // Make sure that the image is in grayscale
    Mat img = imread("input.bmp", 0);
    Mat mag, ph;

    Mat planes[] = { Mat_<float>(img), Mat::zeros(img.size(), CV_32F) };
    Mat complexI;    //Complex plane to contain the DFT coefficients {[0]-Real,[1]-Img}
    merge(planes, 2, complexI);
    dft(complexI, complexI);  // Applying DFT

    // Reconstructing original imae from the DFT coefficients
    Mat invDFT, invDFTcvt;

    idft(complexI, invDFT, DFT_SCALE | DFT_REAL_OUTPUT); // Applying IDFT

    Mat planesi[] = { Mat_<float>(invDFT), Mat::zeros(invDFT.size(), CV_32F) };
    split(invDFT, planesi);

    invDFT.convertTo(invDFTcvt, CV_8U);
    imshow("Output", invDFTcvt);

    phase(planesi[0], planesi[1], ph, false);

    namedWindow("phase image", CV_WINDOW_AUTOSIZE);
    imshow("phase image", ph);

    //show the image
    imshow("Original Image", img);

    // Wait until user press some key
    return 0;
edit retag flag offensive close merge delete


have you checked the official tutorial and this answer here. The way that you are extracting the phase is ok.

theodore gravatar imagetheodore ( 2015-02-27 07:33:16 -0600 )edit