Skip to content

Commit d394c2a

Browse files
committed
fixed all the tests, there was a bug with Calibraiton initalization
1 parent a83f499 commit d394c2a

5 files changed

Lines changed: 41 additions & 57 deletions

File tree

openptv_python/calibration.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Calibration data structures and functions."""
22

3+
import copy
34
import pathlib
45
from typing import Optional
56

@@ -104,15 +105,15 @@ def __init__(self,
104105
mmlut=None,
105106
mmlut_data=None):
106107
if ext_par is None:
107-
ext_par = Exterior.copy()
108+
ext_par = copy.deepcopy(Exterior)
108109
if int_par is None:
109-
int_par = Interior.copy()
110+
int_par = copy.deepcopy(Interior)
110111
if glass_par is None:
111112
glass_par = np.array([0.0, 0.0, 1.0])
112113
if added_par is None:
113-
added_par = ap_52.copy()
114+
added_par = np.array((0, 0, 0, 0, 0, 1, 0), dtype = np.float64)
114115
if mmlut is None:
115-
mmlut = mm_lut.copy() # (np.zeros(3), 0, 0, 0)
116+
mmlut = copy.deepcopy(mm_lut) # (np.zeros(3), 0, 0, 0)
116117
if mmlut_data is None:
117118
mmlut_data = np.zeros((mmlut.nr, mmlut.nz), dtype=np.float64)
118119

@@ -378,7 +379,7 @@ def get_glass_vec(self) -> np.ndarray:
378379
def set_added_par(self, ap52_array: np.ndarray):
379380
"""Set added par from an numpy array of parameters."""
380381
# self.added_par = np.array(tuple(ap52_array.tolist()), dtype = ap52_dtype).view(np.recarray)
381-
self.added_par = ap52_array
382+
self.added_par = ap52_array.astype(np.float64)
382383

383384
def copy(self, new_copy):
384385
"""Copy the calibration data to a new object."""
@@ -415,8 +416,8 @@ def write_ori(
415416

416417
with open(add_file, "w", encoding="utf-8") as fp:
417418
fp.write(
418-
f"{added_par.k1:.8f} {added_par.k2:.8f} {added_par.k3:.8f} "
419-
f"{added_par.p1:.8f} {added_par.p2:.8f} {added_par.scx:.8f} {added_par.she:.8f}\n"
419+
f"{added_par[0]:.8f} {added_par[1]:.8f} {added_par[2]:.8f} "
420+
f"{added_par[3]:.8f} {added_par[4]:.8f} {added_par[5]:.8f} {added_par[6]:.8f}\n"
420421
)
421422
success = True
422423

@@ -491,15 +492,7 @@ def compare_calibration(c1: Calibration, c2: Calibration) -> bool:
491492

492493
def compare_addpar(a1, a2):
493494
"""Compare added parameters."""
494-
return (
495-
(a1.k1 == a2.k1)
496-
and (a1.k2 == a2.k2)
497-
and (a1.k3 == a2.k3)
498-
and (a1.p1 == a2.p1)
499-
and (a1.p2 == a2.p2)
500-
and (a1.scx == a2.scx)
501-
and (a1.she == a2.she)
502-
)
495+
return np.array_equal(a1, a2)
503496

504497

505498
def read_calibration(ori_file: str, addpar_file: str) -> Calibration:

tests/test_calibration_binding.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,17 @@ def test_full_instantiate(self):
5555

5656
G = np.array(glass)
5757

58-
addpar = np.array((
58+
addpar = np.array(
59+
[
5960
rad_dist[0],
6061
rad_dist[1],
6162
rad_dist[2],
6263
decent[0],
6364
decent[1],
6465
affine[0],
65-
affine[1]),dtype=ap_52.dtype).view(np.recarray)
66+
affine[1]
67+
]
68+
)
6669

6770

6871
cal = Calibration()

tests/test_calibration_class.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,8 @@ def test_glass_initialization(self):
3131

3232
def test_ap_52_initialization(self):
3333
"""Test ap_52 parameters initialization."""
34-
ap = self.cal.added_par
35-
assert ap.k1 == 0.0
36-
assert ap.k2 == 0.0
37-
assert ap.k3 == 0.0
38-
assert ap.p1 == 0.0
39-
assert ap.p2 == 0.0
40-
assert ap.scx == 1.0
41-
assert ap.she == 0.0
34+
assert np.array_equal(self.cal.added_par, \
35+
np.array([0,0,0,0,0,1,0],dtype=np.float64))
4236

4337
def test_mmlut_initialization(self):
4438
"""Test mmlut parameters initialization."""
@@ -59,7 +53,7 @@ def test_calibration_initialization(self):
5953
assert self.cal.added_par.dtype == ap_52.dtype
6054
assert self.cal.mmlut.dtype == mm_lut.dtype
6155

62-
assert isinstance(self.cal.added_par, np.recarray)
56+
assert isinstance(self.cal.added_par, np.ndarray)
6357
assert isinstance(self.cal.mmlut, np.recarray)
6458

6559
def test_exterior_initialization(self):

tests/test_trafo.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,7 @@ def test_round_trip(self):
6060

6161
cal = Calibration()
6262
cal.int_par.xh, cal.int_par.yh, cal.int_par.cc = 1.5, 1.5, 60.0
63-
(
64-
cal.added_par.k1,
65-
cal.added_par.k2,
66-
cal.added_par.k3,
67-
cal.added_par.p1,
68-
cal.added_par.p2,
69-
cal.added_par.scx,
70-
cal.added_par.she,
71-
) = (1e-5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
63+
cal.added_par = np.array((1e-5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0),dtype=np.float64)
7264

7365
xdist, ydist = flat_to_dist(x, y, cal)
7466
xflat, yflat = dist_to_flat(xdist, ydist, cal, iter_eps)
@@ -79,8 +71,8 @@ def test_round_trip(self):
7971
def test_shear_distortion(self):
8072
"""Test the shear distortion parameter."""
8173
x, y = 1.0, 1.0
82-
ap = ap_52.copy()
83-
ap.she=1.0
74+
ap = ap_52[:]
75+
ap[6]=1.0
8476
xp, yp = distort_brown_affine(x, y, ap)
8577
self.assertAlmostEqual(xp, 0.158529)
8678
self.assertAlmostEqual(yp, 0.5403023)
@@ -137,7 +129,7 @@ def test_round_trip2(self):
137129
"""
138130
x, y = 1.0, 1.0
139131
eps = 1e-5
140-
ap = ap_52.copy() # no distortion
132+
ap = ap_52[:] # no distortion
141133

142134
xres, yres = distort_brown_affine(x, y, ap)
143135
xres, yres = correct_brown_affine(xres, yres, ap)
@@ -153,7 +145,7 @@ def test_shear_round_trip(self):
153145
"""
154146
x, y = -1.0, 10.0
155147
eps = 1e-5
156-
ap = ap_52.copy()
148+
ap = ap_52[:]
157149
# ap.she = 1.0 # no distortion
158150

159151
xres, yres = distort_brown_affine(x, y, ap)

tests/test_trafo_mockup.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import copy
1+
22
import unittest
33

44
import numpy as np
55

6-
from openptv_python.calibration import Calibration, Interior
6+
from openptv_python.calibration import Calibration, interior_dtype
77
from openptv_python.parameters import ControlPar
88
from openptv_python.trafo import (
99
dist_to_flat,
@@ -19,48 +19,47 @@ class TestFlatToDist(unittest.TestCase):
1919
"""Test the flat_to_dist function."""
2020

2121
def setUp(self):
22-
self.cal = Calibration()
23-
print(f" k1 = {self.cal.added_par.k1}")
24-
2522
self.cpar = ControlPar()
2623
self.cpar.imx = 1024
2724
self.cpar.imy = 1008
2825
self.cpar.pix_x = 0.010
2926
self.cpar.pix_y = 0.010
3027

3128
def test_zero_coordinates(self):
32-
dist_x, dist_y = flat_to_dist(0, 0, self.cal)
29+
"""Test the case when the input coordinates are zero."""
30+
cal = Calibration()
31+
dist_x, dist_y = flat_to_dist(0, 0, cal)
3332
self.assertEqual(dist_x, 0)
3433
self.assertEqual(dist_y, 0)
3534

3635
def test_positive_coordinates(self):
37-
cal = copy.copy(self.cal)
36+
"""Test the case when the input coordinates are positive."""
37+
cal = Calibration()
3838
cal.int_par.xh = 100
3939
cal.int_par.yh = 50
40-
print("inside test_positive_coordinates\n")
41-
print(f"cal.added_par.k1 = {cal.added_par.k1}\n")
42-
print(f"self.cal.added_par.k1 = {self.cal.added_par.k1}\n")
4340
dist_x, dist_y = flat_to_dist(50, 25, cal)
4441
self.assertEqual(dist_x, 150)
4542
self.assertEqual(dist_y, 75)
4643

4744
def test_distortion(self):
48-
cal = copy.copy(self.cal)
49-
cal.set_added_par(np.array([1e-5, 0, 0, 0, 0, 1, 0]))
45+
"""Test the case when the distortion is not zero."""
46+
cal = Calibration()
47+
cal.set_added_par(np.array([1e-5, 0, 0, 0, 0, 1, 0], dtype=np.float64))
5048
dist_x, dist_y = flat_to_dist(100, 200, cal)
5149
self.assertAlmostEqual(dist_x, 150, places=3)
5250
self.assertAlmostEqual(dist_y, 300, places=3)
5351

5452
def test_pixel_to_metric_and_back(self):
55-
cpar = copy.copy(self.cpar)
56-
cpar.from_file("tests/testing_folder/control_parameters/control.par")
53+
"""Test the pixel_to_metric and metric_to_pixel functions."""
54+
cpar = ControlPar().from_file("tests/testing_folder/control_parameters/control.par")
5755

5856
x, y = metric_to_pixel(1, 1, cpar)
5957
x, y = pixel_to_metric(x, y, cpar)
6058
self.assertTrue(abs(x - 1) < EPS)
6159
self.assertTrue(abs(y - 1) < EPS)
6260

6361
def test_0_0(self):
62+
"""Test the case when the input coordinates are zero."""
6463
xc = 0.0
6564
yc = 0.0
6665

@@ -71,6 +70,7 @@ def test_0_0(self):
7170
self.assertTrue(abs(yc1 - yc) < EPS)
7271

7372
def test_1_0(self):
73+
"""Test the case when the input coordinates are (1, 0)."""
7474
xc = 1.0
7575
yc = 0.0
7676

@@ -81,6 +81,7 @@ def test_1_0(self):
8181
self.assertTrue(abs(yc1 - yc) < EPS)
8282

8383
def test_0_neg1(self):
84+
"""Test the case when the input coordinates are (0, -1)."""
8485
xc = 0.0
8586
yc = -1.0
8687

@@ -91,6 +92,7 @@ def test_0_neg1(self):
9192
self.assertTrue(abs(yc1 - yc) < EPS)
9293

9394
def dist_flat_round_trip(self):
95+
"""Test the round trip from flat to distorted and back."""
9496
# /* Cheks that the order of operations in converting metric flat image to
9597
# distorted image coordinates and vice-versa is correct.
9698

@@ -104,9 +106,9 @@ def dist_flat_round_trip(self):
104106
y = 10.0
105107
iter_eps = 1e-5
106108

107-
cal = copy.copy(self.cal)
108-
cal.int_par = Interior(1.5, 1.5, 60.0)
109-
cal.set_added_par([0.0005, 0, 0, 0, 0, 1, 0])
109+
cal = Calibration()
110+
cal.int_par = np.array( (1.5, 1.5, 60.0), dtype = interior_dtype).view(np.recarray)
111+
cal.set_added_par(np.array([0.0005, 0, 0, 0, 0, 1, 0], dtype=np.float64))
110112

111113
xres, yres = flat_to_dist(x, y, cal)
112114
xres, yres = dist_to_flat(xres, yres, cal, 0.00001)

0 commit comments

Comments
 (0)