前言
本文将使用OpenCV C++ 基于Hu矩进行轮廓匹配。
一、查找轮廓
原图
测试图
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
vector<vector<Point>>findContour(Mat Image) { Mat gray; cvtColor(Image, gray, COLOR_BGR2GRAY); Mat thresh; threshold(gray, thresh, 0, 255, THRESH_BINARY_INV | THRESH_OTSU); vector<vector<Point>>contours; findContours(thresh, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE); vector<vector<Point>>EffectConts; for ( int i = 0; i < contours.size(); i++) { double area = contourArea(contours[i]); if (area > 1000) { EffectConts.push_back(contours[i]); } } return EffectConts; } |
如图所示,这就是找到的最外轮廓。接下来,我们基于轮廓进行匹配。
二、计算Hu矩
OpenCV提供moments API计算图像的中心矩;HuMoments API用于中心矩计算Hu矩。关于moments HuMoments相关知识请大家自行查找。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
Moments m_test = moments(test_contours[0]); Mat hu_test; HuMoments(m_test, hu_test); double MinDis = 1000; int MinIndex = 0; for ( int i = 0; i < src_contours.size(); i++) { Moments m_src = moments(src_contours[i]); Mat hu_src; HuMoments(m_src, hu_src); double dist = matchShapes(hu_test, hu_src, CONTOURS_MATCH_I1, 0); if (dist < MinDis) { MinDis = dist; MinIndex = i; } } |
上面代码段大致思路是:首先计算测试图的Hu矩;然后使用一个for循环计算原图中所有轮廓的Hu矩,依次计算两Hu矩的相似程度。在这里使用matchShapes API计算两个Hu矩。函数返回值代表两Hu矩的相似程度。完全相同返回值为0。即这里通过计算两Hu矩的相似程度,找到返回值最小的那个作为成功匹配。
三、显示效果
1
2
3
4
5
|
drawContours(src, src_contours, MinIndex, Scalar(0, 255, 0), 2); Rect rect = boundingRect(src_contours[MinIndex]); rectangle(src, rect, Scalar(0, 0, 255), 2); |
最终效果如图所示。
四、源码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
#include<iostream> #include<opencv2/opencv.hpp> using namespace std; using namespace cv; vector<vector<Point>>findContour(Mat Image) { Mat gray; cvtColor(Image, gray, COLOR_BGR2GRAY); Mat thresh; threshold(gray, thresh, 0, 255, THRESH_BINARY_INV | THRESH_OTSU); vector<vector<Point>>contours; findContours(thresh, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE); vector<vector<Point>>EffectConts; for ( int i = 0; i < contours.size(); i++) { double area = contourArea(contours[i]); if (area > 1000) { EffectConts.push_back(contours[i]); } } return EffectConts; } int main() { Mat src = imread( "test/hand.jpg" ); Mat test = imread( "test/test-3.jpg" ); if (src.empty() || test.empty()) { cout << "No Image!" << endl; system ( "pause" ); return -1; } vector<vector<Point>>src_contours; vector<vector<Point>>test_contours; src_contours = findContour(src); test_contours = findContour(test); Moments m_test = moments(test_contours[0]); Mat hu_test; HuMoments(m_test, hu_test); double MinDis = 1000; int MinIndex = 0; for ( int i = 0; i < src_contours.size(); i++) { Moments m_src = moments(src_contours[i]); Mat hu_src; HuMoments(m_src, hu_src); double dist = matchShapes(hu_test, hu_src, CONTOURS_MATCH_I1, 0); if (dist < MinDis) { MinDis = dist; MinIndex = i; } } drawContours(src, src_contours, MinIndex, Scalar(0, 255, 0), 2); Rect rect = boundingRect(src_contours[MinIndex]); rectangle(src, rect, Scalar(0, 0, 255), 2); imshow( "test" , test); imshow( "Demo" , src); waitKey(0); system ( "pause" ); return 0; } |
总结
本文使用OpenCV C++基于Hu矩轮廓匹配,关键步骤有以下几点。
1、查找轮廓。在这里,我是基于最外轮廓进行匹配。
2、计算轮廓的Hu矩,然后使用matchShapes计算两Hu矩的距离,以此来判断匹配程度。
到此这篇关于OpenCV实战之基于Hu矩实现轮廓匹配的文章就介绍到这了,更多相关OpenCV Hu矩轮廓匹配内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/Zero___Chen/article/details/122008672