-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
etf.go
222 lines (180 loc) · 6.36 KB
/
etf.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package colidr
import (
"image"
"math"
"sync"
"gocv.io/x/gocv"
)
// Etf is the main entry struct for the edge tangent flow computation.
// It encompass the basic operational entities needed for the matrix operations.
type Etf struct {
flowField gocv.Mat
gradientField gocv.Mat
refinedEtf gocv.Mat
gradientMag gocv.Mat
wg sync.WaitGroup
mu sync.RWMutex
}
// point is a basic struct for vector type operations
type point struct {
x int
y int
}
// NewETF is a constructor method which initializes an Etf struct.
func NewETF() *Etf {
return &Etf{}
}
// Init initializes the ETF matrices.
func (etf *Etf) Init(rows, cols int) {
etf.flowField = gocv.NewMatWithSize(rows, cols, gocv.MatTypeCV32F+gocv.MatChannels3)
etf.gradientField = gocv.NewMatWithSize(rows, cols, gocv.MatTypeCV32F+gocv.MatChannels3)
etf.refinedEtf = gocv.NewMatWithSize(rows, cols, gocv.MatTypeCV32F+gocv.MatChannels3)
etf.gradientMag = gocv.NewMatWithSize(rows, cols, gocv.MatTypeCV32F+gocv.MatChannels3)
}
// InitDefaultEtf computes the gradientField matrix by setting up
// the pixel values from original image on which a sobel threshold has been applied.
func (etf *Etf) InitDefaultEtf(file string, size image.Point) error {
etf.resizeMat(size)
src := gocv.IMRead(file, gocv.IMReadColor)
src.ConvertTo(&src, gocv.MatTypeCV32F, 255)
gocv.Normalize(src, &src, 0.0, 1.0, gocv.NormMinMax)
// Generate gradX and gradY
gradX := gocv.NewMatWithSize(src.Rows(), src.Cols(), gocv.MatTypeCV32F)
gradY := gocv.NewMatWithSize(src.Rows(), src.Cols(), gocv.MatTypeCV32F)
gocv.Sobel(src, &gradX, gocv.MatTypeCV32F, 1, 0, 5, 1, 0, gocv.BorderDefault)
gocv.Sobel(src, &gradY, gocv.MatTypeCV32F, 0, 1, 5, 1, 0, gocv.BorderDefault)
// Compute gradient
gocv.Magnitude(gradX, gradY, &etf.gradientMag)
gocv.Normalize(etf.gradientMag, &etf.gradientMag, 0.0, 1.0, gocv.NormMinMax)
width, height := src.Cols(), src.Rows()
etf.wg.Add(width * height)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
go func(y, x int) {
etf.mu.RLock()
defer etf.mu.RUnlock()
u := gradX.GetVecfAt(y, x)
v := gradY.GetVecfAt(y, x)
etf.gradientField.SetVecfAt(y, x, gocv.Vecf{v[0], u[0], 0})
etf.wg.Done()
}(y, x)
}
}
etf.wg.Wait()
etf.rotateFlow(&etf.gradientField, &etf.flowField, 90)
return nil
}
// RefineEtf will compute the refined edge tangent flow
// based on the formulas from the original paper.
func (etf *Etf) RefineEtf(kernel int) {
width, height := etf.flowField.Cols(), etf.flowField.Rows()
etf.wg.Add(width * height)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
// Spawn computation into separate goroutines
go func(y, x int) {
etf.mu.Lock()
etf.computeNewVector(x, y, kernel)
etf.mu.Unlock()
etf.wg.Done()
}(y, x)
}
}
etf.wg.Wait()
etf.flowField = etf.refinedEtf.Clone()
}
// resizeMat resize all the matrices
func (etf *Etf) resizeMat(size image.Point) {
gocv.Resize(etf.gradientField, &etf.gradientField, size, 0, 0, gocv.InterpolationLinear)
gocv.Resize(etf.flowField, &etf.flowField, size, 0, 0, gocv.InterpolationLinear)
gocv.Resize(etf.refinedEtf, &etf.refinedEtf, size, 0, 0, gocv.InterpolationLinear)
gocv.Resize(etf.gradientMag, &etf.gradientMag, size, 0, 0, gocv.InterpolationLinear)
}
// rotateFlow applies a rotation on the original gradient field and calculates the new angles.
func (etf *Etf) rotateFlow(src, dst *gocv.Mat, theta float64) {
theta = theta / 180.0 * math.Pi
width, height := src.Cols(), src.Rows()
etf.wg.Add(width * height)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
go func(y, x int) {
etf.mu.Lock()
defer etf.mu.Unlock()
v := src.GetVecfAt(y, x)
// Obtain the vector value and rotate it.
rx := float64(v[0])*math.Cos(theta) - float64(v[1])*math.Sin(theta)
ry := float64(v[0])*math.Sin(theta) + float64(v[1])*math.Cos(theta)
dst.SetVecfAt(y, x, gocv.Vecf{float32(rx), float32(ry), 0})
etf.wg.Done()
}(y, x)
}
}
etf.wg.Wait()
}
// computeNewVector computes a new, normalized vector from the refined edge tangent flow matrix following the original paper Eq(1).
func (etf *Etf) computeNewVector(x, y int, kernel int) {
var tNew0, tNew1, tNew2 float32
tCurX := etf.flowField.GetVecfAt(y, x)
for r := y - kernel; r <= y+kernel; r++ {
for c := x - kernel; c <= x+kernel; c++ {
// Checking for boundaries.
if r < 0 || r >= etf.refinedEtf.Rows() || c < 0 || c >= etf.refinedEtf.Cols() {
continue
}
tCurY := etf.flowField.GetVecfAt(r, c)
phi := etf.computePhi(tCurX, tCurY)
// Compute the euclidean distance of the current point and the neighborhood point.
ws := etf.computeWeightSpatial(point{x, y}, point{c, r}, kernel)
wm := etf.computeWeightMagnitude(etf.gradientMag.GetFloatAt(y, x), etf.gradientMag.GetFloatAt(r, c))
wd := etf.computeWeightDirection(tCurX, tCurY)
tNew0 += phi * tCurY[0] * ws * wm * wd
tNew1 += phi * tCurY[1] * ws * wm * wd
tNew2 += phi * tCurY[2] * ws * wm * wd
}
}
etf.refinedEtf.SetVecfAt(y, x, etf.normalize(tNew0, tNew1, tNew2))
}
// computeWeightSpatial implementation of Paper's Eq(2)
func (etf *Etf) computeWeightSpatial(p1, p2 point, r int) float32 {
// Get the euclidean distance of two points.
dx := p2.x - p1.x
dy := p2.y - p1.y
dist := math.Sqrt(float64(dx*dx) + float64(dy*dy))
if dist < float64(r) {
return 1.0
}
return 0.0
}
// computeWeightMagnitude implementation of Paper's Eq(3)
func (etf *Etf) computeWeightMagnitude(gradMagX, gradMagY float32) float32 {
return (1.0 + float32(math.Tanh(float64(gradMagX-gradMagY)))) / 2.0
}
// computeWeightDirection implementation of Paper's Eq(4)
func (etf *Etf) computeWeightDirection(x, y gocv.Vecf) float32 {
return float32(math.Abs(float64(etf.computeDot(x, y))))
}
// computePhi implementation of Paper's Eq(5)
func (etf *Etf) computePhi(x, y gocv.Vecf) float32 {
dot := etf.computeDot(x, y)
if dot > 0 {
return 1.0
}
return -1.0
}
// computeDot computes the dot product of two vectors
func (etf *Etf) computeDot(x, y gocv.Vecf) float32 {
var s float32
ch := etf.flowField.Channels()
for i := 0; i < ch; i++ {
s += x[i] * y[i]
}
return s
}
// normalize returns a normalized vector
func (etf *Etf) normalize(x, y, z float32) gocv.Vecf {
nv := float32(math.Sqrt(float64(x*x) + float64(y*y) + float64(z*z)))
if nv > 0.0 {
return gocv.Vecf{x * 1.0 / nv, y * 1.0 / nv, z * 1.0 / nv}
}
return gocv.Vecf{0.0, 0.0, 0.0}
}