/
ObjectMatcher.cs
140 lines (129 loc) · 4.88 KB
/
ObjectMatcher.cs
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Features2D;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using Emgu.CV.GPU;
namespace FaceTrackingBasics
{
public static class ObjectMatcher
{
public static Boolean Detect(ObjectDetectee observedScene, ObjectDetectee obj)
{
HomographyMatrix homography = null;
VectorOfKeyPoint observedKeyPoints;
Matrix<int> indices;
Matrix<byte> mask;
int k = 2;
double uniquenessThreshold = 0.8;
int testsPassed = 0;
// extract features from the observed image
observedKeyPoints = observedScene.objectKeyPoints;
Matrix<float> observedDescriptors = observedScene.objectDescriptors;
BruteForceMatcher<float> matcher = new BruteForceMatcher<float>(DistanceType.L2);
matcher.Add(obj.objectDescriptors);
if (observedDescriptors == null)
{
return false;
}
indices = new Matrix<int>(observedDescriptors.Rows, k);
using (Matrix<float> dist = new Matrix<float>(observedDescriptors.Rows, k))
{
matcher.KnnMatch(observedDescriptors, indices, dist, k, null);
mask = new Matrix<byte>(dist.Rows, 1);
mask.SetValue(255);
Features2DToolbox.VoteForUniqueness(dist, uniquenessThreshold, mask);
}
int nonZero = 0;
int nonZeroCount = CvInvoke.cvCountNonZero(mask);
if (nonZeroCount >= 4)
{
nonZeroCount = Features2DToolbox.VoteForSizeAndOrientation(obj.objectKeyPoints, observedKeyPoints, indices, mask, 1.5, 20);
if (nonZeroCount >= 4)
{
homography = Features2DToolbox.GetHomographyMatrixFromMatchedFeatures(obj.objectKeyPoints, observedKeyPoints, indices, mask, 2);
for (int i = 0; i < mask.Height; i++)
{
for (int j = 0; j < mask.Width; j++)
{
if (mask[i, j] != 0)
{
nonZero++;
}
}
}
if (nonZero > 4)
{
testsPassed++;
}
}
}
if (homography != null)
{
//draw a rectangle along the projected model
Rectangle rect = obj.objectImage.ROI;
PointF[] pts = new PointF[] {
new PointF(rect.Left, rect.Bottom),
new PointF(rect.Right, rect.Bottom),
new PointF(rect.Right, rect.Top),
new PointF(rect.Left, rect.Top)};
using (MemStorage m1 = new MemStorage())
using (MemStorage m2 = new MemStorage())
{
Contour<PointF> objPoly = new Contour<PointF>(m1);
Contour<PointF> scenePoly = new Contour<PointF>(m2);
pts.OrderBy(p => p.X).ThenBy(p => p.Y);
foreach (PointF i in pts)
{
objPoly.Push(i);
}
homography.ProjectPoints(pts);
pts.OrderBy(p => p.X).ThenBy(p => p.Y);
foreach (PointF i in pts)
{
scenePoly.Push(i);
}
double shapeMatch = CvInvoke.cvMatchShapes(objPoly, scenePoly, Emgu.CV.CvEnum.CONTOURS_MATCH_TYPE.CV_CONTOURS_MATCH_I3, 0);
double ratio = scenePoly.Area / objPoly.Area;
foreach (PointF i in pts)
{
if (i.X < 0 || i.Y < 0)
{
return false;
}
}
if (shapeMatch != 0 && shapeMatch <= 2)
{
testsPassed++;
}
if (ratio > 0.001 && ratio < 5.25)
{
testsPassed++;
}
if (!(Math.Abs(homography.Data[2, 0]) > .005 && Math.Abs(homography.Data[2, 1]) > .005))
{
testsPassed++;
}
if (testsPassed >= 2)
{
return true;
}
else
{
return false;
}
}
}
else
{
return false;
}
}
}
}