-
Notifications
You must be signed in to change notification settings - Fork 8
/
ransac.go
206 lines (176 loc) · 4.21 KB
/
ransac.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
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
// Package ransac contains an implementation of the RANSAC algorithm.
package ransac
import (
"errors"
"image/color"
"math"
"math/rand"
"github.com/rs/zerolog/log"
"go-hep.org/x/hep/fit"
"go-hep.org/x/hep/hplot"
"gonum.org/v1/gonum/optimize"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
)
func sample(rnd *rand.Rand, x, y []float64, n int) ([]float64, []float64) {
if n < 1 {
panic("n must be > 0")
}
if len(x) != len(y) {
panic("x and y must have same length")
}
isel := map[int]struct{}{}
xret, yret := make([]float64, n), make([]float64, n)
for {
// Random index.
i := int(rnd.Intn(len(x)))
// Make sure is new.
if _, ok := isel[i]; ok {
continue
}
isel[i] = struct{}{}
n--
xret[n] = x[i]
yret[n] = y[i]
if n == 0 {
return xret, yret
}
}
}
// ModelFn represents the model to be fitted during RANSAC.
// It has to be implemented by the user.
// Example (poly2):
//
// func(x float64, params []float64) float64 {
// return params[0] + params[1]*x + params[2]*x*x
// }
type ModelFn func(x float64, params []float64) float64
// MetaParams contains the meta-parameters for a RANSAC search.
type MetaParams struct {
MinModelPoints int
MaxIter int
MinInliers int
InlierThreshold float64
Seed int64
}
// Check validates RANSAC params and will panic if there are invalid settings.
func (p *MetaParams) Check(nx int) {
if p.MinModelPoints == 0 {
panic("MinModelPoints cannot be 0")
}
if p.MinModelPoints > nx*3/4 {
panic("MinModelPoints should be <= len(x)*2/3")
}
if p.MaxIter == 0 {
panic("MaxIter must be > 0")
}
if p.MinInliers < p.MinModelPoints {
panic("MinInliers must be at least MinModelPoints")
}
}
// Ransac runs the RANSAC algorithm, trying to find model parameters for ModelFn
// according to the meta parameters.
func Ransac(x, y []float64, model ModelFn, nParams int, p MetaParams) (*optimize.Location, error) {
if len(x) != len(y) {
panic("x and y must have same length")
}
if nParams < 1 {
panic("the model must have at least one param")
}
p.Check(len(x))
// Plot(
// "~/Desktop/data.png",
// x, y, nil, nil, "x", "y = f(x)")
src := rand.NewSource(p.Seed)
// #nosec G404
rnd := rand.New(src)
bestFit := optimize.Location{
F: math.MaxFloat64,
}
for i := 0; i < p.MaxIter; i++ {
// Sample.
xS, yS := sample(rnd, x, y, p.MinModelPoints)
// Fit.
hypoParams, err := fit.Curve1D(
fit.Func1D{
F: model,
N: nParams,
X: xS,
Y: yS,
},
nil, nil,
)
if err != nil {
log.Err(err).Msg("fit did not converge (sample)")
continue
}
// Plot(
// fmt.Sprintf("~/Desktop/fit_sample_%03d_%f.png", i, hypoParams.F),
// xS, yS, hypoParams.X, model, "x", "y = f(x)")
// Select inliers.
xIn, yIn := []float64{}, []float64{}
for j := range x {
yModel := model(x[j], hypoParams.X)
if math.Abs(yModel-y[j]) < p.InlierThreshold {
xIn = append(xIn, x[j])
yIn = append(yIn, y[j])
}
}
if len(xIn) < p.MinInliers {
continue
}
// Fit inliers.
hypoParams, err = fit.Curve1D(
fit.Func1D{
F: model,
N: nParams,
X: xIn,
Y: yIn,
},
nil, nil,
)
if err != nil {
log.Err(err).Msg("fit did not converge (inliers)")
continue
}
// Plot(
// fmt.Sprintf("~/Desktop/fit_inliers_%03d_%f.png", i, hypoParams.F),
// xIn, yIn, hypoParams.X, model, "x", "y = f(x)")
if hypoParams.F < bestFit.F {
bestFit = hypoParams.Location
}
}
if bestFit.F == math.MaxFloat64 {
return nil, errors.New("RANSAC unsuccessful")
}
// Plot(
// "~/Desktop/endresult.png",
// x, y, bestFit.X, model, "x", "y = f(x)")
return &bestFit, nil
}
// Plot is a helper to plot the results of a RANSAC iteration.
func Plot(path string, x, y []float64, ps []float64, fn ModelFn, labelX, labelY string) {
p := hplot.New()
p.X.Label.Text = labelX
p.X.Min = -0
p.X.Max = +10
p.Y.Label.Text = labelY
p.Y.Min = 0
p.Y.Max = 1200
s := hplot.NewS2D(hplot.ZipXY(x, y))
s.Color = color.RGBA{0, 0, 255, 255}
p.Add(s)
if fn != nil {
f := plotter.NewFunction(func(x float64) float64 {
return fn(x, ps)
})
f.Color = color.RGBA{255, 0, 0, 255}
f.Samples = 1000
p.Add(f)
}
p.Add(plotter.NewGrid())
err := p.Save(20*vg.Centimeter, -1, path)
if err != nil {
panic(err)
}
}