Skip to content

Instantly share code, notes, and snippets.

@ianhi
Created October 12, 2022 19:10
Show Gist options
  • Save ianhi/a931d40a6f4f2dd5adf46c3e841085b0 to your computer and use it in GitHub Desktop.
Save ianhi/a931d40a6f4f2dd5adf46c3e841085b0 to your computer and use it in GitHub Desktop.
Image Comparison Using matplotlib
import matplotlib.pyplot as plt
from mpl_draggable_line import DraggableVLine
def compare_images(img1, img2, ax=None):
img1 = np.asanyarray(img1)
img2 = np.asanyarray(img2)
if not np.all(img1.shape == img2.shape):
raise ValueError("Image shapes must match!")
if ax is None:
ax = plt.gca()
x = img1.shape[0]//2
im = ax.imshow(np.hstack((img1[:,:x], img2[:,x:])))
dl = DraggableVLine(ax, x)
def update(x):
im.set_data(np.hstack((img1[:,:int(np.rint(x))], img2[:,int(np.rint(x)):])))
dl.on_line_changed(update)
return dl
# Generate Fake data
import numpy as np
rng = np.random.default_rng()
imgs = rng.random((2, 100,100))
img1 = imgs[0]
img2 = imgs[1]*2
# make comparison plot
fig, ax = plt.subplots()
comparer_line = compare_images(img1, img2)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment