Created
October 16, 2014 10:16
-
-
Save epiphany27/c90a03da1f0d8f9ffa5a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| float getFrequency() { | |
| Mat I = imread(filename, CV_LOAD_IMAGE_GRAYSCALE); | |
| if( I.empty()) | |
| return -1; | |
| Mat padded; //expand input image to optimal size | |
| int m = getOptimalDFTSize( I.rows ); | |
| int n = getOptimalDFTSize( I.cols ); // on the border add zero values | |
| copyMakeBorder(I, padded, 0, m - I.rows, 0, n - I.cols, BORDER_CONSTANT, Scalar::all(0)); | |
| Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)}; | |
| Mat complexI; | |
| merge(planes, 2, complexI); // Add to the expanded another plane with zeros | |
| dft(complexI, complexI); // this way the result may fit in the source matrix | |
| // compute the magnitude and switch to logarithmic scale | |
| // => log(1 + sqrt(Re(DFT(I))^2 + Im(DFT(I))^2)) | |
| split(complexI, planes); // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I)) | |
| magnitude(planes[0], planes[1], planes[0]);// planes[0] = magnitude | |
| Mat magI = planes[0]; | |
| /*cv::sortIdx(magI, magI, CV_SORT_EVERY_ROW + CV_SORT_DESCENDING); | |
| cv::sort(magI, magI, CV_SORT_EVERY_ROW + CV_SORT_DESCENDING);*/ | |
| int sum = 0; | |
| for(int j=0;j<magI.rows;j++) { | |
| for (int i=0;i<magI.cols;i++) { | |
| sum += static_cast<int>(magI.at<uchar>(j,i)); | |
| } | |
| } | |
| float avg = sum/(magI.rows*magI.cols); | |
| std::cout<<"Avg-Frequency: "<<avg<<endl; | |
| return avg; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment