Exemple #1
0
def test_no_motion_3d():
    rnd = np.random.default_rng(0)
    img = rnd.normal(size=(64, 64, 64))

    flow = optical_flow_ilk(img, img)

    assert np.all(flow == 0)
Exemple #2
0
def test_no_motion_3d():
    rnd = np.random.RandomState(0)
    img = rnd.normal(size=(128, 128, 128))

    flow = optical_flow_ilk(img, img)

    assert np.all(flow == 0)
Exemple #3
0
def test_optical_flow_dtype():
    # Generate synthetic data
    rnd = np.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_ilk(image0, image1, dtype='float64')

    assert flow_f64.dtype == 'float64'

    # Estimate the flow at single precision
    flow_f32 = optical_flow_ilk(image0, image1, dtype='float32')

    assert flow_f32.dtype == 'float32'

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

    assert abs(flow_f64 - flow_f32).mean() < 1e-3
Exemple #4
0
def test_2d_motion(gaussian, prefilter):
    # Generate synthetic data
    rnd = np.random.RandomState(0)
    image0 = rnd.normal(size=(256, 256))
    gt_flow, image1 = _sin_flow_gen(image0)
    # Estimate the flow
    flow = optical_flow_ilk(image0, image1,
                            gaussian=gaussian, prefilter=prefilter)
    # Assert that the average absolute error is less then half a pixel
    assert abs(flow - gt_flow).mean() < 0.5
Exemple #5
0
def test_3d_motion(gaussian, prefilter):
    # Generate synthetic data
    rnd = np.random.default_rng(123)
    image0 = rnd.normal(size=(50, 55, 60))
    gt_flow, image1 = _sin_flow_gen(image0, npics=3)
    # Estimate the flow
    flow = optical_flow_ilk(image0,
                            image1,
                            radius=5,
                            gaussian=gaussian,
                            prefilter=prefilter)

    # Assert that the average absolute error is less then half a pixel
    assert abs(flow - gt_flow).mean() < 0.5
Exemple #6
0
def test_2d_motion(dtype, gaussian, prefilter):
    # Generate synthetic data
    rnd = np.random.default_rng(0)
    image0 = rnd.normal(size=(256, 256))
    gt_flow, image1 = _sin_flow_gen(image0)
    image1 = image1.astype(dtype, copy=False)
    float_dtype = _supported_float_type(dtype)
    # Estimate the flow
    flow = optical_flow_ilk(image0,
                            image1,
                            gaussian=gaussian,
                            prefilter=prefilter,
                            dtype=float_dtype)
    assert flow.dtype == _supported_float_type(dtype)
    # Assert that the average absolute error is less then half a pixel
    assert abs(flow - gt_flow).mean() < 0.5

    if dtype != float_dtype:
        with pytest.raises(ValueError):
            optical_flow_ilk(image0,
                             image1,
                             gaussian=gaussian,
                             prefilter=prefilter,
                             dtype=dtype)
Exemple #7
0
def test_wrong_dtype():
    rnd = np.random.RandomState(0)
    img = rnd.normal(size=(256, 256))
    with testing.raises(ValueError):
        u, v = optical_flow_ilk(img, img, dtype='int')
Exemple #8
0
def test_incompatible_shapes():
    rnd = np.random.RandomState(0)
    I0 = rnd.normal(size=(256, 256))
    I1 = rnd.normal(size=(255, 256))
    with testing.raises(ValueError):
        u, v = optical_flow_ilk(I0, I1)
 def time_ilk(self, dtype):
     registration.optical_flow_ilk(self.I0, self.I1, dtype=dtype)
Exemple #10
0
def test_incompatible_shapes():
    rnd = np.random.default_rng(0)
    I0 = rnd.normal(size=(256, 256))
    I1 = rnd.normal(size=(255, 256))
    with pytest.raises(ValueError):
        u, v = optical_flow_ilk(I0, I1)
Exemple #11
0
def test_wrong_dtype():
    rnd = np.random.default_rng(0)
    img = rnd.normal(size=(256, 256))
    with pytest.raises(ValueError):
        u, v = optical_flow_ilk(img, img, dtype='int')
Exemple #12
0
fig.tight_layout()

###################################################################
# The estimated vector field *(u, v)* can also be displayed with a
# quiver plot.
#
# In the following example, Iterative Lukas-Kanade algorithm (iLK) is
# applied to images of particles in the context of particle image
# velocimetry (PIV). The sequence is the Case B from the
# `PIV challenge 2001 <http://www.pivchallenge.org/>`_

image0 = imageio.imread("http://www.pivchallenge.org/pub/B/B001_1.tif")
image1 = imageio.imread("http://www.pivchallenge.org/pub/B/B001_2.tif")

# --- Compute the optical flow
v, u = optical_flow_ilk(image0, image1, radius=15)

# --- Compute flow magnitude
norm = np.sqrt(u**2 + v**2)

# --- Display
fig, (ax0, ax1) = plt.subplots(1, 2, figsize=(8, 4))

# --- Sequence image sample

ax0.imshow(image0, cmap='gray')
ax0.set_title("Sequence image sample")
ax0.set_axis_off()

# --- Quiver plot arguments
 def time_ilk(self):
     registration.optical_flow_ilk(self.I0, self.I1)
sr = StackReg(StackReg.RIGID_BODY)
sr.register(ref, target_img)
target_reg = sr.transform(z_reg[t][ref_z[t], ...])

plt.figure()
io.imshow(ref)
plt.figure()
io.imshow(target_reg - target_reg.min())
# for z in range(z_reg[t].shape[0]):
#     z_reg[t][z,...] = sr.transform( z_reg[t][z,...])

# io.imsave(f'/Users/xies/Desktop/z_reg_t{t}.tif',z_reg[t].astype(np.int16))

#%% Try Optic flow

from skimage import registration
from skimage import transform

v, u = registration.optical_flow_ilk(ref_img, moving_img)
X, Y = ref_img.shape
row_coords, col_coords = np.meshgrid(np.arange(X), np.arange(Y), indexing='ij')

moving_reg = transform.warp(moving_img,
                            np.array([row_coords + v, col_coords + u]),
                            mode='edge')

plt.figure()
io.imshow(ref_img[25, ...])
plt.figure()
io.imshow(moving_reg[25, ...])