-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfisheye.py
134 lines (107 loc) · 4.21 KB
/
fisheye.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
import cv2
import numpy as np
from numpy import arange, arctan, hypot, meshgrid, pi, sin, sqrt, tan
from .base import FisheyeBase
class FisheyeNode(FisheyeBase):
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"image": ("IMAGE",),
"mapping": (
["equidistant", "equisolid", "orthographic", "stereographic"],
),
"format": (["fullframe", "circular"],),
"fov": (
"FLOAT",
{"default": 180.0, "min": 0.0, "max": 360.0, "step": 10.0},
),
"pfov": (
"FLOAT",
{"default": 120.0, "min": 0.0, "max": 360.0, "step": 10.0},
),
"entire_image": ("BOOLEAN", {"default": False}),
"wcenter": (
"FLOAT",
{"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.1},
),
"hcenter": (
"FLOAT",
{"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.1},
),
},
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "apply_fisheye"
CATEGORY = "image/processing"
def calculate_zoom_factor(self, fov_in, fov_out, mapping):
fov_in_rad = fov_in * pi / 180.0
fov_out_rad = fov_out * pi / 180.0
max_in = tan(fov_in_rad / 2.0)
if mapping == "equidistant":
max_out = fov_out_rad / 2.0
elif mapping == "equisolid":
max_out = 2 * sin(fov_out_rad / 4.0)
elif mapping == "orthographic":
max_out = sin(fov_out_rad / 2.0)
elif mapping == "stereographic":
max_out = 2 * tan(fov_out_rad / 4.0)
# Return zoom factor needed to make output radius contain input
return max_out / max_in if max_in > 0 else 1.0
def get_focal_length(self, dim, fov_degrees):
fov_rad = fov_degrees * pi / 180.0
half_fov = fov_rad / 2.0
if self.mapping == "equidistant":
return dim / fov_rad
elif self.mapping == "equisolid":
return dim / (4 * sin(half_fov / 2))
elif self.mapping == "orthographic":
return dim / (2 * sin(half_fov))
elif self.mapping == "stereographic":
return dim / (4 * tan(half_fov / 2))
return dim / 2
def map_fisheye(self, i, j, width, height, dim, xcenter, ycenter):
xd = i - xcenter
yd = j - ycenter
rd = hypot(xd, yd)
f_out = self.get_focal_length(dim, self.fov)
f_in = self.get_focal_length(dim, self.pfov)
if self.entire_image:
zoom = self.calculate_zoom_factor(self.pfov, self.fov, self.mapping)
f_in *= zoom
theta = arctan(rd / f_in)
if self.mapping == "equidistant":
r = f_out * theta
elif self.mapping == "equisolid":
r = 2 * f_out * sin(theta / 2)
elif self.mapping == "orthographic":
r = f_out * sin(theta)
elif self.mapping == "stereographic":
r = 2 * f_out * tan(theta / 2)
rdmask = rd != 0
xs = xd.astype(np.float32).copy()
ys = yd.astype(np.float32).copy()
xs[rdmask] = (rd[rdmask] / r[rdmask]) * xd[rdmask] + xcenter
ys[rdmask] = (rd[rdmask] / r[rdmask]) * yd[rdmask] + ycenter
xs[~rdmask] = xcenter
ys[~rdmask] = ycenter
return xs, ys
def apply_fisheye(
self, image, mapping, format, fov, pfov, entire_image, wcenter, hcenter
):
self.setup_parameters(fov, pfov, mapping, format)
self.entire_image = entire_image
image_np = self.process_image_tensor(image)
height, width = image_np.shape[:2]
xcenter = width * wcenter
ycenter = height * hcenter
if format == "circular":
dim = min(width, height)
else: # fullframe
dim = sqrt(width**2 + height**2)
i = arange(width)
j = arange(height)
i, j = meshgrid(i, j)
xs, ys = self.map_fisheye(i, j, width, height, dim, xcenter, ycenter)
remapped = cv2.remap(image_np, xs, ys, cv2.INTER_LINEAR)
return (self.tensor_to_image(remapped),)