-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvirtual_polygons.go
More file actions
316 lines (292 loc) · 10.6 KB
/
Copy pathvirtual_polygons.go
File metadata and controls
316 lines (292 loc) · 10.6 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package odam
import (
"image"
"image/color"
"math"
blob "github.com/LdDl/gocv-blob/v2/blob"
"gocv.io/x/gocv"
)
// VIRTUAL_POLYGON_TYPE Alias to int
// @Warning: Should be deprecated
type VIRTUAL_POLYGON_TYPE int
const (
// @Warning: Should be deprecated
// CONVEX_POLYGON See ref. https://en.wikipedia.org/wiki/Convex_polygon
CONVEX_POLYGON = VIRTUAL_POLYGON_TYPE(iota + 1)
// CONCAVE_POLYGON See ref. https://en.wikipedia.org/wiki/Concave_polygon
CONCAVE_POLYGON
)
// VirtualPolygon Detection polygon attributes
type VirtualPolygon struct {
// Polygon's identifier (inherited by wrapping structure)
ID int64 `json:"-"`
// Color of stroke line
Color color.RGBA `json:"-"`
// Information about coordinates [scaled]
Coordinates []image.Point `json:"-"`
// Information about coordinates [non-scaled]
SourceCoordinates []image.Point `json:"-"`
// Type of virtual polygon: could be convex or concave
// @Warning: Should be deprecated
PolygonType VIRTUAL_POLYGON_TYPE `json:"-"`
gocvPoly gocv.PointVector
gocvPolyDraw gocv.PointsVector
}
// Constructor for VirtualPolygon
// (x1, y1) - Left
// (x2, y2) - Right
func NewVirtualPolygon(polygonID int64, pairs ...image.Point) *VirtualPolygon {
vpolygon := VirtualPolygon{
ID: polygonID,
Coordinates: make([]image.Point, len(pairs)),
SourceCoordinates: make([]image.Point, len(pairs)),
}
for i := range pairs {
vpolygon.Coordinates[i] = image.Point{X: pairs[i].X, Y: pairs[i].Y}
vpolygon.SourceCoordinates[i] = image.Point{X: pairs[i].X, Y: pairs[i].Y}
}
if vpolygon.isConvex() {
vpolygon.PolygonType = CONVEX_POLYGON
} else {
vpolygon.PolygonType = CONCAVE_POLYGON
}
vpolygon.gocvPolyDraw = gocv.NewPointsVectorFromPoints([][]image.Point{vpolygon.Coordinates})
vpolygon.gocvPoly = gocv.NewPointVectorFromPoints(vpolygon.Coordinates)
return &vpolygon
}
// Draw Draw virtual polygon on image
func (vpolygon *VirtualPolygon) Draw(img *gocv.Mat) {
gocv.Polylines(img, vpolygon.gocvPolyDraw, true, vpolygon.Color, 2)
}
// isConvex check if polygon either convex or concave
// @Warning: Should be deprecated
func (vpolygon *VirtualPolygon) isConvex() bool {
// time complexity: O(n)
n := len(vpolygon.Coordinates)
if n < 3 {
// Well, this is not that strange if polygon have been prepared wrongly
return false
}
previousCrossProduct := 0
currentCrossProduct := 0
for i := range vpolygon.Coordinates {
currentCrossProduct = crossProduct(vpolygon.Coordinates[i], vpolygon.Coordinates[(i+1)%n], vpolygon.Coordinates[(i+2)%n])
if currentCrossProduct != 0 {
if currentCrossProduct*previousCrossProduct < 0 {
return false
} else {
previousCrossProduct = currentCrossProduct
}
}
}
return true
}
// crossProduct Cross product of two vectors
// @Warning: Should be deprecated
func crossProduct(a image.Point, b image.Point, c image.Point) int {
// direction of vector b.x -> a.x
x1 := b.X - a.X
// direction of vector b.y -> a.y
y1 := b.Y - a.Y
// direction of vector c.x -> a.x
x2 := c.X - a.X
// direction of vector c.y -> a.y
y2 := c.Y - a.Y
return x1*y2 - y1*x2
}
// Scale Scales down (so scale factor can be > 1.0 ) virtual polygon
// (scaleX, scaleY) - How to scale source (x1,y1) and (x2,y2) coordinates
// Important notice:
// 1. Source coordinates won't be modified
// 2. Source coordinates would be used for scaling. So you can't scale polygon multiple times
func (vpolygon *VirtualPolygon) Scale(scaleX, scaleY float64) {
for i := range vpolygon.Coordinates {
vpolygon.Coordinates[i].X = int(math.Round(float64(vpolygon.Coordinates[i].X) / scaleX))
vpolygon.Coordinates[i].Y = int(math.Round(float64(vpolygon.Coordinates[i].Y) / scaleY))
}
vpolygon.gocvPolyDraw = gocv.NewPointsVectorFromPoints([][]image.Point{vpolygon.Coordinates})
vpolygon.gocvPoly = gocv.NewPointVectorFromPoints(vpolygon.Coordinates)
}
// BlobEntered Checks if an object has entered the polygon
// Let's clarify for future questions: we are assuming the object is represented by a center, not a bounding box
// So object has entered polygon when its center had entered polygon too
func (vpolygon *VirtualPolygon) BlobEntered(b blob.Blobie) bool {
// Early exit if blob already have same property
if prop, ok := b.GetProperty("polygon_id"); ok {
switch propStr := prop.(type) {
case int64:
if propStr == vpolygon.ID {
return false
}
default:
return false
}
}
track := b.GetTrack()
n := len(track)
if n < 2 {
// Blob can't have one coordinates pair in track
return false
}
lastPosition := track[len(track)-1]
secondLastPosition := track[len(track)-2]
// If P(xN-1,yN-1) is not inside of polygon and P(xN,yN) is inside of polygon then object has entered the polygon
if !vpolygon.ContainsPoint(secondLastPosition) && vpolygon.ContainsPoint(lastPosition) {
b.SetProperty("polygon_id", vpolygon.ID)
return true
}
return false
}
// BlobLeft Checks if an object has left the polygon
// Let's clarify for future questions: we are assuming the object is represented by a center, not a bounding box
// So object has left polygon when its center had left polygon too
func (vpolygon *VirtualPolygon) BlobLeft(b blob.Blobie) bool {
track := b.GetTrack()
n := len(track)
if n < 2 {
// Blob can't have one coordinates pair in track
return false
}
lastPosition := track[len(track)-1]
secondLastPosition := track[len(track)-2]
// If P(xN-1,yN-1) is inside of polygon and P(xN,yN) is not inside of polygon then object has left the polygon
if vpolygon.ContainsPoint(secondLastPosition) && !vpolygon.ContainsPoint(lastPosition) {
b.SetProperty("polygon_id", -1)
return true
}
return false
}
// ContainsBlob Checks if polygon contains the given object
// Let's clarify for future questions: we are assuming the object is represented by a center, not a bounding box
// So object is inside of polygon when its center is inside of polygon too
func (vpolygon *VirtualPolygon) ContainsBlob(b blob.Blobie) bool {
return vpolygon.ContainsPoint(b.GetCenter())
}
// ContainsPoint Checks if polygon contains the given point
func (vpolygon *VirtualPolygon) ContainsPoint(p image.Point) bool {
return gocv.PointPolygonTest(vpolygon.gocvPoly, p, true) >= 0
}
// convexContainsPoint Checks if CONVEX polygon contains the given point
// Heavily inspired by this: https://github.com/LdDl/gocv-blob/blob/master/v2/blob/line_cross.go#L5
// @Warning: Should be deprecated
func (vpolygon *VirtualPolygon) convexContainsPoint(p image.Point) bool {
n := len(vpolygon.Coordinates)
extremePoint := image.Point{
X: 99999, // @todo: math.maxInt could lead to overflow obviously. Need good workaround. PRs are welcome
Y: p.Y,
}
intersectionsCnt := 0
previous := 0
for {
current := (previous + 1) % n
// Check if the segment from given point P to extreme point intersects with the segment from polygon point on previous interation to polygon point on current interation
if isIntersects(
vpolygon.Coordinates[previous].X, vpolygon.Coordinates[previous].Y,
vpolygon.Coordinates[current].X, vpolygon.Coordinates[current].Y,
p.X, p.Y,
extremePoint.X, extremePoint.Y,
) {
orientation := getOrientation(
vpolygon.Coordinates[previous].X, vpolygon.Coordinates[previous].Y,
p.X, p.Y,
vpolygon.Coordinates[current].X, vpolygon.Coordinates[current].Y,
)
// If given point P is collinear with segment from polygon point on previous interation to polygon point on current interation
if orientation == Collinear {
// then check if it is on segment
// 'True' will be returns if it lies on segment. Otherwise 'False' will be returned
return isOnSegment(vpolygon.Coordinates[previous].X, vpolygon.Coordinates[previous].Y, p.X, p.Y, vpolygon.Coordinates[current].X, vpolygon.Coordinates[current].Y)
}
intersectionsCnt++
}
previous = current
if previous == 0 {
break
}
}
// If ray intersects even number of times then return true
// Otherwise return false
if intersectionsCnt%2 == 1 {
return true
}
return false
}
// concaveContainsPoint Checks if CONCAVE polygon contains the given point
func (vpolygon *VirtualPolygon) concaveContainsPoint(p image.Point) bool {
// @todo
// @Warning: Should be deprecated, so no todo :P
return false
}
// isOnSegment Checks if point Q lies on segment PR
// Input: three colinear points Q, Q and R
// @Warning: Should be deprecated
func isOnSegment(Px, Py, Qx, Qy, Rx, Ry int) bool {
if Qx <= maxInt(Px, Rx) && Qx >= minInt(Px, Rx) && Qy <= maxInt(Py, Ry) && Qy >= minInt(Py, Ry) {
return true
}
return false
}
// @Warning: Should be deprecated
type PointsOrientation int
const (
// @Warning: Should be deprecated
Collinear = iota
Clockwise
CounterClockwise
)
// getOrientation Gets orientations of points P -> Q -> R.
// Possible output values: Collinear / Clockwise or CounterClockwise
// Input: points P, Q and R in provided order
// @Warning: Should be deprecated
func getOrientation(Px, Py, Qx, Qy, Rx, Ry int) PointsOrientation {
val := (Qy-Py)*(Rx-Qx) - (Qx-Px)*(Ry-Qy)
if val == 0 {
return Collinear
}
if val > 0 {
return Clockwise
}
return CounterClockwise // if it's neither collinear nor clockwise
}
// isIntersects Checks if segments intersect each other
// Input:
// firstPx, firstPy, firstQx, firstQy === first segment
// secondPx, secondPy, secondQx, secondQy === second segment
/*
Notation
P1 = (firstPx, firstPy)
Q1 = (firstQx, firstQy)
P2 = (secondPx, secondPy)
Q2 = (secondQx, secondQy)
*/
// @Warning: Should be deprecated
func isIntersects(firstPx, firstPy, firstQx, firstQy, secondPx, secondPy, secondQx, secondQy int) bool {
// Find the four orientations needed for general case and special ones
o1 := getOrientation(firstPx, firstPy, firstQx, firstQy, secondPx, secondPy)
o2 := getOrientation(firstPx, firstPy, firstQx, firstQy, secondQx, secondQy)
o3 := getOrientation(secondPx, secondPy, secondQx, secondQy, firstPx, firstPy)
o4 := getOrientation(secondPx, secondPy, secondQx, secondQy, firstQx, firstQy)
// General case
if o1 != o2 && o3 != o4 {
return true
}
/* Special cases */
// P1, Q1, P2 are colinear and P2 lies on segment P1-Q1
if o1 == Collinear && isOnSegment(firstPx, firstPy, secondPx, secondPy, firstQx, firstQy) {
return true
}
// P1, Q1 and Q2 are colinear and Q2 lies on segment P1-Q1
if o2 == Collinear && isOnSegment(firstPx, firstPy, secondQx, secondQy, firstQx, firstQy) {
return true
}
// P2, Q2 and P1 are colinear and P1 lies on segment P2-Q2
if o3 == Collinear && isOnSegment(secondPx, secondPy, firstPx, firstPy, secondQx, secondQy) {
return true
}
// P2, Q2 and Q1 are colinear and Q1 lies on segment P2-Q2
if o4 == Collinear && isOnSegment(secondPx, secondPy, firstQx, firstQy, secondQx, secondQy) {
return true
}
// Segments do not intersect
return false
}