CRICKET / code /pitch.py
d9993r's picture
Add files using upload-large-folder tool
14c58d8 verified
Raw History Blame Contribute Delete
3.94 kB
"""Single-camera pitch calibration from four clicked crease points (section 8.2).
Click order on a frame, left/right as seen in the image:
1 bowler-end popping crease x left return crease
2 bowler-end popping crease x right return crease
3 batter-end popping crease x left return crease
4 batter-end popping crease x right return crease
World frame: x = metres to image-right of the middle-stump line, y = metres from the
bowler-end popping crease toward the batter. Valid on the pitch surface only (z = 0),
so it measures where the ball BOUNCES, not where it is in the air."""
import cv2
import numpy as np
POPPING_GAP_M = 17.68 # popping crease to popping crease (20.12 - 2 x 1.22)
RETURN_HALF_M = 1.32 # return crease to middle-stump line
BATTER_STUMPS_Y_M = 17.68 + 1.22
WORLD = np.float32([[-RETURN_HALF_M, 0], [RETURN_HALF_M, 0],
[-RETURN_HALF_M, POPPING_GAP_M], [RETURN_HALF_M, POPPING_GAP_M]])
NAMED_GROUND = {"bowler_left": (-RETURN_HALF_M, 0.0), "bowler_right": (RETURN_HALF_M, 0.0),
"batter_left": (-RETURN_HALF_M, POPPING_GAP_M), "batter_right": (RETURN_HALF_M, POPPING_GAP_M),
"bowler_bc_left": (-RETURN_HALF_M, -1.22), "bowler_bc_right": (RETURN_HALF_M, -1.22),
"batter_bc_left": (-RETURN_HALF_M, BATTER_STUMPS_Y_M), "batter_bc_right": (RETURN_HALF_M, BATTER_STUMPS_Y_M),
"bowler_stump_base": (0.0, -1.22), "batter_stump_base": (0.0, BATTER_STUMPS_Y_M)}
def ground_points(cal):
"""{name: [x, y]} for clicked points that lie on the ground."""
pts = dict(zip(["bowler_left", "bowler_right", "batter_left", "batter_right"], cal.get("points_px") or []))
pts.update({k: v for k, v in (cal.get("named_px") or {}).items() if v})
pts.update({k: v for k, v in (cal.get("extra_px") or {}).items() if v})
return {k: v for k, v in pts.items() if v and k in NAMED_GROUND}
def homography(points_px):
"""Image -> pitch plane. Accepts the classic 4 corner list, or a calibration dict with
any 4+ named ground points (needed for side-on cameras that see only one end)."""
if isinstance(points_px, dict):
g = ground_points(points_px)
if len(g) < 4:
raise ValueError("need at least 4 ground points")
H, _ = cv2.findHomography(np.float32(list(g.values())), np.float32([NAMED_GROUND[k] for k in g]))
return H
return cv2.getPerspectiveTransform(np.float32(points_px), WORLD)
def to_pitch(H, xy):
p = cv2.perspectiveTransform(np.float32([[xy]]), np.asarray(H, np.float64))[0, 0]
return float(p[0]), float(p[1])
def bucket(value, table):
for name, (lo, hi) in table.items():
if lo <= value < hi:
return name
return None
def derive_line_length(bounce_px, calib, handedness, onto):
"""Measured bounce in metres -> length (always) and line (needs batter handedness)."""
H = homography(calib)
x, y = to_pitch(H, bounce_px)
from_stumps = BATTER_STUMPS_Y_M - y
out = {"bounce_m": {"x_image_right_m": round(x, 3), "y_from_bowler_popping_m": round(y, 3),
"length_from_batter_stumps_m": round(from_stumps, 3)},
"calibration_id": calib["calibration_id"], "camera_end": calib.get("camera_end", "bowler"),
"valid": -1.0 <= y <= POPPING_GAP_M + 2.0 and abs(x) < 4.0}
th = onto["thresholds"]
out["actual_length"] = bucket(from_stumps, th["length_from_batter_stumps_m"]) if out["valid"] else None
out["actual_line"] = None
if out["valid"] and handedness in ("right", "left"):
# Seen from behind the bowler, a right-hander's off side is image-left.
off_is_left = (handedness == "right") == (out["camera_end"] == "bowler")
off = -x if off_is_left else x
out["bounce_m"]["offside_from_middle_stump_m"] = round(off, 3)
out["actual_line"] = bucket(off, th["line_offside_from_middle_stump_m"])
return out