Skip to content

Commit

Permalink
Fix errors when stitching synthetic data
Browse files Browse the repository at this point in the history
Input tiles created via whole-pixel crops of a larger image would generate
negative error metric values due to numerical precision issues in utils.nccw,
ultimately causing a networkx error when building the spanning tree. This
change clamps small negative error metric values up to zero and raises an
exception for large negative values.
  • Loading branch information
jmuhlich committed Jul 6, 2023
1 parent fe5c555 commit 387eff5
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion ashlar/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,18 @@ def nccw(img1, img2, sigma):
correlation = np.abs(np.sum(img1w * img2w))
total_amplitude = np.linalg.norm(img1w) * np.linalg.norm(img2w)
if correlation > 0 and total_amplitude > 0:
error = -np.log(correlation / total_amplitude)
diff = correlation - total_amplitude
if diff <= 0:
error = -np.log(correlation / total_amplitude)
elif diff < 1e-5:
# This situation can occur due to numerical precision issues when
# img1 and img2 are very nearly or exactly identical. If the
# difference is small enough, let it slide.
error = 0
else:
raise RuntimeError(
f"correlation > total_amplitude (diff={diff})"
)
else:
error = np.inf
return error
Expand Down

0 comments on commit 387eff5

Please sign in to comment.