Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider layer transform metadata when applying transform #85

Merged
merged 3 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/affinder/_tests/test_affinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import napari
import pytest
from scipy import ndimage as ndi
from pathlib import Path


layer0_pts = np.array([[140.38371886,
Expand All @@ -21,8 +22,9 @@
# get reference and moving layer types
im0 = data.camera()
im1 = transform.rotate(im0[100:, 32:496], 60)
labels0 = zarr.open('./src/affinder/_tests/labels0.zarr', mode='r')
labels1 = zarr.open('./src/affinder/_tests/labels1.zarr', mode='r')
this_dir = Path(__file__).parent.absolute()
labels0 = zarr.open(this_dir / 'labels0.zarr', mode='r')
labels1 = zarr.open(this_dir / 'labels1.zarr', mode='r')


def make_vector_border(layer_pts):
Expand Down Expand Up @@ -126,6 +128,20 @@ def test_apply_affine():
np.testing.assert_allclose(res_layer[0], ref_im)


def test_apply_affine_with_scale():
ref_im = np.random.random((5, 5))
mov_im = ndi.zoom(ref_im, 2, order=0)

ref_layer = napari.layers.Image(ref_im, scale=(0.2, 0.2))
mov_layer = napari.layers.Image(mov_im, scale=(0.4, 0.4))
mov_layer.affine = np.array([[0.25, 0, 0], [0, 0.25, 0], [0, 0, 1]])

widget = apply_affine()
res_layer = widget(ref_layer, mov_layer)

np.testing.assert_allclose(res_layer[0], ref_im)


def test_apply_affine_nonimage():
ref_im = np.random.random((5, 5))
mov_pts = np.random.random((5, 2))
Expand Down
19 changes: 18 additions & 1 deletion src/affinder/apply_tf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from magicgui import magic_factory
from skimage import transform
from napari.utils.transforms import CompositeAffine
import numpy as np


Expand Down Expand Up @@ -62,8 +63,24 @@ def apply_affine(
'Only image transforms supported at this point.'
)

reference_meta = CompositeAffine(
scale=reference_layer.scale,
translate=reference_layer.translate,
rotate=reference_layer.rotate,
shear=reference_layer.shear,
)
moving_meta = CompositeAffine(
scale=moving_layer.scale,
translate=moving_layer.translate,
rotate=moving_layer.rotate,
shear=moving_layer.shear,
)
# Find the transformation relative to the reference image
affine = np.linalg.inv(reference_layer.affine) @ moving_layer.affine
affine = (
np.linalg.inv(reference_meta)
@ np.linalg.inv(reference_layer.affine) @ moving_layer.affine
@ moving_meta
)

# Apply the transformation
transformed = _apply_affine_image(
Expand Down
Loading