44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
import numpy as np
|
|
import pytest
|
|
|
|
from bookmark.image_processing import crop_to_book
|
|
|
|
|
|
def make_reference(size=100, color=200):
|
|
return np.full((size, size, 3), color, dtype=np.uint8)
|
|
|
|
|
|
def make_book_frame(size=100, ref_color=200, book_color=50, box=(30, 30, 20, 20)):
|
|
frame = np.full((size, size, 3), ref_color, dtype=np.uint8)
|
|
x, y, w, h = box
|
|
frame[y:y + h, x:x + w] = book_color
|
|
return frame
|
|
|
|
|
|
def test_crop_raises_when_no_difference_from_reference():
|
|
reference = make_reference()
|
|
with pytest.raises(ValueError):
|
|
crop_to_book(reference.copy(), reference)
|
|
|
|
|
|
def test_crop_returns_region_around_book_with_margin():
|
|
reference = make_reference()
|
|
frame = make_book_frame(box=(30, 30, 20, 20))
|
|
|
|
cropped = crop_to_book(frame, reference, margin=10)
|
|
|
|
# book occupies x:[30,50) y:[30,50), with margin=10 expected crop is
|
|
# roughly x:[20,60) y:[20,60) -> 40x40
|
|
assert cropped.shape[0] == pytest.approx(40, abs=2)
|
|
assert cropped.shape[1] == pytest.approx(40, abs=2)
|
|
|
|
|
|
def test_crop_clamps_margin_to_frame_bounds():
|
|
reference = make_reference()
|
|
frame = make_book_frame(box=(0, 0, 20, 20))
|
|
|
|
cropped = crop_to_book(frame, reference, margin=10)
|
|
|
|
# book touches top-left corner, margin should clamp instead of going negative
|
|
assert cropped.shape[0] <= 30
|
|
assert cropped.shape[1] <= 30
|