Skip to content

Commit

Permalink
Replace correct_drift algorithm with scharr alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
gb119 committed Nov 17, 2021
1 parent 2ee84d8 commit 093b15b
Show file tree
Hide file tree
Showing 3 changed files with 1,180 additions and 1,195 deletions.
31 changes: 8 additions & 23 deletions Stoner/Image/imagefuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ def convert(image, dtype, force_copy=False, uniform=False, normalise=True):
return image.astype(dtype)


def correct_drift(im, ref, threshold=0.005, upsample_factor=50, box=False, do_shift=True):
def correct_drift(im, ref, **kargs):
"""Align images to correct for image drift.
Args:
Expand All @@ -476,30 +476,15 @@ def correct_drift(im, ref, threshold=0.005, upsample_factor=50, box=False, do_sh
Adds 'drift_shift' to the metadata as the (x,y) vector that translated the
image back to it's origin.
"""
if isinstance(box, bool) and not box:
im = im.clone.crop(box=box)
cls = type(im)

refed = cls(ref, get_metadata=False)
refed = refed.crop(box=box)
refed = refed.filter_image(sigma=1)
refed = refed > refed.threshold_otsu()
refed = refed.corner_fast(threshold=threshold)

imed = im.clone
imed = imed.filter_image(sigma=1)
imed = imed > imed.threshold_otsu()
imed = imed.corner_fast(threshold=threshold)
do_shift = kargs.pop("do_shift", True)
kargs["scale"] = kargs.pop("upscale", kargs.get("scale", 5.0))
kargs.setdefault("meothd", "scharr")

refed = refed.view(np.ndarray)
imed = imed.view(np.ndarray)

shift = registration.phase_cross_correlation(
refed, imed, upsample_factor=upsample_factor, reference_mask=~np.isnan(refered), moving_mask=~np.isnan(imed)
)[0]
ret = align(im, ref, **kargs)
if do_shift:
im = im.translate(translation=(-shift[1], -shift[0])) # x,y
im.metadata["correct_drift"] = (-shift[1], -shift[0])
im = ret
im["correct_drift"] = -np.array(ret["tvec"])[::-1]

return im


Expand Down
30 changes: 15 additions & 15 deletions tests/Stoner/mixedmetatest.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
TDI Format 1.5 1 2 3 4
Stoner.class{String}=Data 0 1 2 3
t0{Boolean}=True 4 5 6 7
t1{I32}=1 8 9 10 11
t10{List}=[1, (1, 2), 'abc']
t11{List}=[[[1]]]
t12{Void}=None
t2{Double Float}=0.2
t3{Cluster (I32,String)}={'a': 1, 'b': 'abc'}
t4{Cluster (I32,I32)}=(1, 2)
t5{1D Array (Invalid Type)}=array([0, 1, 2])
t6{List}=[1, 2, 3]
t7{String}=abc
t8{String}=\\abc\cde
t9{Double Float}=1e-20
TDI Format 1.5 1 2 3 4
Stoner.class{String}=Data 0 1 2 3
t0{Boolean}=True 4 5 6 7
t1{I32}=1 8 9 10 11
t10{List}=[1, (1, 2), 'abc']
t11{List}=[[[1]]]
t12{Void}=None
t2{Double Float}=0.2
t3{Cluster (I32,String)}={'a': 1, 'b': 'abc'}
t4{Cluster (I32,I32)}=(1, 2)
t5{1D Array (Invalid Type)}=array([0, 1, 2])
t6{List}=[1, 2, 3]
t7{String}=abc
t8{String}=\\abc\cde
t9{Double Float}=1e-20

0 comments on commit 093b15b

Please sign in to comment.