c++
#include"all.h"
using namespace std;
using namespace cv;
void MyClass::day039() {
Mat origin = read(PATH + "images\\origin.jpg");
Mat temp = read(PATH + "images\\template.jpg");
int r_row = origin.rows - temp.rows + 1; int r_col = origin.cols - temp.cols + 1;
Mat result = Mat::zeros(Size(r_col, r_row), CV_32FC1);
matchTemplate(origin, temp, result, TM_CCOEFF_NORMED);
imshow("result", result);
const float t = 0.95;
int h = result.rows; int w = result.cols;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
float v = result.at<float>(i, j);
if (v > t) {
rectangle(origin, Point(j, i), Point(j + temp.cols, i + temp.rows), Scalar(255, 0, 0),
1, 8, 0);
}
}
}
imshow("template matched result", origin);
waitKey(0);
}
c++中的新知识点
matchTemplate():用于在某图片中寻找temp图片的匹配,实际上是个卷积操作,结果为某位置领域图片和temp匹配的概率
rectangle():给出左上角和右下角坐标后,在该位置画指定大小的框