IMPEM2D_BEGIN_NAMESPACE void get_autocorrelation2d_no_preprocessing(const cv::Mat &M, cv::Mat &corr) { IMP_LOG_VERBOSE("Computing 2D autocorrelation no preprocessing" << std::endl); IMP_USAGE_CHECK(((M.rows != 0) && (M.cols != 0)), "em2d:get_autocorrelation2d: Output matrix is empty"); cv::Mat temp; cv::mulSpectrums(M, M, temp, 0, true); cv::idft(temp, temp, cv::DFT_SCALE + cv::DFT_REAL_OUTPUT); temp(cv::Rect(0, 0, corr.cols, corr.rows)).copyTo(corr); do_matrix_to_image_flip(corr); }
void get_correlation2d_no_preprocessing(const cv::Mat &M1, const cv::Mat &M2, cv::Mat &corr) { IMP_LOG_VERBOSE("Computing 2D correlation no preprocessing "<<std::endl); IMP_USAGE_CHECK(((M1.rows==M2.rows) && (M1.cols == M2.cols)), "em2d:get_correlation2d: Matrices have different size."); cv::Mat temp; cv::mulSpectrums(M1, M2, temp,0,true); cv::idft(temp, temp,cv::DFT_SCALE+cv::DFT_REAL_OUTPUT); // now copy the result to corr temp(cv::Rect(0, 0, corr.cols, corr.rows)).copyTo(corr); do_matrix_to_image_flip(corr); }
void get_correlation2d(const cv::Mat &A, const cv::Mat &B, cv::Mat &corr) { IMP_LOG_VERBOSE("Computing 2D correlation " <<std::endl); IMP_USAGE_CHECK(((A.rows==B.rows) && (A.cols == B.cols)), "em2d:get_correlation2d: Matrices have different size."); // resize the output array if needed corr.create(A.rows,A.cols, A.type()); cv::Size dftSize; // compute the optimal size for faster DFT transform dftSize.width = cv::getOptimalDFTSize(A.cols); dftSize.height = cv::getOptimalDFTSize(A.rows); // temporary matrices cv::Mat tempA(dftSize, A.type(), cv::Scalar::all(0)); cv::Mat tempB(dftSize, B.type(), cv::Scalar::all(0)); // copy A and B to the top-left corners of tempA and tempB, respectively cv::Mat roiA(tempA, cv::Rect(0,0,A.cols,A.rows)); A.copyTo(roiA); cv::Mat roiB(tempB, cv::Rect(0,0,B.cols,B.rows)); B.copyTo(roiB); // FFT the padded A & B in-place; // use "nonzeroRows" hint for faster processing cv::dft(tempA, tempA, 0, A.rows); cv::dft(tempB, tempB, 0, B.rows); // multiply the spectrums; // the function handles packed spectrum representations well cv::mulSpectrums(tempA, tempB, tempA,0,true); // inverse transform // Even though all the result rows will be non-zero, // we need only the first corr.rows of them, and thus we // pass nonzeroRows == corr.rows cv::dft(tempA, tempA, cv::DFT_INVERSE + cv::DFT_SCALE, corr.rows); // now copy the result back to C. tempA(cv::Rect(0, 0, corr.cols, corr.rows)).copyTo(corr); do_matrix_to_image_flip(corr); }