OpenCV金字塔模板匹配c++调用拿个函数
时间: 2023-12-03 17:02:05 浏览: 87
以下是使用OpenCV实现金字塔模板匹配的示例代码:
```c++
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
void pyramidTemplateMatch(Mat& src, Mat& templ, float scale, int maxLevel)
{
Mat result;
for (int level = 0; level <= maxLevel; level++)
{
Mat img;
resize(src, img, Size(round(src.cols * pow(scale, level)), round(src.rows * pow(scale, level))));
matchTemplate(img, templ, result, TM_CCOEFF_NORMED);
double minVal, maxVal;
Point minLoc, maxLoc;
minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc);
if (level == maxLevel)
{
rectangle(src, Rect(maxLoc.x / pow(scale, level), maxLoc.y / pow(scale, level), templ.cols, templ.rows), Scalar(0, 0, 255), 2);
}
}
}
int main()
{
Mat src = imread("test.jpg");
Mat templ = imread("template.jpg");
if (src.empty() || templ.empty())
{
cerr << "Can't read image!" << endl;
return -1;
}
float scale = 0.8;
int maxLevel = 3;
pyramidTemplateMatch(src, templ, scale, maxLevel);
imshow("result", src);
waitKey();
return 0;
}
```
该函数将输入图像和模板图像缩放到不同的金字塔层级,然后使用OpenCV的matchTemplate函数进行模板匹配,最后在原始图像上绘制矩形框以标记匹配位置。函数的参数包括输入图像、模板图像、缩放比例和最大金字塔层数。
阅读全文