Skip to content

Commit 387eff5

Browse files
committed
Fix errors when stitching synthetic data
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.
1 parent fe5c555 commit 387eff5

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

ashlar/utils.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,18 @@ def nccw(img1, img2, sigma):
7070
correlation = np.abs(np.sum(img1w * img2w))
7171
total_amplitude = np.linalg.norm(img1w) * np.linalg.norm(img2w)
7272
if correlation > 0 and total_amplitude > 0:
73-
error = -np.log(correlation / total_amplitude)
73+
diff = correlation - total_amplitude
74+
if diff <= 0:
75+
error = -np.log(correlation / total_amplitude)
76+
elif diff < 1e-5:
77+
# This situation can occur due to numerical precision issues when
78+
# img1 and img2 are very nearly or exactly identical. If the
79+
# difference is small enough, let it slide.
80+
error = 0
81+
else:
82+
raise RuntimeError(
83+
f"correlation > total_amplitude (diff={diff})"
84+
)
7485
else:
7586
error = np.inf
7687
return error

0 commit comments

Comments
 (0)