#include "opencv2/opencv.hpp"
using namespace cv;
#include <iostream>
using namespace std;
int main()
{
Mat templateImg = imread("test2.jpg", CV_LOAD_IMAGE_COLOR);
Mat copyImg1 = templateImg.clone();
cvtColor(templateImg, templateImg, CV_BGR2GRAY);
threshold(templateImg, templateImg, 100, 255, CV_THRESH_BINARY);
imshow("thresh1", templateImg);
vector<vector<Point>> contours1;
findContours(templateImg, contours1, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
drawContours(copyImg1, contours1, -1, Scalar(0, 255, 0), 2, 8);
Mat testImg = imread("test1.jpg", CV_LOAD_IMAGE_COLOR);
Mat copyImg2 = testImg.clone();
cvtColor(testImg, testImg, CV_BGR2GRAY);
threshold(testImg, testImg, 100, 255, CV_THRESH_BINARY);
imshow("thresh2", testImg);
vector<vector<Point>> contours2;
findContours(testImg, contours2, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
for (int i = 0; i < contours2.size(); i++)
{
double a0 = matchShapes(contours1[0], contours2[i], CV_CONTOURS_MATCH_I1, 0);
cout << "模版轮廓与待测试图像轮廓" << i << "的相似度:" << a0 << endl;
if (a0<0.08)
{
drawContours(copyImg2, contours2, i, Scalar(0, 255, 0), 2, 8);
}
imshow("copyImg2", copyImg2);
if (waitKey(0) == 27)
{
cout << "ESC退出" << endl;
break;
}
}
waitKey(0);
return 0;
}