Example #1
0
def test_optical_flow_dtype():
    # Generate synthetic data
    rnd = cp.random.RandomState(0)
    image0 = rnd.normal(size=(256, 256))
    gt_flow, image1 = _sin_flow_gen(image0)
    # Estimate the flow at double precision
    flow_f64 = optical_flow_tvl1(image0,
                                 image1,
                                 attachment=5,
                                 dtype=np.float64)

    assert flow_f64.dtype == np.float64

    # Estimate the flow at single precision
    flow_f32 = optical_flow_tvl1(image0,
                                 image1,
                                 attachment=5,
                                 dtype=np.float32)

    assert flow_f32.dtype == np.float32

    # Assert that floating point precision does not affect the quality
    # of the estimated flow

    assert cp.abs(flow_f64 - flow_f32).mean() < 1e-3
Example #2
0
def test_no_motion_3d():
    rnd = np.random.RandomState(0)
    img = cp.array(rnd.normal(size=(64, 64, 64)))

    flow = optical_flow_tvl1(img, img)

    assert cp.all(flow == 0)
Example #3
0
def test_no_motion_2d():
    rnd = cp.random.RandomState(0)
    img = rnd.normal(size=(256, 256))

    flow = optical_flow_tvl1(img, img)

    assert cp.all(flow == 0)
Example #4
0
def test_2d_motion(dtype):
    # Generate synthetic data
    rnd = cp.random.RandomState(0)
    image0 = rnd.normal(size=(256, 256)).astype(dtype)
    gt_flow, image1 = _sin_flow_gen(image0)
    image1 = image1.astype(dtype, copy=False)
    # Estimate the flow
    flow = optical_flow_tvl1(image0, image1, attachment=5, dtype=dtype)
    assert flow.dtype == dtype
    # Assert that the average absolute error is less then half a pixel
    assert abs(flow - gt_flow).mean() < 0.5
Example #5
0
def test_3d_motion(dtype):
    # Generate synthetic data
    rnd = np.random.RandomState(0)
    image0 = cp.array(rnd.normal(size=(100, 100, 100))).astype(dtype)
    gt_flow, image1 = _sin_flow_gen(image0)
    image1 = image1.astype(dtype, copy=False)
    # Estimate the flow
    # TODO: note: when changing _sin_flow_gen to use a float deformation field
    #             had to increase attachment here from 5 to pass the tolerance.
    flow = optical_flow_tvl1(image0, image1, attachment=10, dtype=dtype)
    assert flow.dtype == dtype
    # Assert that the average absolute error is less then half a pixel
    assert abs(flow - gt_flow).mean() < 0.5
Example #6
0
def test_wrong_dtype():
    rnd = cp.random.RandomState(0)
    img = rnd.normal(size=(256, 256))
    with pytest.raises(ValueError):
        u, v = optical_flow_tvl1(img, img, dtype=np.int64)
Example #7
0
def test_incompatible_shapes():
    rnd = cp.random.RandomState(0)
    I0 = rnd.normal(size=(256, 256))
    I1 = rnd.normal(size=(128, 256))
    with pytest.raises(ValueError):
        u, v = optical_flow_tvl1(I0, I1)