-
Notifications
You must be signed in to change notification settings - Fork 0
/
fisheyedewarp.go
208 lines (169 loc) · 5.34 KB
/
fisheyedewarp.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
package fisheyedewarp
import (
"fmt"
"image"
"image/color"
"math"
"unsafe"
"gocv.io/x/gocv"
)
// ProjectionType defines the type of projection for the dewarping operation.
type ProjectionType string
const (
Linear ProjectionType = "Linear"
EqualArea ProjectionType = "EqualArea"
Orthographic ProjectionType = "Orthographic"
Stereographic ProjectionType = "Stereographic"
)
// Dewarp transforms a fisheye image into a perspective image based on the provided field of view (FOV) values and projection type.
//
// - fov (float64): The field of view of the input fisheye image in degrees. A value of 180 represents a full hemispherical fisheye image.
// The valid range is 0 < fov <= 180. Default value is 180 for a full hemisphere.
// - pfov (float64): The output perspective field of view in degrees. Values must be 0 < pfov < 180.
// The default value is 120 degrees both vertically and horizontally for a circular fisheye image and diagonally for a full frame fisheye.
// The pFOV relative to the input fov determines the proportional area of the fisheye image that will be transformed.
func Dewarp(img gocv.Mat, fov float64, pfov float64, projectionType ProjectionType) (*gocv.Mat, error) {
if fov <= 0 || fov > 180 {
return nil, fmt.Errorf("invalid FOV: %f, must be in the range 0 < fov <= 180", fov)
}
if pfov <= 0 || pfov >= 180 {
return nil, fmt.Errorf("invalid PFOV: %f, must be in the range 0 < pfov < 180", pfov)
}
rawWidth := img.Cols()
rawHeight := img.Rows()
rawXCenter := rawWidth / 2
rawYCenter := rawHeight / 2
dim := int(math.Min(float64(rawWidth), float64(rawHeight)))
x0 := rawXCenter - dim/2
xf := rawXCenter + dim/2
y0 := rawYCenter - dim/2
yf := rawYCenter + dim/2
roi := img.Region(image.Rect(x0, y0, xf, yf))
defer roi.Close()
width := roi.Cols()
height := roi.Rows()
dimF := math.Sqrt(math.Pow(float64(width), 2) + math.Pow(float64(height), 2))
ofoc := dimF / (2.0 * math.Tan(pfov*math.Pi/360.0))
ofocinv := 1.0 / ofoc
i := arange(width)
j := arange(height)
iGrid, jGrid := meshgrid(i, j)
xCenter := float64(width-1) / 2.0
yCenter := float64(height-1) / 2.0
xs, ys := remap(iGrid, jGrid, ofocinv, dimF, fov, xCenter, yCenter, projectionType)
xsMat, err := convertToMat(xs)
if err != nil {
return nil, err
}
defer xsMat.Close()
ysMat, err := convertToMat(ys)
if err != nil {
return nil, err
}
defer ysMat.Close()
remapped := gocv.NewMat()
gocv.Remap(roi, &remapped, &xsMat, &ysMat, gocv.InterpolationLinear, gocv.BorderConstant, color.RGBA{})
return &remapped, nil
}
func arange(n int) []float64 {
arr := make([]float64, n)
for k := 0; k < n; k++ {
arr[k] = float64(k)
}
return arr
}
func meshgrid(x, y []float64) ([][]float64, [][]float64) {
m := len(x)
n := len(y)
xGrid := make([][]float64, n)
yGrid := make([][]float64, n)
for i := 0; i < n; i++ {
xGrid[i] = make([]float64, m)
yGrid[i] = make([]float64, m)
for j := 0; j < m; j++ {
xGrid[i][j] = x[j]
yGrid[i][j] = y[i]
}
}
return xGrid, yGrid
}
func remap(i, j [][]float64, ofocinv, dim, fov, xcenter, ycenter float64, projectionType ProjectionType) ([][]float32, [][]float32) {
rows := len(i)
cols := len(i[0])
xd := make([][]float64, rows)
yd := make([][]float64, rows)
rd := make([][]float64, rows)
phiang := make([][]float64, rows)
rr := make([][]float64, rows)
for row := 0; row < rows; row++ {
xd[row] = make([]float64, cols)
yd[row] = make([]float64, cols)
rd[row] = make([]float64, cols)
phiang[row] = make([]float64, cols)
rr[row] = make([]float64, cols)
for col := 0; col < cols; col++ {
xdVal := i[row][col] - xcenter
ydVal := j[row][col] - ycenter
xd[row][col] = xdVal
yd[row][col] = ydVal
rdVal := math.Hypot(xdVal, ydVal)
rd[row][col] = rdVal
phiang[row][col] = math.Atan(ofocinv * rdVal)
}
}
var ifoc float64
switch projectionType {
case Linear:
ifoc = dim * 180.0 / (fov * math.Pi)
case EqualArea:
ifoc = dim / (2.0 * math.Sin(fov*math.Pi/720.0))
case Orthographic:
ifoc = dim / (2.0 * math.Sin(fov*math.Pi/360.0))
case Stereographic:
ifoc = dim / (2.0 * math.Tan(fov*math.Pi/720.0))
}
for row := 0; row < rows; row++ {
for col := 0; col < cols; col++ {
var rrVal float64
switch projectionType {
case Linear:
rrVal = ifoc * phiang[row][col]
case EqualArea:
rrVal = ifoc * math.Sin(phiang[row][col]/2.0)
case Orthographic:
rrVal = ifoc * math.Sin(phiang[row][col])
case Stereographic:
rrVal = ifoc * math.Tan(phiang[row][col]/2.0)
}
rr[row][col] = rrVal
}
}
xs := make([][]float32, rows)
ys := make([][]float32, rows)
for row := 0; row < rows; row++ {
xs[row] = make([]float32, cols)
ys[row] = make([]float32, cols)
for col := 0; col < cols; col++ {
if rd[row][col] != 0 {
scale := rr[row][col] / rd[row][col]
xs[row][col] = float32(scale*xd[row][col] + xcenter)
ys[row][col] = float32(scale*yd[row][col] + ycenter)
} else {
xs[row][col] = 0
ys[row][col] = 0
}
}
}
return xs, ys
}
func convertToMat(data [][]float32) (gocv.Mat, error) {
rows := len(data)
cols := len(data[0])
totalElements := rows * cols
flatData := make([]float32, 0, totalElements)
for _, row := range data {
flatData = append(flatData, row...)
}
byteSlice := unsafe.Slice((*byte)(unsafe.Pointer(&flatData[0])), totalElements*4)
return gocv.NewMatFromBytes(rows, cols, gocv.MatTypeCV32F, byteSlice)
}