Skip to content

Commit

Permalink
candsearch_in_pix_rest
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlib committed Nov 9, 2023
1 parent af5afb5 commit ceb41ca
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
37 changes: 24 additions & 13 deletions openptv_python/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
MAX_CANDS,
MAX_TARGETS,
NEXT_NONE,
POS_INF,
PREV_NONE,
PT_UNUSED,
TR_BUFSPACE,
Expand Down Expand Up @@ -401,7 +402,7 @@ def candsearch_in_pix(
p[2] = p3
p[3] = p4

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

for j in range(4):
if p[j] != -1:
Expand All @@ -410,13 +411,25 @@ def candsearch_in_pix(
return counter


def candsearch_in_pix_rest(next_frame, cent_x, cent_y, dl, dr, du, dd, cpar):
def candsearch_in_pix_rest(
next_frame: List[Target],
num_targets: int,
cent_x: float,
cent_y: float,
dl: float,
dr: float,
du: float,
dd: float,
p: List[int],
cpar: ControlPar,
) -> int:
"""Search for a nearest candidate in unmatched target list.
Arguments:
---------
next_frame - 2D numpy array of targets (pointer, x,y, n, nx,ny, sumg, track ID),
assumed to be y sorted.
num_targets - number of targets in the next_frame
cent_x, cent_y - image coordinates of the position of a particle [pixel]
dl, dr, du, dd - respectively the left, right, up, down distance
to the search area borders from its center, [pixel]
Expand All @@ -427,32 +440,30 @@ def candsearch_in_pix_rest(next_frame, cent_x, cent_y, dl, dr, du, dd, cpar):
int - the number of candidates found, between 0 - 1
"""
counter = 0
dmin = 1e20
dmin = POS_INF
xmin, xmax, ymin, ymax = cent_x - dl, cent_x + dr, cent_y - du, cent_y + dd

xmin = max(xmin, 0.0)
xmax = min(xmax, cpar.imx)
ymin = max(ymin, 0.0)
ymax = min(ymax, cpar.imy)

p = np.array([-1], dtype=np.int32)

if 0 <= cent_x <= cpar.imx and 0 <= cent_y <= cpar.imy:
# binarized search for start point of candidate search
j0, dj = next_frame.shape[0] // 2, next_frame.shape[0] // 4
j0, dj = num_targets // 2, num_targets // 4
while dj > 1:
j0 += dj if next_frame[j0, 1] < ymin else -dj
dj //= 2

j0 -= 12 if j0 >= 12 else j0 # due to trunc
for j in range(j0, next_frame.shape[0]):
if next_frame[j, 3] == -1: # tnr == TR_UNUSED
if next_frame[j, 1] > ymax:
for j in range(j0, num_targets):
if next_frame[j].tnr == -1: # tnr == TR_UNUSED
if next_frame[j].y > ymax:
break # finish search
if xmin < next_frame[j, 0] < xmax and ymin < next_frame[j, 1] < ymax:
if xmin < next_frame[j].x < xmax and ymin < next_frame[j].y < ymax:
d = np.sqrt(
(cent_x - next_frame[j, 0]) ** 2
+ (cent_y - next_frame[j, 1]) ** 2
(cent_x - next_frame[j].x) ** 2
+ (cent_y - next_frame[j].y) ** 2
)
if d < dmin:
dmin = d
Expand All @@ -461,7 +472,7 @@ def candsearch_in_pix_rest(next_frame, cent_x, cent_y, dl, dr, du, dd, cpar):
if p[0] != -1:
counter += 1

return counter, p
return counter


def searchquader(point, tpar, cpar, cal):
Expand Down
31 changes: 29 additions & 2 deletions tests/test_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from openptv_python.track import (
angle_acc,
candsearch_in_pix,
candsearch_in_pix_rest,
pos3d_in_bounds,
predict,
search_volume_center_moving,
Expand Down Expand Up @@ -176,7 +177,7 @@ def test_candsearch_in_pix(self):
counter = candsearch_in_pix(
test_targets, num_targets, cent_x, cent_y, dl, dr, du, dd, p, test_cpar
)
print(f"p = {p}, counter = {counter}")
# print(f"p = {p}, counter = {counter}")

self.assertEqual(counter, 2)

Expand All @@ -188,9 +189,35 @@ def test_candsearch_in_pix(self):
counter = candsearch_in_pix(
test_targets, num_targets, cent_x, cent_y, dl, dr, du, dd, p, test_cpar
)
print(f"p = {p}, counter = {counter}")
# print(f"p = {p}, counter = {counter}")
self.assertEqual(counter, 4)

test_targets = [
Target(0, 0.0, -0.2, 5, 1, 2, 10, 0),
Target(6, 100.0, 100.0, 10, 8, 1, 20, -1),
Target(3, 102.0, 102.0, 10, 3, 3, 30, -1),
Target(4, 103.0, 103.0, 10, 3, 3, 40, 2),
Target(1, -0.7, 1.2, 10, 3, 3, 50, 5),
Target(7, 1.2, 1.3, 10, 3, 3, 60, 7),
Target(5, 1200, 201.1, 10, 3, 3, 70, 11),
]
num_targets = len(test_targets)

cent_x = cent_y = 98.9
dl = dr = du = dd = 3
p = [-1] * num_cams # Initialize p

counter = candsearch_in_pix_rest(
test_targets, num_targets, cent_x, cent_y, dl, dr, du, dd, p, test_cpar
)

# print(f"p = {p}, counter = {counter}")
self.assertEqual(counter, 1)
self.assertTrue(
isclose(test_targets[p[0]].x, 100.0, rel_tol=1e-9),
f"Expected 100.0 but found {test_targets[p[0]].x}",
)


if __name__ == "__main__":
unittest.main()

0 comments on commit ceb41ca

Please sign in to comment.