This repository was archived by the owner on Jul 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcv3d.py
More file actions
68 lines (51 loc) · 2.03 KB
/
cv3d.py
File metadata and controls
68 lines (51 loc) · 2.03 KB
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
"""
Python wrapping around cffi layer to interop to OpenCV 3D functions.
"""
from typing import Sequence, Tuple
from sb_vision.native import _cv3d # type: ignore
from .coordinates import Cartesian, Orientation, PixelCoordinate
class Cv3dError(RuntimeError):
"""A 3D related OpenCV error."""
pass
def _ffi_flattened_float_array(values: Sequence[Sequence[float]]):
if not values:
raise ValueError("Refusing to create ffi matrix for empty 'values'.")
lengths = list({len(x) for x in values})
if len(lengths) != 1:
raise ValueError(
"'values' is not rectangular (got row lengths of {} and {})".format(
", ".join(str(x) for x in lengths[:-1]),
lengths[-1],
),
)
flattened_values = sum([tuple(x) for x in values], ())
count = len(values) * lengths[0]
return _cv3d.ffi.new('double[{}]'.format(count), flattened_values)
def solve_pnp(
object_points: Sequence[Sequence[float]],
pixel_corners: Sequence[PixelCoordinate],
camera_matrix: Sequence[Sequence[float]],
distance_coefficients: Sequence[Sequence[float]],
) -> Tuple[Cartesian, Orientation]:
"""
Wrapper around OpenCV solvePnP.
See the OpenCV docs for details.
"""
# https://docs.opencv.org/2.4/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#solvepnp
# https://docs.opencv.org/master/d9/d0c/group__calib3d.html#ga549c2075fac14829ff4a58bc931c033d
orientation_vector = _cv3d.ffi.new('double[3]')
translation_vector = _cv3d.ffi.new('double[3]')
return_value = _cv3d.lib.solve_pnp(
_ffi_flattened_float_array(object_points),
_ffi_flattened_float_array(pixel_corners),
_ffi_flattened_float_array(camera_matrix),
_ffi_flattened_float_array(distance_coefficients),
orientation_vector,
translation_vector,
)
if not return_value:
raise Cv3dError("OpenCV solvePnP failed")
return (
Cartesian(*translation_vector),
Orientation(*orientation_vector),
)