-
Notifications
You must be signed in to change notification settings - Fork 1
/
defisheye.py
225 lines (183 loc) · 7.23 KB
/
defisheye.py
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
223
224
225
#!/usr/bin/env python3.6
# -*- Coding: UTF-8 -*-
"""
Defisheye algorithm.
Developed by: E. S. Pereira.
e-mail: [email protected]
Based in the work of F. Weinhaus.
http://www.fmwconcepts.com/imagemagick/defisheye/index.php
Copyright [2019] [E. S. Pereira]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import cv2
from numpy import arange, sqrt, arctan, sin, tan, meshgrid, pi
from numpy import ndarray, hypot
import torch
from typing import Union
class Defisheye:
"""
Defisheye
fov: fisheye field of view (aperture) in degrees
pfov: perspective field of view (aperture) in degrees
xcenter: x center of fisheye area
ycenter: y center of fisheye area
radius: radius of fisheye area
angle: image rotation in degrees clockwise
dtype: linear, equalarea, orthographic, stereographic
format: circular, fullframe
"""
def __init__(self, infile, **kwargs):
vkwargs = {"fov": 180,
"pfov": 120,
"xcenter": None,
"ycenter": None,
"radius": None,
"angle": 0,
"dtype": "equalarea",
"format": "fullframe"
}
self._start_att(vkwargs, kwargs)
if type(infile) == str:
_image = cv2.imread(infile)
elif type(infile) == ndarray:
_image = infile
else:
raise Exception("Image format not recognized")
width = _image.shape[1]
height = _image.shape[0]
xcenter = width // 2
ycenter = height // 2
dim = min(width, height)
self.x0 = xcenter - dim // 2
self.xf = xcenter + dim // 2
self.y0 = ycenter - dim // 2
self.yf = ycenter + dim // 2
self._image = _image[self.y0:self.yf, self.x0:self.xf, :]
self._width = self._image.shape[1]
self._height = self._image.shape[0]
if self._xcenter is None:
self._xcenter = (self._width - 1) // 2
if self._ycenter is None:
self._ycenter = (self._height - 1) // 2
def _map(self, i, j, ofocinv, dim):
xd = i - self._xcenter
yd = j - self._ycenter
rd = hypot(xd, yd)
phiang = arctan(ofocinv * rd)
if self._dtype == "linear":
ifoc = dim * 180 / (self._fov * pi)
rr = ifoc * phiang
# rr = "rr={}*phiang;".format(ifoc)
elif self._dtype == "equalarea":
ifoc = dim / (2.0 * sin(self._fov * pi / 720))
rr = ifoc * sin(phiang / 2)
# rr = "rr={}*sin(phiang/2);".format(ifoc)
elif self._dtype == "orthographic":
ifoc = dim / (2.0 * sin(self._fov * pi / 360))
rr = ifoc * sin(phiang)
# rr="rr={}*sin(phiang);".format(ifoc)
elif self._dtype == "stereographic":
ifoc = dim / (2.0 * tan(self._fov * pi / 720))
rr = ifoc * tan(phiang / 2)
rdmask = rd != 0
xs = xd.copy()
ys = yd.copy()
xs[rdmask] = (rr[rdmask] / rd[rdmask]) * xd[rdmask] + self._xcenter
ys[rdmask] = (rr[rdmask] / rd[rdmask]) * yd[rdmask] + self._ycenter
xs[~rdmask] = 0
ys[~rdmask] = 0
xs = xs.astype(int)
ys = ys.astype(int)
return xs, ys
def calculate_conversions(self):
"""
Added functionality to allow for a single calculated mapping to be applied to a series of images
from the same fisheye camera.
"""
if self._format == "circular":
dim = min(self._width, self._height)
elif self._format == "fullframe":
dim = sqrt(self._width ** 2.0 + self._height ** 2.0)
if self._radius is not None:
dim = 2 * self._radius
# compute output (perspective) focal length and its inverse from ofov
# phi=fov/2; r=N/2
# r/f=tan(phi);
# f=r/tan(phi);
# f= (N/2)/tan((fov/2)*(pi/180)) = N/(2*tan(fov*pi/360))
ofoc = dim / (2 * tan(self._pfov * pi / 360))
ofocinv = 1.0 / ofoc
i = arange(self._width)
j = arange(self._height)
self.i, self.j = meshgrid(i, j)
self.xs, self.ys, = self._map(self.i, self.j, ofocinv, dim)
return self.xs, self.ys, self.i, self.j
def unwarp(self, image: Union[ndarray, torch.Tensor]):
"""
Added functionality to allow for a single calculated mapping to be applied to a series of images
from the same fisheye camera.
"""
# if image is a single image, add a dimension to make it a batch of 1
image_rank = len(image.shape)
if image_rank == 3:
image = image[None, ...]
if isinstance(image, torch.Tensor):
with torch.no_grad():
image = image[:, self.y0:self.yf, self.x0:self.xf, :]
img = image.clone()
img[:, self.i, self.j, :] = image[:, self.xs, self.ys, :]
elif isinstance(image, ndarray):
image = image[:, self.y0:self.yf, self.x0:self.xf, :]
img = image.copy()
img[:, self.i, self.j, :] = image[:, self.xs, self.ys, :]
else:
raise Exception("Image format not recognized. Please provide a numpy array or a torch tensor.")
if image_rank == 3: # if image was a single image, remove the batch dimension
img = img[0]
return img
def convert(self, outfile=None):
if self._format == "circular":
dim = min(self._width, self._height)
elif self._format == "fullframe":
dim = sqrt(self._width ** 2.0 + self._height ** 2.0)
if self._radius is not None:
dim = 2 * self._radius
# compute output (perspective) focal length and its inverse from ofov
# phi=fov/2; r=N/2
# r/f=tan(phi);
# f=r/tan(phi);
# f= (N/2)/tan((fov/2)*(pi/180)) = N/(2*tan(fov*pi/360))
ofoc = dim / (2 * tan(self._pfov * pi / 360))
ofocinv = 1.0 / ofoc
i = arange(self._width)
j = arange(self._height)
i, j = meshgrid(i, j)
xs, ys, = self._map(i, j, ofocinv, dim)
img = self._image.copy()
img[i, j, :] = self._image[xs, ys, :]
if outfile is not None:
cv2.imwrite(outfile, img)
return img
def _start_att(self, vkwargs, kwargs):
"""
Starting atributes
"""
pin = []
for key, value in kwargs.items():
if key not in vkwargs:
raise NameError("Invalid key {}".format(key))
else:
pin.append(key)
setattr(self, "_{}".format(key), value)
pin = set(pin)
rkeys = set(vkwargs.keys()) - pin
for key in rkeys:
setattr(self, "_{}".format(key), vkwargs[key])