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
/
Copy pathContour.cpp
209 lines (176 loc) · 6.23 KB
/
Contour.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "Contour.h"
#include "utils.h"
/* Draw a single contour to the given matrix. */
void drawContour(Contour *contour, Mat *img)
{
vector<Contour> contours(0);
contours.push_back(*contour);
Scalar color = Scalar( 200, 200, 100);
drawContours(*img, contours, 0, color, 2);
}
/* contourDrawList(Contour*, String, Size sz)
* Draws each contour in the list in a separate random color. The reusults are
* displayed in a new window.
*
* contourDrawLines(Contour*, String, Mat*)
* Same as above, but draws into the Matrix the caller provides and does not
* display the result in a window.
*
* If called in a loop winTitle should be something like:
* "ContourPoints-" + to_string(i)
* to avoid duplicate window names.
*
* sz - Size of the Mat to create. Get this from your source image:
* srcMat.size()
*/
void contourDrawList(vector<Contour> *contourList, String winTitle, Size sz)
{
Mat img(sz, CV_8UC3);
drawContourList(contourList, &img);
imshow(winTitle, img);
}
void drawContourList(vector<Contour> *contourList, Mat *img)
{
for (int i=0; i < contourList->size(); i++) {
drawContours(*img, *contourList, i, randColor(), 2);
}
}
/* contourDrawPoints(Contour*, String, Size sz)
* Draws and Displays a point for each point within a contour
* in a new window. All points are drawn in redish color.
*
* contourDrawPoints(Contour*, String, Mat*)
* Same as above, but draws into the Matrix the caller provides and does not
* display the result in a window.
*
* If called in a loop winTitle should be something like:
* "ContourPoints-" + to_string(i)
* to avoid duplicate window names.
*
* sz - Size of the Mat to create. Get this from your source image:
* srcMat.size()
*/
void contourDrawPoints(Contour *contour, String winTitle, Size sz)
{
Mat img(sz, CV_32FC3);
contourDrawPoints(contour, &img);
imshow(winTitle, img);
}
void contourDrawPoints(Contour *contour, Mat *img)
{
Scalar color = Scalar( 0, 0, 255);
int i;
for (i=0; i < contour->size()-1; i++) {
Point point = (*contour)[i];
cv::line(*img, point, point, color, 2);
}
}
/* contourDrawLines(Contour*, String, Size sz)
* Draws and Displays line segments connecting all the points within a contour
* in a new window. The lines are drawn between adjacent points within the
* contour vector, and a line is drawn from the last point to the first point
* to connect/close the shape created by the contour. All lines are drawn in
* a random color except for the final closing line which is red.
*
* contourDrawLines(Contour*, String, Mat*)
* Same as above, but draws into the Matrix the caller provides and does not
* display the result in a window.
*
* If called in a loop winTitle should be something like:
* "ContourPoints-" + to_string(i)
* to avoid duplicate window names.
*
* sz - Size of the Mat to create. Get this from your source image:
* srcMat.size()
*/
void contourDrawLines(Contour *contour, String winTitle, Size sz)
{
Mat img(sz, CV_32FC3);
contourDrawLines(contour, &img);
imshow(winTitle, img);
}
void contourDrawLines(Contour *contour, Mat *img)
{
int i;
for (i=0; i < contour->size()-1; i++) {
Point point = (*contour)[i];
Point point2 = (*contour)[i+1];
cv::line(*img, point, point2, randColor(), 2);
}
cv::line(*img, (*contour)[i], (*contour)[0], Scalar(0,0,255), 2);
}
/* contourInclusionTest(Contour*, String, Size sz)
* Colors a new Matrix such that any inside the polygon created by the given
* contour will be gree, anything outside will be red and anything landing on
* an edge will be white.
*
* contourInclusionTest(Contour*, String, Mat*)
* Same as above, but draws into the Matrix the caller provides and does not
* display the result in a window.
*
* If called in a loop winTitle should be something like:
* "ContourPoints-" + to_string(i)
* to avoid duplicate window names.
*
* sz - Size of the Mat to create. Get this from your source image:
* srcMat.size()
*/
void contourInclusionTest(Contour *contour, String winTitle, Size sz)
{
Mat img(sz, CV_8UC3);
contourInclusionTest(contour, &img);
imshow(winTitle, img);
}
void contourInclusionTest(Contour *contour, Mat *img)
{
int xMax = img->rows-1;
int yMax = img->cols-1;
for (int y=0; y <= yMax; y++) {
for (int x=0; x <= xMax; x++) {
Point point = Point(y,x);
int rc = pointPolygonTest(*contour, point, false);
if (rc < 0)
cv::line(*img, point, point, Scalar(0,0,255), 1);
else if (rc > 0)
cv::line(*img, point, point, Scalar(0,255,0), 1);
else
cv::line(*img, point, point, Scalar(0,0,0), 1);
}
}
}
vector<Contour> detectContoursColorFiltered(Mat *img, HsvRange *hrange, int minPoints, int minLength)
{
vector<Contour> contours, contoursApprox;
Mat hsvImg, maskImg;
cvtColor(*img, hsvImg, CV_BGR2HSV);
inRange(hsvImg, HSV_YELLOW.hsvMin, HSV_YELLOW.hsvMax, maskImg);
findContours(maskImg, contours, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
for(int i=0; i < contours.size(); i++) {
Contour contour = contours[i];
// Approximate polygonal shape
Contour approxShape(contour.size());
approxPolyDP(contour, approxShape, 3, true);
// Reject contours wilth a very small size
Rect rect = boundingRect(Mat(approxShape));
if (rect.width < minLength || rect.height < minLength)
continue;
// Reject contours that don't have enough points to be the correct shape
if (approxShape.size() < minPoints)
continue;
contoursApprox.push_back(contour);
}
return contoursApprox;
}
Point contourCentoid(Contour* contour)
{
Moments m = moments(*contour);
int cx = m.m10/m.m00;
int cy = m.m01/m.m00;
return Point(cx, cy);
}
bool contourNested(Contour *parent, Contour *child)
{
Point childCentoid = contourCentoid(child);
int rc = pointPolygonTest(*parent, childCentoid, false);
return (rc >= 0);
}