具體應用
https://blog.csdn.net/kyjl888/article/details/85060883
OpenCV中提供了幾個與輪廓相關的函數:
findContours():從二值圖像中尋找輪廓
drawContours():繪制輪廓
matchShape():使用Hu矩進行輪廓匹配
下面是一個使用這些函數的小例子,測試圖片為:
test3_c.jpg如下:

test4_c.jpg如下:

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main() {
string path1 = "images/test3_c.jpg";
string path2 = "images/test4_c.jpg";
Mat image1 = imread(path1, IMREAD_GRAYSCALE);
Mat image2 = imread(path2, IMREAD_GRAYSCALE);
image1 = 255 - image1; // 反色
image2 = 255 - image2;
imshow("1", image1); // 顯示反色后的圖像
imshow("2", image2);
Mat image1_copy = imread(path1);
Mat image2_copy = imread(path2);
// CV_RETR_EXTERNAL 檢測外輪廓
vector<vector<Point>> contours1, contours2;
findContours(image1, contours1, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
findContours(image2, contours2, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
drawContours(image1_copy, contours1, -1, Scalar(0, 255, 0), 2, 8);
drawContours(image2_copy, contours2, -1, Scalar(0, 255, 0), 2, 8);
imshow("輪廓1", image1_copy);
imshow("輪廓2", image2_copy);
//返回輪廓之間的匹配度, rate越小越相似
double rate = matchShapes(contours1[0], contours2[0], CV_CONTOURS_MATCH_I1, 0);
cout << rate << endl;
waitKey(0);
return 0;
}

