基于FFT的模板匹配

include “opencv2/core/core.hpp”

include “opencv2/imgproc/imgproc.hpp”

include “opencv2/highgui/highgui.hpp”

include

using namespace cv;
using namespace std;

Mat getTemplateImage(Mat sourceImage);
Mat getSquareSourceIntegrationImage(Mat sourceImage);

void convolveDFT(Mat A, Mat B, Mat& C)
{
// reallocate the output array if needed
//A.convertTo(A, CV_8U);
//tempB.convertTo(tempB, CV_8U);
//cv::imshow(“A”, A);
C.create(abs(A.rows - B.rows) + 1, abs(A.cols - B.cols) + 1, A.type());
Size dftSize;
// calculate the size of DFT transform
dftSize.width = getOptimalDFTSize(A.cols + B.cols - 1);
dftSize.height = getOptimalDFTSize(A.rows + B.rows - 1);

// allocate temporary buffers and initialize them with 0's
Mat tempA(dftSize, A.type(), Scalar::all(0));//initial 0
Mat tempB(dftSize, B.type(), Scalar::all(0));

// copy A and B to the top-left corners of tempA and tempB, respectively
Mat roiA(tempA, Rect(0, 0, A.cols, A.rows));
A.copyTo(roiA);
Mat roiB(tempB, Rect(0, 0, B.cols, B.rows));
B.copyTo(roiB);

// now transform the padded A & B in-place;
// use "nonzeroRows" hint for faster processing
//tempA.convertTo(tempA, CV_8U);
//tempB.convertTo(tempB, CV_8U);
//cv::imshow("tempA", tempA);
//cv::imshow("tempB", tempB);
//cv::waitKey();
double start = double(getTickCount());
dft(tempA, tempA, 0, A.rows);
dft(tempB, tempB, 0, B.rows);

// multiply the spectrums;
// the function handles packed spectrum representations well
mulSpectrums(tempA, tempB, tempA, DFT_COMPLEX_OUTPUT, true);
//mulSpectrums(tempA, tempB, tempA, DFT_REAL_OUTPUT);

// transform the product back from the frequency domain.
// Even though all the result rows will be non-zero,
// you need only the first C.rows of them, and thus you
// pass nonzeroRows == C.rows
dft(tempA, tempA, DFT_INVERSE + DFT_SCALE, C.rows);

// now copy the result back to C.
tempA(Rect(0, 0, C.cols, C.rows)).copyTo(C);
double duration_ms = (double(getTickCount()) - start) * 1000 / getTickFrequency();//计时
cout << "time consu
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值