/
kfgocv.go
158 lines (127 loc) · 4.43 KB
/
kfgocv.go
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
package main
import (
"fmt"
"image"
"image/color"
"log"
"math"
"math/rand"
filter "github.com/milosgajdos/go-estimate"
"github.com/milosgajdos/go-estimate/estimate"
"github.com/milosgajdos/go-estimate/kalman/kf"
"github.com/milosgajdos/go-estimate/noise"
"github.com/milosgajdos/go-estimate/sim"
"github.com/milosgajdos/matrix"
"gocv.io/x/gocv"
"gonum.org/v1/gonum/mat"
)
func GetDotPos(center image.Point, r, angle float64) image.Point {
p := image.Pt(center.X, center.Y)
x := math.Cos(angle) * r
y := (-math.Sin(angle)) * r
return p.Add(image.Pt(int(x), int(y)))
}
func DrawMarker(img *gocv.Mat, center image.Point, c color.RGBA, d int) {
gocv.Line(img, image.Pt(center.X-d, center.Y-d), image.Pt(center.X+d, center.Y+d), c, 3)
gocv.Line(img, image.Pt(center.X+d, center.Y-d), image.Pt(center.X-d, center.Y+d), c, 3)
}
func main() {
A := mat.NewDense(2, 2, []float64{1.0, 1.0, 0.0, 1.0})
C := mat.NewDense(1, 2, []float64{1.0, 0.0})
// dot is the model of the system we will simulate
dot, err := sim.NewBaseModel(A, nil, C, nil)
if err != nil {
log.Fatalf("Failed to created dot model: %v", err)
}
// measurement noise used to simulate real system
measCov := mat.NewSymDense(1, []float64{1e-1})
measNoise, err := noise.NewGaussian([]float64{0.0}, measCov)
if err != nil {
log.Fatalf("Failed to create measurement noise: %v", err)
}
// initial state covariance
stateCov := mat.NewSymDense(2, []float64{1e-5, 0, 0, 1e-5})
stateNoise, err := noise.NewGaussian([]float64{0.0, 0.0}, stateCov)
if err != nil {
log.Fatalf("Failed to create state noise: %v", err)
}
// initial system state: we simply generate some random numbers
x1, x2 := rand.NormFloat64()*0.1, rand.NormFloat64()*0.1
var x mat.Vector = mat.NewVecDense(2, []float64{x1, x2})
fmt.Println("Initial Model State: \n", matrix.Format(x))
// initial condition of KF
initCond := sim.NewInitCond(x, stateCov)
// z stores real system measurement: y+noise
z := new(mat.VecDense)
// filter initial estimate
initX := &mat.VecDense{}
initX.CloneFromVec(x)
initX.AddVec(initX, stateNoise.Sample())
fmt.Println("Initial KF State: \n", matrix.Format(initX))
var est filter.Estimate
est, err = estimate.NewBase(initX)
if err != nil {
log.Fatalf("Failed to create initial estimate: %v", err)
}
// create Kalman Filter
f, err := kf.New(dot, initCond, stateNoise, measNoise)
//f, err := kf.New(dot, initCond, nil, measNoise)
if err != nil {
log.Fatalf("Failed to create KF filter: %v", err)
}
fmt.Println("=============================================")
// GoCV simulation environment
img := gocv.NewMatWithSize(500, 500, gocv.MatTypeCV8UC3)
center := image.Pt(img.Cols()/2, img.Rows()/2)
r := float64(img.Cols()) / 3.0
// create simple window to show the simulation
window := gocv.NewWindow("Kalman Filter")
for {
// ground truth propagation
x, err = dot.Propagate(x, nil, nil)
if err != nil {
log.Fatalf("Model Propagation error: %v", err)
}
fmt.Printf("TRUTH State:\n%v\n", matrix.Format(x))
// ground truth observation
y, err := dot.Observe(x, nil, nil)
if err != nil {
log.Fatalf("Model Observation error: %v", err)
}
fmt.Printf("TRUTH Output:\n%v\n", matrix.Format(y))
yPt := GetDotPos(center, r, y.At(0, 0))
fmt.Printf("Model Out Point: %v\n", yPt)
// measurement: z = y+noise
noise := measNoise.Sample()
fmt.Println("NOISE:", matrix.Format(noise))
z.AddVec(y, noise)
fmt.Printf("Measurement:\n%v\n", matrix.Format(z))
measPt := GetDotPos(center, r, z.At(0, 0))
fmt.Printf("Meas Point: %v\n", measPt)
// propagate particle filters to the next step
pred, err := f.Predict(est.Val(), nil)
if err != nil {
log.Fatalf("Filter Prediction error: %v", err)
}
// correct state estimate using measurement z
est, err = f.Update(pred.Val(), nil, z)
if err != nil {
log.Fatalf("Filter Udpate error: %v", err)
}
fmt.Printf("CORRECTED State:\n%v\n", matrix.Format(est.Val()))
corrPt := GetDotPos(center, r, est.Val().At(0, 0))
fmt.Printf("Corr Point: %v\n", corrPt)
fmt.Println("---------------------------------------------")
// reset all pixels to 0
img.SetTo(gocv.Scalar{Val1: 0, Val2: 0, Val3: 0, Val4: 0})
// draw markers
DrawMarker(&img, yPt, color.RGBA{0, 255, 0, 0}, 3)
DrawMarker(&img, measPt, color.RGBA{255, 0, 0, 0}, 3)
DrawMarker(&img, corrPt, color.RGBA{100, 149, 237, 0}, 3)
window.IMShow(img)
if window.WaitKey(int(500)) == 27 {
fmt.Printf("Shutting down: ESC pressed\n")
break
}
}
}