This repository has been archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShapeMatcher.cpp
138 lines (109 loc) · 4.45 KB
/
ShapeMatcher.cpp
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
#include "ShapeMatcher.h"
#include "InclusionTester.h"
#include "utils.h"
using namespace cv;
using namespace std;
ShapeMatcher::ShapeMatcher(HsvRange* matchColor)
{
this->matchColor = matchColor;
this->debugDraw = false;
}
bool ShapeMatcher::findMatch(vector<Contour> *contours, Mat *img, Contour *matchingContour, Rect *matchingBoundRect)
{
for(int i=0; i < contours->size(); i++) {
Contour *contour = &((*contours)[i]);
Rect rect = boundingRect(Mat(*contour));
// Is this shape an L shape?
bool isL = isMatch(contour, img);
if (!isL) continue;
// If this is the L shape we're looking for, draw green rectangle (and shape end-points) on source image!
if (this->debugDraw) {
drawRectangle(img, &rect, &GREEN);
contourDrawPoints(contour, img);
}
*matchingContour = *contour;
*matchingBoundRect = rect;
return true;
}
return false;
}
/***** BackwardsLMatcher *****/
bool BackwardsLMatcher::isMatch(Contour *contour, Mat *img)
{
vector<bool> expectedAnswers { false, false, true,
false, false, true,
true, true, true
};
InclusionTester it(3, expectedAnswers);
int matches = it.test(contour, img);
return (matches == 9);
}
/***** LMatcher *****/
bool LMatcher::isMatch(Contour *contour, Mat *img)
{
vector<bool> expectedAnswers { true, false, false,
true, false, false,
true, true, true
};
InclusionTester it(3, expectedAnswers);
int matches = it.test(contour, img);
return (matches == 9);
}
/***** YellowToteMatcher *****/
bool YellowToteMatcher::isMatch(Contour *contour, Mat *img)
{
// Can't use inclusion tester here because the tote has angles, it is not square.
// We probably want to look at the angles at the edgles
// Aspect ratio check
/*
Rect rect = boundingRect(Mat(*contour));
float whRatio = (float)rect.width / rect.height;
float whRatioMin = 1.2;
float whRatioMax = 2.2;
if (whRatio < whRatioMin || whRatio > whRatioMax) {
//cout << "Rejecting shape based on aspect-ratio: " << whRatio << endl;
return false;
}
*/
// Color check - Ensure a portion of the tote is the desired match color
/*
Mat hsvImg;
cvtColor(*img, hsvImg, CV_BGR2HSV);
vector<Point> testPoints(4);
testPoints.push_back( Point(rect.x + rect.width/20, rect.y + rect.height/20) );
testPoints.push_back( Point(rect.x + rect.width - rect.width/20, rect.y + rect.height/20) );
testPoints.push_back( Point(rect.x + rect.width - rect.width/20, rect.y + rect.height - rect.height/20) );
testPoints.push_back( Point(rect.x + rect.width/20, rect.y + rect.height - rect.height/20) );
testPoints.push_back( Point(rect.x + rect.width/10, rect.y + rect.height/10) );
testPoints.push_back( Point(rect.x + rect.width - rect.width/10, rect.y + rect.height/10) );
testPoints.push_back( Point(rect.x + rect.width - rect.width/10, rect.y + rect.height - rect.height/10) );
testPoints.push_back( Point(rect.x + rect.width/10, rect.y + rect.height - rect.height/10) );
int matchFound = matchColor->testPoints(&hsvImg, &testPoints);
if (matchFound < 1) return false;
*/
// Area check
//double minArea = (rect.width * rect.height) * 0.55;
//double area = contourArea(*contour, false);
//cout << " Area is " << area << " minArea=" << minArea << endl;
//if (area < minArea) {
// cout << "AREA FAIL!\n";
// return false;
// }
// Perimiter check
/*
double rectP = (rect.width * 2) + (rect.height * 2);
double minP = rectP * 0.75;
double maxP = rectP * 1.40;
double p = arcLength(*contour,true);
cout << "perim is: " << p << " " << "minP=" << minP << " maxP=" << maxP << endl;
if (p > maxP) cout << " FAILED max check!" << endl;
if (p < minP) cout << " FAILED min check!" << endl;
*/
vector<bool> expectedAnswers { true, true, true,
true, true, true,
true, true, true
};
InclusionTester it(3, expectedAnswers);
int matches = it.test(contour, img);
return (matches >= 7);
}