Files
mediaproc/tests/detect/test_edge_sensitivity.py
2026-03-26 22:22:35 -03:00

88 lines
2.9 KiB
Python

"""Parameter sensitivity tests for edge detection.
Verifies that adjusting parameters in expected directions produces
expected changes in detection counts. Uses synthetic frames with
known geometry.
"""
import importlib.util
from pathlib import Path
import cv2
import numpy as np
import pytest
# Load edges module directly
_spec = importlib.util.spec_from_file_location(
"cv_edges", Path("gpu/models/cv/edges.py"),
)
_edges_mod = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(_edges_mod)
detect_edges = _edges_mod.detect_edges
detect_edges_debug = _edges_mod.detect_edges_debug
def _frame_with_lines(n_pairs: int = 3, line_thickness: int = 2) -> np.ndarray:
"""Create a frame with multiple horizontal line pairs."""
image = np.zeros((1080, 1920, 3), dtype=np.uint8)
y_start = 300
for i in range(n_pairs):
y_top = y_start + i * 120
y_bot = y_top + 40 + i * 10 # varying gap
cv2.line(image, (100, y_top), (1800, y_top), (255, 255, 255), line_thickness)
cv2.line(image, (100, y_bot), (1800, y_bot), (255, 255, 255), line_thickness)
return image
def test_canny_low_sensitivity():
"""Lowering canny_low should find same or more horizontal lines."""
image = _frame_with_lines()
high_threshold = detect_edges_debug(image, canny_low=100, canny_high=200)
low_threshold = detect_edges_debug(image, canny_low=30, canny_high=200)
assert low_threshold["horizontal_count"] >= high_threshold["horizontal_count"]
def test_hough_threshold_sensitivity():
"""Lowering hough_threshold should find same or more lines."""
image = _frame_with_lines()
strict = detect_edges_debug(image, hough_threshold=150)
lenient = detect_edges_debug(image, hough_threshold=40)
assert lenient["horizontal_count"] >= strict["horizontal_count"]
def test_pair_distance_range():
"""Widening pair distance range should find same or more pairs."""
image = _frame_with_lines()
narrow = detect_edges_debug(image, pair_min_distance=30, pair_max_distance=50)
wide = detect_edges_debug(image, pair_min_distance=10, pair_max_distance=200)
assert wide["pair_count"] >= narrow["pair_count"]
def test_hough_min_length_sensitivity():
"""Shorter min_length should find same or more lines."""
image = _frame_with_lines()
long_min = detect_edges_debug(image, hough_min_length=500)
short_min = detect_edges_debug(image, hough_min_length=50)
assert short_min["horizontal_count"] >= long_min["horizontal_count"]
def test_blank_frame_no_false_positives():
"""All parameter combinations on blank frame should produce zero regions."""
image = np.zeros((720, 1280, 3), dtype=np.uint8)
# Very lenient parameters
results = detect_edges(
image, canny_low=10, canny_high=50, hough_threshold=10,
hough_min_length=20, pair_min_distance=5, pair_max_distance=500,
)
assert results == []