test_roi_padding.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import numpy as np
  2. import pytest
  3. from module.atom import image as image_module
  4. from module.atom.image import RuleImage
  5. @pytest.mark.parametrize('method', ['match', 'match_multi_scale', 'match_all', 'match_all_any'])
  6. @pytest.mark.parametrize('position', [(0, 0), (16, 17)])
  7. @pytest.mark.parametrize('pad', [0, 2])
  8. def test_matching_padding_preserves_absolute_coordinates(monkeypatch, method, position, pad):
  9. monkeypatch.setattr(image_module, 'ROI_MATCH_PAD', pad)
  10. template = np.random.default_rng(19).integers(0, 256, (10, 10, 3), dtype=np.uint8)
  11. screen = np.zeros((40, 40, 3), dtype=np.uint8)
  12. x, y = position
  13. screen[y:y + 10, x:x + 10] = template
  14. # Desktop padding compensates for a one-pixel displacement; emulator uses exact ROI.
  15. roi = (x + bool(pad), y + bool(pad), 10, 10)
  16. rule = RuleImage(roi, roi, 'Template matching', 0.99, 'unused.png')
  17. rule._image = template
  18. if method == 'match_multi_scale':
  19. assert rule.match_multi_scale(screen, scales=[1.0])
  20. assert tuple(rule.roi_front[:2]) == position
  21. elif method == 'match':
  22. assert rule.match(screen)
  23. assert tuple(rule.roi_front[:2]) == position
  24. else:
  25. results = getattr(rule, method)(screen)
  26. assert any(tuple(result[1:3]) == position for result in results)
  27. def test_emulator_without_padding_does_not_accept_shifted_template(monkeypatch):
  28. monkeypatch.setattr(image_module, 'ROI_MATCH_PAD', 0)
  29. pixels = np.random.default_rng(19).integers(0, 256, (10, 10, 3), dtype=np.uint8)
  30. screen = np.zeros((30, 30, 3), dtype=np.uint8)
  31. screen[10:20, 10:20] = pixels
  32. rule = RuleImage((11, 11, 10, 10), (11, 11, 10, 10), 'Template matching', 0.99, 'unused.png')
  33. rule._image = pixels
  34. assert not rule.match(screen)