Skip to content

Commit c5bc2fd

Browse files
committed
updating compatability
1 parent 76d9d27 commit c5bc2fd

4 files changed

Lines changed: 317 additions & 42 deletions

File tree

src/openptv_python/_native_convert.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ def _optv_parameters_module() -> ModuleType:
3232

3333

3434
def to_native_calibration(cal: Calibration):
35+
if not isinstance(cal, Calibration):
36+
return cal
37+
3538
if not HAS_NATIVE_CALIBRATION or optv_calibration is None:
3639
raise RuntimeError("optv Calibration is not available")
3740

@@ -49,6 +52,9 @@ def to_native_calibration(cal: Calibration):
4952

5053

5154
def to_native_control_par(cpar: ControlPar):
55+
if not isinstance(cpar, ControlPar):
56+
return cpar
57+
5258
parameters_module = _optv_parameters_module()
5359

5460
flags = [
@@ -83,6 +89,9 @@ def to_native_control_par(cpar: ControlPar):
8389

8490

8591
def to_native_target_par(tpar: TargetPar):
92+
if not isinstance(tpar, TargetPar):
93+
return tpar
94+
8695
parameters_module = _optv_parameters_module()
8796

8897
thresholds = list(tpar.gvthresh)

src/openptv_python/segmentation.py

Lines changed: 80 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,48 @@ class Peak:
3535
n_touch: int = 0
3636

3737

38+
def _control_image_size(cpar: ControlPar) -> tuple[int, int]:
39+
if hasattr(cpar, "get_image_size"):
40+
return cpar.get_image_size()
41+
return cpar.imx, cpar.imy
42+
43+
44+
def _target_thresholds(targ_par: TargetPar) -> list[int]:
45+
if hasattr(targ_par, "get_grey_thresholds"):
46+
return list(targ_par.get_grey_thresholds())
47+
return list(targ_par.gvthresh)
48+
49+
50+
def _target_discont(targ_par: TargetPar) -> int:
51+
if hasattr(targ_par, "get_max_discontinuity"):
52+
return targ_par.get_max_discontinuity()
53+
return targ_par.discont
54+
55+
56+
def _target_nn_bounds(targ_par: TargetPar) -> tuple[int, int]:
57+
if hasattr(targ_par, "get_pixel_count_bounds"):
58+
return targ_par.get_pixel_count_bounds()
59+
return targ_par.nnmin, targ_par.nnmax
60+
61+
62+
def _target_nx_bounds(targ_par: TargetPar) -> tuple[int, int]:
63+
if hasattr(targ_par, "get_xsize_bounds"):
64+
return targ_par.get_xsize_bounds()
65+
return targ_par.nxmin, targ_par.nxmax
66+
67+
68+
def _target_ny_bounds(targ_par: TargetPar) -> tuple[int, int]:
69+
if hasattr(targ_par, "get_ysize_bounds"):
70+
return targ_par.get_ysize_bounds()
71+
return targ_par.nymin, targ_par.nymax
72+
73+
74+
def _target_min_sum_grey(targ_par: TargetPar) -> int:
75+
if hasattr(targ_par, "get_min_sum_grey"):
76+
return targ_par.get_min_sum_grey()
77+
return targ_par.sumg_min
78+
79+
3880
def targ_rec(
3981
img: np.ndarray,
4082
targ_par: TargetPar,
@@ -46,12 +88,20 @@ def targ_rec(
4688
num_cam,
4789
) -> List[Target]:
4890
"""Target recognition function."""
91+
imx, imy = _control_image_size(cpar)
92+
thresholds = _target_thresholds(targ_par)
93+
disco = _target_discont(targ_par)
94+
nnmin, nnmax = _target_nn_bounds(targ_par)
95+
nxmin, nxmax = _target_nx_bounds(targ_par)
96+
nymin, nymax = _target_ny_bounds(targ_par)
97+
sumg_min = _target_min_sum_grey(targ_par)
98+
4999
if (
50100
should_use_native("target_recognition")
51101
and xmin <= 0
52102
and ymin <= 0
53-
and xmax >= cpar.imx
54-
and ymax >= cpar.imy
103+
and xmax >= imx
104+
and ymax >= imy
55105
):
56106
native_tpar = to_native_target_par(targ_par)
57107
native_cpar = to_native_control_par(cpar)
@@ -64,29 +114,23 @@ def targ_rec(
64114
subrange_y=None,
65115
)
66116
targets = from_native_target_array(native_targets)
67-
threshold = targ_par.gvthresh[int(num_cam)]
117+
threshold = thresholds[int(num_cam)]
68118
for target in targets:
69119
target.sumg -= target.n * threshold
70120
return targets
71121

72-
thres = targ_par.gvthresh[num_cam]
73-
disco = targ_par.discont
122+
thres = thresholds[num_cam]
74123

75124
# Make sure the min/max coordinates don't cause us to access memory
76125
# outside the image memory.
77126
if xmin <= 0:
78127
xmin = 1
79128
if ymin <= 0:
80129
ymin = 1
81-
if xmax >= cpar.imx:
82-
xmax = cpar.imx - 1
83-
if ymax >= cpar.imy:
84-
ymax = cpar.imy - 1
85-
86-
nnmin, nnmax = targ_par.nnmin, targ_par.nnmax
87-
nxmin, nxmax = targ_par.nxmin, targ_par.nxmax
88-
nymin, nymax = targ_par.nymin, targ_par.nymax
89-
sumg_min = targ_par.sumg_min
130+
if xmax >= imx:
131+
xmax = imx - 1
132+
if ymax >= imy:
133+
ymax = imy - 1
90134

91135
pix = fast_targ_rec(
92136
img,
@@ -293,13 +337,18 @@ def peak_fit(
293337
num_cam: int,
294338
) -> List[Target]:
295339
"""Fit the peaks in the image to a gaussian."""
296-
imx, imy = cpar.imx, cpar.imy
340+
imx, imy = _control_image_size(cpar)
341+
thresholds = _target_thresholds(targ_par)
342+
disco = _target_discont(targ_par)
343+
nnmin, nnmax = _target_nn_bounds(targ_par)
344+
nxmin, nxmax = _target_nx_bounds(targ_par)
345+
nymin, nymax = _target_ny_bounds(targ_par)
346+
sumg_min = _target_min_sum_grey(targ_par)
297347
n_peaks = 0
298348
n_wait = 0
299349
x8, y8 = [0, 1, 0, -1], [1, 0, -1, 0]
300350
p2 = 0
301-
thres = targ_par.gvthresh[num_cam]
302-
disco = targ_par.discont
351+
thres = thresholds[num_cam]
303352
intx1, inty1 = 0, 0
304353
unify = 0
305354
unified = 0
@@ -525,13 +574,13 @@ def peak_fit(
525574

526575
if (
527576
peaks[i].unr == 0
528-
and peaks[i].sumg > targ_par.sumg_min
529-
and (peaks[i].xmax - peaks[i].xmin + 1) >= targ_par.nxmin
530-
and (peaks[i].ymax - peaks[i].ymin + 1) >= targ_par.nymin
531-
and (peaks[i].xmax - peaks[i].xmin) < targ_par.nxmax
532-
and (peaks[i].ymax - peaks[i].ymin) < targ_par.nymax
533-
and peaks[i].n >= targ_par.nnmin
534-
and peaks[i].n <= targ_par.nnmax
577+
and peaks[i].sumg > sumg_min
578+
and (peaks[i].xmax - peaks[i].xmin + 1) >= nxmin
579+
and (peaks[i].ymax - peaks[i].ymin + 1) >= nymin
580+
and (peaks[i].xmax - peaks[i].xmin) < nxmax
581+
and (peaks[i].ymax - peaks[i].ymin) < nymax
582+
and peaks[i].n >= nnmin
583+
and peaks[i].n <= nnmax
535584
):
536585
sumg = peaks[i].sumg
537586

@@ -641,14 +690,19 @@ def target_recognition(
641690
-------
642691
Number of object holding the targets found.
643692
"""
693+
if hasattr(cparam, "get_image_size"):
694+
imx, imy = cparam.get_image_size()
695+
else:
696+
imx, imy = cparam.imx, cparam.imy
697+
644698
# Set the subrange (to default if not given):
645699
if subrange_x is None:
646-
xmin, xmax = 0, cparam.imx
700+
xmin, xmax = 0, imx
647701
else:
648702
xmin, xmax = subrange_x
649703

650704
if subrange_y is None:
651-
ymin, ymax = 0, cparam.imy
705+
ymin, ymax = 0, imy
652706
else:
653707
ymin, ymax = subrange_y
654708

src/openptv_python/trafo.py

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,35 @@
99
from .parameters import ControlPar
1010

1111

12+
def _control_image_size(parameters: ControlPar) -> tuple[int, int]:
13+
if hasattr(parameters, "get_image_size"):
14+
return parameters.get_image_size()
15+
return parameters.imx, parameters.imy
16+
17+
18+
def _control_pixel_size(parameters: ControlPar) -> tuple[float, float]:
19+
if hasattr(parameters, "get_pixel_size"):
20+
return parameters.get_pixel_size()
21+
return parameters.pix_x, parameters.pix_y
22+
23+
24+
def _calibration_primary_point(cal: Calibration) -> tuple[float, float]:
25+
if hasattr(cal, "get_primary_point"):
26+
primary_point = cal.get_primary_point()
27+
return float(primary_point[0]), float(primary_point[1])
28+
return float(cal.int_par.xh), float(cal.int_par.yh)
29+
30+
31+
def _calibration_added_par(cal: Calibration) -> np.ndarray:
32+
if hasattr(cal, "added_par"):
33+
return np.asarray(cal.added_par, dtype=np.float64)
34+
35+
radial = np.asarray(cal.get_radial_distortion(), dtype=np.float64)
36+
decentering = np.asarray(cal.get_decentering(), dtype=np.float64)
37+
affine = np.asarray(cal.get_affine(), dtype=np.float64)
38+
return np.r_[radial, decentering, affine]
39+
40+
1241
def pixel_to_metric(
1342
x_pixel: float, y_pixel: float, parameters: ControlPar
1443
) -> Tuple[float, float]:
@@ -20,13 +49,15 @@ def pixel_to_metric(
2049
x_pixel, y_pixel (float): input pixel coordinates.
2150
parameters (ControlPar): control structure holding image and pixel sizes.
2251
"""
52+
imx, imy = _control_image_size(parameters)
53+
pix_x, pix_y = _control_pixel_size(parameters)
2354
return fast_pixel_to_metric(
2455
x_pixel,
2556
y_pixel,
26-
parameters.imx,
27-
parameters.imy,
28-
parameters.pix_x,
29-
parameters.pix_y,
57+
imx,
58+
imy,
59+
pix_x,
60+
pix_y,
3061
)
3162

3263

@@ -80,13 +111,15 @@ def metric_to_pixel(
80111
-------
81112
x_pixel, y_pixel (float): output pixel coordinates.
82113
"""
114+
imx, imy = _control_image_size(parameters)
115+
pix_x, pix_y = _control_pixel_size(parameters)
83116
return fast_metric_to_pixel(
84117
x_metric,
85118
y_metric,
86-
parameters.imx,
87-
parameters.imy,
88-
parameters.pix_x,
89-
parameters.pix_y,
119+
imx,
120+
imy,
121+
pix_x,
122+
pix_y,
90123
)
91124

92125

@@ -115,8 +148,11 @@ def arr_metric_to_pixel(metric: np.ndarray, parameters: ControlPar) -> np.ndarra
115148
"""
116149
metric = np.atleast_2d(metric)
117150

151+
imx, imy = _control_image_size(parameters)
152+
pix_x, pix_y = _control_pixel_size(parameters)
153+
118154
return fast_arr_metric_to_pixel(
119-
metric, parameters.imx, parameters.imy, parameters.pix_x, parameters.pix_y
155+
metric, imx, imy, pix_x, pix_y
120156
)
121157

122158

@@ -228,21 +264,22 @@ def flat_to_dist(flat_x: float, flat_y: float, cal: Calibration) -> Tuple[float,
228264
image coordinates, because distortion formula assumes it, [1] p.180.
229265
"""
230266
# print(f"flat_x {flat_x}, flat_y {flat_y}")
231-
# print(f"cal.int {cal.int_par.xh}, {cal.int_par.yh}")
232-
flat_x += cal.int_par.xh
233-
flat_y += cal.int_par.yh
267+
xh, yh = _calibration_primary_point(cal)
268+
flat_x += xh
269+
flat_y += yh
234270

235271
# print(f"flat_x {flat_x}, flat_y {flat_y}")
236272

237-
dist_x, dist_y = distort_brown_affine(flat_x, flat_y, cal.added_par)
273+
dist_x, dist_y = distort_brown_affine(flat_x, flat_y, _calibration_added_par(cal))
238274
# print(f"dist_x {dist_x}, dist_y {dist_y}")
239275

240276
return dist_x, dist_y
241277

242278

243279
def dist_to_flat(dist_x: float, dist_y: float, cal: Calibration, tol: float = 1e-5):
244280
"""Convert real-image coordinates to flat-image coordinates."""
245-
flat_x, flat_y = correct_brown_affine(dist_x, dist_y, cal.added_par, tol)
246-
flat_x -= cal.int_par.xh
247-
flat_y -= cal.int_par.yh
281+
xh, yh = _calibration_primary_point(cal)
282+
flat_x, flat_y = correct_brown_affine(dist_x, dist_y, _calibration_added_par(cal), tol)
283+
flat_x -= xh
284+
flat_y -= yh
248285
return flat_x, flat_y

0 commit comments

Comments
 (0)