Skip to content

Commit

Permalink
seems that most of it works
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlib committed Nov 18, 2023
1 parent 1d7aa50 commit 3a2ba7f
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 51 deletions.
16 changes: 8 additions & 8 deletions openptv_python/calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pathlib
from dataclasses import dataclass, field
from typing import Optional
from typing import List, Optional

import numpy as np

Expand Down Expand Up @@ -111,15 +111,15 @@ class ap_52:
scx: float = 1.0
she: float = 0.0

def set_radial_distortion(self, dist_list: np.ndarray) -> None:
def set_radial_distortion(self, dist_list: List[float]) -> None:
"""Set the radial distortion parameters k1, k2, k3."""
self.k1, self.k2, self.k3 = dist_list

def set_decentering(self, decent: np.ndarray) -> None:
def set_decentering(self, decent: List[float]) -> None:
"""Set the decentring parameters p1 and p2."""
self.p1, self.p2 = decent

def set_affine_distortion(self, affine: np.ndarray) -> None:
def set_affine_distortion(self, affine: List[float]) -> None:
"""Set the affine distortion parameters scx and she."""
self.scx, self.she = affine

Expand Down Expand Up @@ -214,7 +214,7 @@ def from_file(self, ori_file: str, add_file: str) -> None:
# Additional parameters
try:
with open(add_file, "r", encoding="utf-8") as fp:
tmp = np.array(list(map(float, fp.readline().split())))
tmp = list(map(float, fp.readline().split()))

self.added_par.set_radial_distortion(tmp[:3])
self.added_par.set_decentering(tmp[3:5])
Expand Down Expand Up @@ -313,7 +313,7 @@ def get_primary_point(self):
"""
return np.r_[self.int_par.xh, self.int_par.yh, self.int_par.cc]

def set_radial_distortion(self, dist_coeffs: np.ndarray):
def set_radial_distortion(self, dist_coeffs: List[float]) -> None:
"""
Set the parameters for the image radial distortion, where the x/y.
Expand All @@ -337,7 +337,7 @@ def get_radial_distortion(self):
"""
return np.r_[self.added_par.k1, self.added_par.k2, self.added_par.k3]

def set_decentering(self, decent: np.ndarray) -> None:
def set_decentering(self, decent: List[float]) -> None:
"""
Set the parameters of decentering distortion (a.k.a. p1, p2, see [1]).
Expand All @@ -357,7 +357,7 @@ def get_decentering(self):
ret[1] = self.added_par.p2
return ret

def set_affine_trans(self, affine: np.ndarray) -> None:
def set_affine_trans(self, affine: List[float]) -> None:
"""
Set the affine transform parameters (x-scale, shear) of the image.
Expand Down
59 changes: 37 additions & 22 deletions openptv_python/correspondences.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,31 +70,46 @@ def safely_allocate_target_usage_marks(
# lists[c1][c2] = None


# def safely_allocate_adjacency_lists(
# num_cams: int, target_counts: List[int]
# ) -> List[List[List[Correspond]]]:
# """Allocate adjacency lists."""
# lists = [[[] for _ in range(num_cams)] for _ in range(num_cams)]
# error = 0

# for c1 in range(num_cams - 1):
# for c2 in range(c1 + 1, num_cams):
# if error == 0:
# lists[c1][c2] = [Correspond() for _ in range(target_counts[c1])] # type: ignore
# if not lists[c1][c2]:
# error = 1
# lists[c1][c2] = []

# for edge in range(target_counts[c1]):
# lists[c1][c2][edge].n = 0
# lists[c1][c2][edge].p1 = 0
# else:
# lists[c1][c2] = []

# if error == 0:
# return lists

# return []


def safely_allocate_adjacency_lists(
num_cams: int, target_counts: List[int]
) -> List[List[List[Correspond]]]:
"""Allocate adjacency lists."""
lists = [[[float] for _ in range(num_cams)] for _ in range(num_cams)]
error = 0

for c1 in range(num_cams - 1):
for c2 in range(c1 + 1, num_cams):
if error == 0:
lists[c1][c2] = [Correspond() for _ in range(target_counts[c1])] # type: ignore
if not lists[c1][c2]:
error = 1
lists[c1][c2] = []

for edge in range(target_counts[c1]):
lists[c1][c2][edge].n = 0
lists[c1][c2][edge].p1 = 0
else:
lists[c1][c2] = []

if error == 0:
return lists

return []
try:
lists = [
[[Correspond() for _ in range(target_counts[c1])] for _ in range(num_cams)]
for c1 in range(num_cams)
]
except MemoryError:
print("Memory allocation failed.")
lists = []

return lists


def four_camera_matching(
Expand Down
3 changes: 0 additions & 3 deletions openptv_python/epi.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ def epi_mm(xl, yl, cal1, cal2, mmp, vpar) -> tuple[float, float, float, float]:
-------
_type_: _description_
"""
z_min, z_max = 0, 0
# pos, v, X = [0, 0, 0], [0, 0, 0], [0, 0, 0]

pos, v = ray_tracing(xl, yl, cal1, mmp)

# calculate min and max depth for position (valid only for one setup)
Expand Down
2 changes: 1 addition & 1 deletion openptv_python/find_candidate.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def find_candidate(
-------
cand: list of candidates, or empty list if nothing is found
"""
cand = []
cand: List[Candidate] = []

# The image space is the image plane of the camera. The image space is
# given in millimeters of sensor size and the origin is in the center of the sensor.
Expand Down
3 changes: 3 additions & 0 deletions openptv_python/imgcoord.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ def img_coord(
) -> Tuple[float, float]:
"""Image coordinate."""
# Estimate metric coordinates in image space using flat_image_coord()
if pos.shape[0] != 3:
raise ValueError("pos must be a 3D vector")

x, y = flat_image_coord(pos, cal, mm)
# print(f"flat_image_coord: x = {x}, y = {y}")

Expand Down
5 changes: 2 additions & 3 deletions openptv_python/orientation.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def weighted_dumbbell_precision(
"""Calculate the weighted dumbbell precision of the current orientation."""
res = [np.empty((3,)), np.empty((3,))]
dtot = 0.0
len_err_tot = 0.0
len_err_tot: float = 0.0

num_targs = targets.shape[0]
num_cams = targets.shape[1]
Expand All @@ -124,7 +124,7 @@ def weighted_dumbbell_precision(

if pt % 2 == 1:
dist = np.linalg.norm(res[0] - res[1])
len_err_tot += 1 - (
len_err_tot += 1.0 - float(
db_length / dist if dist > db_length else dist / db_length
)

Expand Down Expand Up @@ -590,7 +590,6 @@ def raw_orient(
beta = np.zeros(6)
dm = 0.0001
drad = 0.0001
xp, yp, xc, yc = 0, 0, 0, 0
pos = np.zeros(3)

cal.added_par.k1 = 0
Expand Down
6 changes: 3 additions & 3 deletions openptv_python/segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,9 @@ def peak_fit(
gv1, gv2 = 0, 0
x1, x2, y1, y2, s12 = 0.0, 0.0, 0.0, 0.0, 0.0
label_img = [0] * (imx * imy)
peaks = []
waitlist = [[]]
pix = []
peaks: List[Peak] = []
waitlist: List[List[int]] = [[]]
pix: List[Target] = []
n_target = 0

for i in range(ymin, ymax - 1):
Expand Down
5 changes: 3 additions & 2 deletions openptv_python/vec_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ def vec_norm(vec: np.ndarray) -> float:
return float(np.linalg.norm(vec))


def vec_dot(vec1: np.ndarray, vec2: np.ndarray) -> np.ndarray:
def vec_dot(vec1: np.ndarray, vec2: np.ndarray) -> float:
"""vec_dot() gives the dot product of two vectors as lists of floats."""
return np.dot(vec1, vec2)
val = np.dot(vec1, vec2)
return float(val)


def vec_cross(vec1: np.ndarray, vec2: np.ndarray) -> np.ndarray:
Expand Down
25 changes: 16 additions & 9 deletions tests/gen_track_data.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import List

import numpy as np

from openptv_python.calibration import Calibration
Expand All @@ -23,22 +25,26 @@
cpar = ControlPar(num_cams=3)
cpar.from_file("testing_fodder/track/parameters/control_newpart.par")

targs = []
targs: List[List[List[float]]] = [
[[0.0, 0.0] for _ in range(num_frames)] for _ in range(num_cams)
]

for cam in range(num_cams):
cal = Calibration()
cal.from_file(
f"testing_fodder/cal/sym_cam{cam+1}.tif.ori",
"testing_fodder/cal/cam1.tif.addpar",
)
# check this out
x, y = img_coord(part_traject, cal, cpar.mm)
x, y = metric_to_pixel(x, y, cpar)
targs.append([x, y])
for frame in range(num_frames):
x, y = img_coord(part_traject[frame, :], cal, cpar.mm)
x, y = metric_to_pixel(x, y, cpar)
targs[cam][frame] = [x, y]

for frame in range(num_frames):
# write 3D positions:
with open(
"testing_fodder/track/res_orig/particles.%d" % (frame + 1), "w"
f"testing_fodder/track/res_orig/particles.{frame+1}", "w", encoding="utf-8"
) as outfile:
# Note correspondence to the single target in each frame.
outfile.writelines(
Expand All @@ -48,7 +54,7 @@
1,
part_traject[frame, 0],
part_traject[frame, 1],
part_traject[frame, 1],
part_traject[frame, 2],
0,
0,
0,
Expand All @@ -60,16 +66,17 @@
# write associated targets from all cameras:
for cam in range(num_cams):
with open(
"testing_fodder/track/newpart/cam%d.%04d_targets" % (cam + 1, frame + 1),
f"testing_fodder/track/newpart/cam{cam+1}.{frame+1:04d}_targets",
"w",
encoding="utf-8",
) as outfile:
outfile.writelines(
[
str(1) + "\n",
"{:5d}{:10.3f}{:10.3f}{:5d}{:5d}{:5d}{:10d}{:5d}\n".format(
0,
targs[cam][frame, 0],
targs[cam][frame, 1],
targs[cam][frame][0],
targs[cam][frame][1],
100,
10,
10,
Expand Down

0 comments on commit 3a2ba7f

Please sign in to comment.