Skip to content

Instantly share code, notes, and snippets.

@epiphany27
Created October 16, 2014 10:16
Show Gist options
  • Select an option

  • Save epiphany27/c90a03da1f0d8f9ffa5a to your computer and use it in GitHub Desktop.

Select an option

Save epiphany27/c90a03da1f0d8f9ffa5a to your computer and use it in GitHub Desktop.
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