Skip to content

Commit 0cb1f07

Browse files
committed
upd tracker
1 parent 9044c0c commit 0cb1f07

File tree

3 files changed

+91
-48
lines changed

3 files changed

+91
-48
lines changed

blob/array_tracker.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ package blob
22

33
import (
44
"image"
5-
"image/color"
65
"math"
76

87
uuid "github.com/satori/go.uuid"
9-
"gocv.io/x/gocv"
108
)
119

1210
// Blobies - Array of blobs
@@ -15,6 +13,8 @@ type Blobies struct {
1513
maxNoMatch int
1614
minThresholdDistance float64
1715
maxPointsInTrack int
16+
17+
DrawingOptions *DrawOptions
1818
}
1919

2020
// NewBlobiesDefaults - Constructor for Blobies (default values)
@@ -30,6 +30,7 @@ func NewBlobiesDefaults() *Blobies {
3030
maxNoMatch: 5,
3131
minThresholdDistance: 15,
3232
maxPointsInTrack: 10,
33+
DrawingOptions: NewDrawOptionsDefault(),
3334
}
3435
}
3536

@@ -39,6 +40,7 @@ func (bt *Blobies) MatchToExisting(rects []image.Rectangle) {
3940
blobies := make([]*Blobie, len(rects))
4041
for i := range blobies {
4142
blobies[i] = NewBlobie(rects[i], bt.maxPointsInTrack)
43+
blobies[i].drawingOptions = bt.DrawingOptions
4244
}
4345

4446
for i := range blobies {
@@ -98,18 +100,6 @@ func (bt *Blobies) deregister(guid uuid.UUID) {
98100
delete(bt.Objects, guid)
99101
}
100102

101-
// DrawTrack - Draw blob's track
102-
func (b *Blobie) DrawTrack(mat *gocv.Mat, optionalText string) {
103-
gocv.Rectangle(mat, (*b).CurrentRect, color.RGBA{255, 255, 0, 0}, 2)
104-
if (*b).isStillBeingTracked {
105-
for i := range (*b).Track {
106-
gocv.Circle(mat, (*b).Track[i], 4, color.RGBA{255, 0, 0, 0}, 1)
107-
}
108-
pt := image.Pt((*b).CurrentRect.Min.X, (*b).CurrentRect.Min.Y)
109-
gocv.PutText(mat, optionalText, pt, gocv.FontHersheyPlain, 1.2, color.RGBA{0, 255, 0, 0}, 2)
110-
}
111-
}
112-
113103
// IsCrossedTheLine - Check if blob crossed the HORIZONTAL line
114104
func (b *Blobie) IsCrossedTheLine(vertical, leftX, rightX int, direction bool) bool {
115105
trackLen := len(b.Track)

blob/blob.go

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"math"
66

77
uuid "github.com/satori/go.uuid"
8+
"gocv.io/x/gocv"
89
)
910

1011
// Blobie - Main blob structure
@@ -23,7 +24,8 @@ type Blobie struct {
2324
PredictedNextPosition image.Point
2425

2526
// For array tracker
26-
crossedLine bool
27+
drawingOptions *DrawOptions
28+
crossedLine bool
2729
}
2830

2931
// NewBlobie - Constructor for Blobie (default values)
@@ -113,39 +115,20 @@ func (b *Blobie) PredictNextPosition(n int) {
113115
(*b).PredictedNextPosition.Y = (*b).Track[len((*b).Track)-1].Y + deltaY
114116
}
115117

116-
// // IsCrossedTheLine - Check if blob crossed the HORIZONTAL line
117-
// func (b *Blobie) IsCrossedTheLine(horizontal int, counter *int, direction bool) bool {
118-
// if (*b).isStillBeingTracked == true && len((*b).Track) >= 2 && (*b).counted == false {
119-
// prevFrame := len((*b).Track) - 2
120-
// currFrame := len((*b).Track) - 1
121-
// if direction {
122-
// if (*b).Track[prevFrame].Y <= horizontal && (*b).Track[currFrame].Y > horizontal { // TO us
123-
// *counter++
124-
// b.AsCounted()
125-
// return true
126-
// }
127-
// } else {
128-
// if (*b).Track[prevFrame].Y > horizontal && (*b).Track[currFrame].Y <= horizontal { // FROM us
129-
// *counter++
130-
// b.AsCounted()
131-
// return true
132-
// }
133-
// }
134-
// }
135-
// return false
136-
// }
137-
138-
// // DrawTrack - Draw blob's track
139-
// func (b *Blobie) DrawTrack(mat *gocv.Mat, optionalText string) {
140-
// if (*b).isStillBeingTracked == true {
141-
// for i := range (*b).Track {
142-
// gocv.Circle(mat, (*b).Track[i], 4, color.RGBA{255, 0, 0, 0}, 1)
143-
// }
144-
// gocv.Rectangle(mat, (*b).CurrentRect, color.RGBA{0, 255, 255, 0}, 2)
145-
// pt := image.Pt((*b).CurrentRect.Min.X, (*b).CurrentRect.Min.Y)
146-
// gocv.PutText(mat, optionalText, pt, gocv.FontHersheyPlain, 1.2, color.RGBA{0, 255, 0, 0}, 2)
147-
// }
148-
// }
118+
// DrawTrack - Draw blob's track
119+
func (b *Blobie) DrawTrack(mat *gocv.Mat, optionalText string) {
120+
if b.drawingOptions == nil {
121+
b.drawingOptions = NewDrawOptionsDefault()
122+
}
123+
gocv.Rectangle(mat, b.CurrentRect, b.drawingOptions.BBoxColor.Color, b.drawingOptions.BBoxColor.Thickness)
124+
if b.isStillBeingTracked {
125+
for i := range b.Track {
126+
gocv.Circle(mat, b.Track[i], b.drawingOptions.CentroidColor.Radius, b.drawingOptions.CentroidColor.Color, b.drawingOptions.CentroidColor.Thickness)
127+
}
128+
pt := image.Pt(b.CurrentRect.Min.X, b.CurrentRect.Min.Y)
129+
gocv.PutText(mat, optionalText, pt, gocv.FontHersheyPlain, b.drawingOptions.TextColor.Scale, b.drawingOptions.TextColor.Color, b.drawingOptions.TextColor.Thickness)
130+
}
131+
}
149132

150133
func min(x, y int) int {
151134
if x < y {

blob/draw_options.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package blob
2+
3+
import (
4+
"image/color"
5+
6+
"gocv.io/x/gocv"
7+
)
8+
9+
// DrawOptions Options for blob.DrawTrack function
10+
type DrawOptions struct {
11+
BBoxColor DrawBBoxOptions
12+
CentroidColor DrawCentroidOptions
13+
TextColor DrawTextOptions
14+
}
15+
16+
// DrawBBoxOptions Options for bounding box of blob
17+
type DrawBBoxOptions struct {
18+
Color color.RGBA
19+
Thickness int
20+
}
21+
22+
// DrawCentroidOptions Options for centroid of blob
23+
type DrawCentroidOptions struct {
24+
Color color.RGBA
25+
Radius int
26+
Thickness int
27+
}
28+
29+
// DrawTextOptions Options for text if top left corner bounding box of blob
30+
type DrawTextOptions struct {
31+
Color color.RGBA
32+
Scale float64
33+
Thickness int
34+
Font gocv.HersheyFont
35+
}
36+
37+
// NewDrawOptionsDefault Returns pointer to new DrawOptions (default)
38+
func NewDrawOptionsDefault() *DrawOptions {
39+
bbox := DrawBBoxOptions{
40+
Color: color.RGBA{255, 255, 0, 0},
41+
Thickness: 2,
42+
}
43+
centroid := DrawCentroidOptions{
44+
Color: color.RGBA{255, 0, 0, 0},
45+
Radius: 4,
46+
Thickness: 2,
47+
}
48+
text := DrawTextOptions{
49+
Color: color.RGBA{0, 255, 0, 0},
50+
Scale: 1.2,
51+
Thickness: 2,
52+
Font: gocv.FontHersheyPlain,
53+
}
54+
opts := DrawOptions{
55+
BBoxColor: bbox,
56+
CentroidColor: centroid,
57+
TextColor: text,
58+
}
59+
return &opts
60+
}
61+
62+
// NewDrawOptions Returns pointer to new DrawOptions (custom)
63+
func NewDrawOptions(bbox DrawBBoxOptions, centroid DrawCentroidOptions, text DrawTextOptions) *DrawOptions {
64+
opts := DrawOptions{
65+
BBoxColor: bbox,
66+
CentroidColor: centroid,
67+
TextColor: text,
68+
}
69+
return &opts
70+
}

0 commit comments

Comments
 (0)