#include "program.h" int main(int argc, char ** argv) { Mat src; if( argc > 1) { char* first_file_passed_in = argv[1]; src = cv::imread( first_file_passed_in, CV_LOAD_IMAGE_GRAYSCALE ); } // quit if we didnt read in any data. if( src.empty()) return -1; //return worksheetone(int argc, char ** argv); return worksheettwo(src); //return worksheetthree( src); //return worksheetfour(src); //return worksheetfive( src); //return worksheetsix( src); //return worksheetseven( src); //return worksheeteight( src); //return worksheetnine( src); //return worksheetten( src); //return worksheeteleven( src); //return worksheettwelve( src); //return worksheetthirteen( src); //return worksheetfourteen( src); } /** * Rearrange the quadrants of a fourier transform */ void rearrange( Mat& dftimage) { // rearrange the quadrants of Fourier image so that the origin is at the image center int cx = dftimage.cols/2; int cy = dftimage.rows/2; Mat q0(dftimage, Rect(0, 0, cx, cy)); // Top-Left - Create a ROI per quadrant Mat q1(dftimage, Rect(cx, 0, cx, cy)); // Top-Right Mat q2(dftimage, Rect(0, cy, cx, cy)); // Bottom-Left Mat q3(dftimage, Rect(cx, cy, cx, cy)); // Bottom-Right Mat tmp; // swap quadrants (Top-Left with Bottom-Right) q0.copyTo(tmp); q3.copyTo(q0); tmp.copyTo(q3); q1.copyTo(tmp); // swap quadrant (Top-Right with Bottom-Left) q2.copyTo(q1); tmp.copyTo(q2); } /** * Rotate an image */ void rotate(cv::Mat& src, double angle, cv::Mat& dst) { cv::Point2f pt(src.cols/2., src.rows/2.); cv::Mat r = cv::getRotationMatrix2D(pt, angle, 1.0); cv::warpAffine(src, dst, r, cv::Size(src.cols, src.rows)); } // here we produce a magnitude image so that we can view our fourier transform. // i.e. we want a new image whose values are sqrt( X^2+ Y^2) of our complex image X+iY. Mat computeMagnitudeImage(Mat& complexImage) { Mat planes[] = {Mat::zeros(complexImage.size(), CV_32F), Mat::zeros(complexImage.size(), CV_32F)}; // split the complex image back into its Re and Imaginary parts. // planes[0] = Re(DFT(I)), planes[1] = Im(DFT(I)) split(complexImage, planes); // 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; // luckily for us, there is an easy way to compute this. magnitude(planes[0], planes[1], magnitudeImage); // compute the magnitude and switch to logarithmic scale // => log(1 + sqrt(Re(DFT(I))^2 + Im(DFT(I))^2)) magnitudeImage += Scalar::all(1); // switch to logarithmic scale log(magnitudeImage, magnitudeImage); // crop the spectrum, if it has an odd number of rows or columns magnitudeImage = magnitudeImage(Rect(0, 0, magnitudeImage.cols & -2, magnitudeImage.rows & -2)); // Transform the matrix with float values into a // viewable image form (float between values 0 and 1). normalize(magnitudeImage, magnitudeImage, 0, 1, CV_MINMAX); return magnitudeImage; } Mat embedInComplexImage(Mat& realImage) { // Now, because of the way the DFT works, we are going to need to pad our image, // i.e. we are going to have to add some empty space around the image so that it has an // 'optimal' size for the DFT. Mat padded; //we now compute the optimal image size for our matrix. int m = getOptimalDFTSize( realImage.rows ); int n = getOptimalDFTSize( realImage.cols ); // add some padding to our image, so that it becomes an m x n matrix, // on the border add zero values copyMakeBorder(realImage, padded, 0, m - realImage.rows, 0, n - realImage.cols, BORDER_CONSTANT, Scalar::all(0)); // This is the COMPLEX version of our image. Mat complexImage; // we are going to set the values of this image to be X + 0i, where X are the // original pixel values in our image. I.e. the real part is our original image. // create an array of images, the first one will be the REAL part of our DFT image, // and the second one will be the complex part. Mat planes[] = {Mat_(padded), Mat::zeros(padded.size(), CV_32F)}; // add both of these planes together to make our complex image. X+0i. merge(planes, 2, complexImage); // Add to the expanded another plane with zeros return complexImage; }