Files
supervision/tests/cv2/test_transform.py
T
3d669f1ab4 feat(cv2): add image fallback backend (#2431)
- Organize facade constants and implementations into thematic private modules.
- Add NumPy/Pillow/SciPy fallbacks with OpenCV parity coverage.
- Mirror package modules in cv2 tests and verify blocked imports.
- Split compound operation tests into isolated cases.
- Parameterize color parity and fallback bindings for targeted failures.
- Inline color conversion cases at their only use site.
- Inline fallback binding cases while preserving reusable manifests.

---------

Co-authored-by: Codex <codex@openai.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
2026-07-15 21:55:08 +02:00

75 lines
2.3 KiB
Python

"""Tests for private transform and filter fallbacks."""
from __future__ import annotations
import importlib
import numpy as np
import pytest
from supervision._cv2._transform import (
_blur,
_distance_transform,
_get_rotation_matrix_2d,
_warp_affine,
)
from supervision._cv2.constants import _DIST_L2
try:
cv2 = importlib.import_module("cv2")
except (ImportError, OSError):
pytest.skip(
"OpenCV is required as the reference implementation for this test module",
allow_module_level=True,
)
def test_fallback_identity_affine_matches_opencv() -> None:
"""Match OpenCV for an identity affine transform."""
source = np.arange(25, dtype=np.uint8).reshape(5, 5)
matrix = _get_rotation_matrix_2d((2, 2), 0, 1)
actual = _warp_affine(source, matrix, (5, 5))
expected = cv2.warpAffine(source, matrix, (5, 5))
np.testing.assert_array_equal(actual, expected)
def test_fallback_rotated_affine_matches_opencv() -> None:
"""Match OpenCV for a rotated affine transform within its pixel budget."""
source = np.arange(25, dtype=np.uint8).reshape(5, 5)
rotated_matrix = _get_rotation_matrix_2d((2, 2), 17, 1)
rotated = _warp_affine(source, rotated_matrix, (5, 5))
expected_rotated = cv2.warpAffine(source, rotated_matrix, (5, 5))
np.testing.assert_allclose(rotated, expected_rotated, atol=3, rtol=0)
def test_fallback_blur_preserves_shape_and_dtype() -> None:
"""Preserve source shape and dtype during blurring."""
source = np.arange(25, dtype=np.uint8).reshape(5, 5)
blurred = _blur(source, (3, 3))
assert blurred.shape == source.shape
assert blurred.dtype == source.dtype
def test_fallback_distance_transform_preserves_shape_and_dtype() -> None:
"""Preserve source shape and expose float32 distance values."""
source = np.ones((7, 7), dtype=np.uint8)
source[3, 3] = 0
actual = _distance_transform(source, _DIST_L2, 3)
assert actual.shape == source.shape
assert actual.dtype == np.float32
def test_fallback_distance_transform_preserves_distance_order() -> None:
"""Preserve zero locations and monotonic distances for the L2 transform."""
source = np.ones((7, 7), dtype=np.uint8)
source[3, 3] = 0
actual = _distance_transform(source, _DIST_L2, 3)
assert actual[3, 3] == 0
assert actual[3, 2] < actual[3, 1] < actual[3, 0]