原理见opencv学习(三十八)之图像模板匹配matchTemplate()
下面是平方差匹配法CV_TM_SQDIFF的自己实现:
/**
* @description: 模板匹配
* @param src 输入图像
* @param temp 模板图像
* @param dst 输出图像
*/
void matchtemplate(cv::Mat& src, cv::Mat& temp, cv::Mat& dst)
{
for (int i = 0; i < dst.cols; ++i)
{
for (int j = 0; j < dst.rows; ++j)
{
int R = 0;
for (int m = 0; m < temp.cols; ++m)
{
for (int n = 0; n < temp.rows; ++n)
{
R += pow(src.at<uchar>(j + n, i + m) - temp.at<uchar>(n, m), 2);
}
}
dst.at<int>(j, i) = R;
}
}
}