File size: 1,300 Bytes
b5af7d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import cv2
import numpy as np
import pytest
from backend.experimental.mesh import fit_mesh,dense_mesh


def test_mesh_recovers_smooth_displacement_on_unseen_points():
    h=w=600
    y,x=np.mgrid[40:560:20,40:560:20]
    points=np.stack([x.ravel(),y.ravel()],-1).astype(np.float32)
    def truth(p):return p+np.stack([4*np.sin(p[:,1]/200),3*np.sin(p[:,0]/180)],-1)
    coef=fit_mesh(points,truth(points),np.eye(3),(h,w))
    mapping,good,_=dense_mesh(coef,np.eye(3),(h,w))
    unseen=np.random.default_rng(7).uniform(80,520,(150,2)).astype(np.float32)
    prediction=cv2.remap(mapping,unseen[:,0,None],unseen[:,1,None],cv2.INTER_LINEAR)[:,0]
    assert np.percentile(np.linalg.norm(prediction-truth(unseen),axis=1),95)<.5
    assert good[80:520,80:520].all()


def test_mesh_marks_excessive_movement_untrusted():
    coef=np.full((9,9,2),50,np.float32)
    _,good,_=dense_mesh(coef,np.eye(3),(200,200))
    assert not good.any()


def test_mesh_rejects_inadequate_constraints():
    with pytest.raises(ValueError):fit_mesh(np.zeros((4,2),np.float32),np.zeros((4,2),np.float32),np.eye(3),(200,200))


def test_mesh_rejects_folded_total_mapping():
    h=np.array([[-1,0,199],[0,1,0],[0,0,1]],dtype=np.float64)
    _,good,_=dense_mesh(np.zeros((9,9,2),np.float32),h,(200,200))
    assert not good.any()