// ================= // || Worksheet 5 // || In this example, we are going be visualizing waves. // ================= // for another (less detailed example) see, // http://docs.opencv.org/doc/tutorials/core/discrete_fourier_transform/discrete_fourier_transform.html #include "program.h" using namespace std; using namespace cv; // In this worksheet, we will be performing DFT's and Inverse DFT's // (for this exercise, we are going to ignore the image passed in i.e. 'src') int worksheetfive(Mat& src) { // We are going to start with a blank (fourier space) image. I..e // a blank image indicates that we are not 'playing' any notes on our 2d piano. // we'll make a 400x400 matrix of zeros, with floating point values. Mat blankImage = Mat::zeros(400,400, CV_32F); // now we embed this real image into a complex image // this is where fourier transforms live // an image where every pixel represents a complex number. Mat complexImage = embedInComplexImage(blankImage); // now, set only a few pixels (i.e. musical notes) to be non-zero. // the value we set on the can be any complex number. here we are just using 1+0i complexImage.at>(0,6) = complex(1.0,0); complexImage.at>(0,12) = complex(1.0,0); complexImage.at>(0,18) = complex(1.0,0); // Now, we are going to compute the magnitude of our complex image. // i.e. we want a new image whose values are sqrt( X^2+ Y^2) of our complex image X+iY. Mat magnitudeImage = computeMagnitudeImage(complexImage); // now we rearrange, to make the image look a little better. rearrange(magnitudeImage); // Show the result imshow("Spectrum Magnitude", magnitudeImage); // PERFORM THE INVERSE DISCRETE FOURIER TRANSFORM (IDFT) cv::Mat invtransformedImage; // perform the inverse transform of our complex image. cv::dft(complexImage, invtransformedImage, cv::DFT_INVERSE | cv::DFT_REAL_OUTPUT); /// just normalize our image to be safe. normalize(invtransformedImage, invtransformedImage, 0, 1, CV_MINMAX); // show the IDFT imshow("Reconstructed", invtransformedImage); waitKey(); return 0; }