Skip to content

Commit

Permalink
track test is the last one. stops on 10996
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlib committed Nov 17, 2023
1 parent 44c8916 commit d3f16e3
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 129 deletions.
2 changes: 1 addition & 1 deletion openptv_python/correspondences.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def safely_allocate_adjacency_lists(
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])]
lists[c1][c2] = [Correspond() for _ in range(target_counts[c1])] # type: ignore
if lists[c1][c2] is None:
error = 1
lists[c1][c2] = None
Expand Down
16 changes: 9 additions & 7 deletions openptv_python/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from pathlib import Path
from typing import List, Tuple

from openptv_python.constants import TR_MAX_CAMS


@dataclass
class MultimediaPar:
Expand Down Expand Up @@ -546,16 +548,16 @@ def read_target_par(filename: str) -> TargetPar | None:
ret = TargetPar()
try:
with open(filename, "r", encoding="utf-8") as file:
for i in range(4): # todo - make it no. cameras
for _ in range(TR_MAX_CAMS): # todo - make it no. cameras
ret.gvthresh.append(int(file.readline()))

ret.discont = int(file.readline())
ret.nnmin = float(file.readline())
ret.nnmax = float(file.readline())
ret.nxmin = float(file.readline())
ret.nxmax = float(file.readline())
ret.nymin = float(file.readline())
ret.nymax = float(file.readline())
ret.nnmin = int(file.readline())
ret.nnmax = int(file.readline())
ret.nxmin = int(file.readline())
ret.nxmax = int(file.readline())
ret.nymin = int(file.readline())
ret.nymax = int(file.readline())
ret.sumg_min = int(file.readline())
ret.cr_sz = int(file.readline())
return ret
Expand Down
98 changes: 29 additions & 69 deletions openptv_python/segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,28 @@
from typing import List

import numpy as np
from scipy.ndimage import center_of_mass, gaussian_filter, label, maximum_filter

from .constants import CORRES_NONE, NMAX
from .constants import CORRES_NONE
from .parameters import ControlPar, TargetPar
from .tracking_frame_buf import Target, TargetArray
from .tracking_frame_buf import Target


@dataclass
class Peak:
"""Peak dataclass."""

pos: int | None = None
status: int | None = None
xmin: int | None = None
xmax: int | None = None
ymin: int | None = None
ymax: int | None = None
n: int | None = None
sumg: int | None = None
x: float | None = None
y: float | None = None
unr: int | None = None
pos: int = 0
status: int = 0
xmin: int = 0
xmax: int = 0
ymin: int = 0
ymax: int = 0
n: int = 0
sumg: int = 0
x: float = 0.0
y: float = 0.0
unr: int = 0
touch: list[int] = field(default_factory=list, repr=False)
n_touch: int = 0

Expand All @@ -36,7 +37,7 @@ def targ_rec(
ymax: int,
cpar: ControlPar,
num_cam,
) -> TargetArray:
) -> List[Target]:
"""Target recognition function."""
n = 0
n_wait = 0
Expand Down Expand Up @@ -141,6 +142,7 @@ def targ_rec(
and (gvref + disco >= img[yn, xn - 1])
and (gvref + disco >= img[yn, xn + 1])
):
print(f"gv = {gv} sumg = {sumg}")
sumg += gv
img0[yn, xn] = 0
if xn < xa:
Expand Down Expand Up @@ -188,11 +190,7 @@ def targ_rec(
and ny <= targ_par.nymax
and sumg > targ_par.sumg_min
):
pix.append(Target())
pix[n_targets].n = numpix
pix[n_targets].nx = nx
pix[n_targets].ny = ny
pix[n_targets].sumg = sumg
pix.append(Target(n=numpix, nx=nx, ny=ny, sumg=sumg))
sumg -= numpix * thres
# finish the grey-value weighting:
x /= sumg
Expand Down Expand Up @@ -222,17 +220,15 @@ def peak_fit(
ymax: int,
cpar: ControlPar,
num_cam: int,
) -> TargetArray:
) -> List[Target]:
"""Fit the peaks in the image to a gaussian."""
imx, imy = cpar.imx, cpar.imy
n_peaks = 0
n_wait = 0
x8, y8 = [0, 1, 0, -1], [1, 0, -1, 0]
p2 = 0
thres = targ_par.gvthres[num_cam]
thres = targ_par.gvthresh[num_cam]
disco = targ_par.discont
pnr, sumg, xn, yn = 0, 0, 0, 0
n_target = 0
intx1, inty1 = 0, 0
unify = 0
unified = 0
Expand All @@ -241,12 +237,10 @@ 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)
# nmax = 1024
nmax = NMAX
peaks = [0] * (4 * nmax)
ptr_peak = Peak()
peaks = []
waitlist = [[]]
pix = []
n_target = 0

for i in range(ymin, ymax - 1):
for j in range(xmin, xmax):
Expand Down Expand Up @@ -275,21 +269,7 @@ def peak_fit(
# label peak in label_img, initialize peak
n_peaks += 1
label_img[n] = n_peaks
ptr_peak.pos = n
ptr_peak.status = 1
ptr_peak.xmin = j
ptr_peak.xmax = j
ptr_peak.ymin = i
ptr_peak.ymax = i
ptr_peak.unr = 0
ptr_peak.n = 0
ptr_peak.sumg = 0
ptr_peak.x = 0
ptr_peak.y = 0
ptr_peak.n_touch = 0
for k in range(4):
ptr_peak.touch[k] = 0
ptr_peak += 1
peaks.append(Peak(pos=n, status=1, xmin=j, xmax=i, ymin=i, ymax=i))

waitlist[0][0] = j
waitlist[0][1] = i
Expand Down Expand Up @@ -498,10 +478,10 @@ def peak_fit(
pix[n_target].pnr = n_target
n_target += 1

t = TargetArray()
t.num_targs = n_target
t.targs = pix
return t
# t = TargetArray(num_targets=n_target)
# t.num_targs = n_target
# t.append(pix)
return pix


def check_touch(tpeak, p1, p2):
Expand Down Expand Up @@ -544,12 +524,10 @@ def peak_fit_new(
-------
List[Peak]: A list of Peak objects representing the detected peaks.
"""
from scipy.ndimage import center_of_mass, gaussian_filter, label, maximum_filter

smoothed = gaussian_filter(image, sigma)
mask = smoothed > threshold * np.max(smoothed)
mask = smoothed > threshold * np.max(smoothed) # type: ignore
maxima = maximum_filter(smoothed, footprint=np.ones((3, 3))) == smoothed
labeled, num_objects = label(maxima)
labeled, num_objects = label(maxima) # type: ignore
peaks = []
for i in range(num_objects):
indices = np.argwhere(labeled == i + 1)
Expand All @@ -560,32 +538,14 @@ def peak_fit_new(
return peaks


def test_peak_fit_new():
import matplotlib.pyplot as plt

# Generate test image
x, y = np.meshgrid(np.linspace(-5, 5, 100), np.linspace(-5, 5, 100))
z = np.sin(np.sqrt(x**2 + y**2))

# Find peaks
peaks = peak_fit(z)

# Plot image and detected peaks
fig, ax = plt.subplots()
ax.imshow(z, cmap="viridis")
for peak in peaks:
ax.scatter(peak.y, peak.x, marker="x", c="r", s=100)
plt.show()


def target_recognition(
img: np.ndarray,
tpar: TargetPar,
cam: int,
cparam: ControlPar,
subrange_x=None,
subrange_y=None,
) -> TargetArray:
) -> List[Target]:
"""
Detect targets (contiguous bright blobs) in an image.
Expand Down
35 changes: 24 additions & 11 deletions openptv_python/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,18 @@ def copy_foundpix_array(


def register_closest_neighbs(
targets, num_targets, cam, cent_x, cent_y, dl, dr, du, dd, reg, cpar
):
targets: List[Target],
num_targets: int,
cam: int,
cent_x: float,
cent_y: float,
dl: float,
dr: float,
du: float,
dd: float,
reg: List[Foundpix],
cpar: ControlPar,
) -> List[int]:
"""Register_closest_neighbs() finds candidates for continuing a particle's.
path in the search volume, and registers their data in a foundpix array
Expand All @@ -111,21 +121,23 @@ def register_closest_neighbs(
reg -- an array of foundpix objects, one for each possible neighbour. Output array.
cpar -- control parameter object
"""
all_cands = [-999] * MAX_CANDS # Initialize all candidate indexes to -999
# all_cands = [-999] * MAX_CANDS # Initialize all candidate indexes to -999

candsearch_in_pix(
targets, num_targets, cent_x, cent_y, dl, dr, du, dd, all_cands, cpar
all_cands = candsearch_in_pix(
targets, num_targets, cent_x, cent_y, dl, dr, du, dd, cpar
)

for cand_idx in range(MAX_CANDS):
# Set default value for unused foundpix objects
if all_cands[cand_idx] == -999:
if all_cands[cand_idx] == TR_UNUSED:
reg[cand_idx].ftnr = TR_UNUSED
else:
# Register candidate data in the foundpix object
reg[cand_idx].whichcam[cam] = 1
reg[cand_idx].ftnr = targets[all_cands[cand_idx]].tnr

return all_cands


def search_volume_center_moving(prev_pos, curr_pos, output):
"""Find the position of the center of the search volume for a moving.
Expand Down Expand Up @@ -238,13 +250,13 @@ def candsearch_in_pix(
dr: float,
du: float,
dd: float,
p: List[int],
cpar: ControlPar,
) -> int:
) -> List[int]:
"""Search for a nearest candidate in unmatched target list."""
counter = 0
dmin = 1e20
p1 = p2 = p3 = p4 = -1
p1 = p2 = p3 = p4 = TR_UNUSED
p = [-1] * MAX_CANDS
d1 = d2 = d3 = d4 = dmin

xmin, xmax, ymin, ymax = cent_x - dl, cent_x + dr, cent_y - du, cent_y + dd
Expand Down Expand Up @@ -305,11 +317,12 @@ def candsearch_in_pix(

# print("from inside p = ", p)

# TODO: check why we need counter
for j in range(4):
if p[j] != -1:
counter += 1

return counter
return p


def candsearch_in_pix_rest(
Expand Down Expand Up @@ -561,7 +574,7 @@ def sorted_candidates_in_volume(center, center_proj, frm, run):
right[cam],
up[cam],
down[cam],
points[cam * MAX_CANDS],
points,
run.cpar,
)

Expand Down
54 changes: 30 additions & 24 deletions openptv_python/tracking_frame_buf.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def read_targets(file_base: str, frame_num: int) -> List[Target]:
# file_base = file_base.split(".")[0]

if frame_num > 0:
filename = f"{file_base}{frame_num}_targets"
filename = f"{file_base}{frame_num:04d}_targets"
else:
filename = f"{file_base}_targets"

Expand Down Expand Up @@ -816,35 +816,41 @@ def write_path_frame(
success = False

try:
with open(corres_fname, "w", encoding="utf8") as corres_file:
corres_file.write(f"{num_parts}\n")
with open(linkage_fname, "w", encoding="utf8") as linkage_file:
linkage_file.write(f"{num_parts}\n")
corres_file = open(corres_fname, "w", encoding="utf8")
corres_file.write(f"{num_parts}\n")

if prio_file_base is not None:
with open(prio_fname, "w", encoding="utf8") as prio_file: # type: ignore
prio_file.write(f"{num_parts}\n")
linkage_file = open(linkage_fname, "w", encoding="utf8")
linkage_file.write(f"{num_parts}\n")

if prio_file_base is not None:
prio_file = open(prio_fname, "w", encoding="utf8") # type: ignore
prio_file.write(f"{num_parts}\n")

for pix in range(num_parts):
linkage_file.write(
for pix in range(num_parts):
linkage_file.write(
f"{path_buf[pix].prev_frame} {path_buf[pix].next_frame} "
f"{path_buf[pix].x[0]:.3f} {path_buf[pix].x[1]:.3f} "
f"{path_buf[pix].x[2]:.3f}\n"
)

corres_file.write(
f"{pix + 1} {path_buf[pix].x[0]:.3f} "
f"{path_buf[pix].x[1]:.3f} {path_buf[pix].x[2]:.3f} "
f"{cor_buf[pix].p[0]} {cor_buf[pix].p[1]} "
f"{cor_buf[pix].p[2]} {cor_buf[pix].p[3]}\n"
)

if prio_file_base is not None:
prio_file.write(
f"{path_buf[pix].prev_frame} {path_buf[pix].next_frame} "
f"{path_buf[pix].x[0]:.3f} {path_buf[pix].x[1]:.3f} "
f"{path_buf[pix].x[2]:.3f}\n"
)

corres_file.write(
f"{pix + 1} {path_buf[pix].x[0]:.3f} "
f"{path_buf[pix].x[1]:.3f} {path_buf[pix].x[2]:.3f} "
f"{cor_buf[pix].p[0]} {cor_buf[pix].p[1]} "
f"{cor_buf[pix].p[2]} {cor_buf[pix].p[3]}\n"
f"{path_buf[pix].x[2]:.3f} {path_buf[pix].prio}\n"
)

if prio_file_base:
prio_file.write(
f"{path_buf[pix].prev_frame} {path_buf[pix].next_frame} "
f"{path_buf[pix].x[0]:.3f} {path_buf[pix].x[1]:.3f} "
f"{path_buf[pix].x[2]:.3f} {path_buf[pix].prio}\n"
)
corres_file.close()
linkage_file.close()
if prio_file_base is not None:
prio_file.close()

success = True

Expand Down
Loading

0 comments on commit d3f16e3

Please sign in to comment.