<jupyter_start><jupyter_text>Plot correspondences in registration  Imports and magics<jupyter_code>%load_ext autoreload
%autoreload 2
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import ipywidgets as ipyw
import matplotlib.animation as animation
from mpl_toolkits.axes_grid1 import make_axes_locatable
from scipy.io import loadmat
from IPython.display import HTML

from otimage import imagerep, io
from otimage.utils import plot_maxproj
from otimage.imagereg import ot_registration, gw_registration<jupyter_output><empty_output><jupyter_text>Load Zimmer data<jupyter_code># Frame index
t1 = 15
t2 = 45

# Load two successive frames from dataset
img_path = '/home/mn2822/Desktop/WormOT/data/zimmer/raw/mCherry_v00065-00115.hdf5'
with io.ZimmerReader(img_path) as reader:
    img_1 = reader.get_frame(t1)
    img_2 = reader.get_frame(t2)

# Load MP components
mp_path = '/home/mn2822/Desktop/WormOT/data/zimmer/mp_components/mp_0000_0050.mat'
with io.MPReader(mp_path) as reader:
    mp_1 = reader.get_frame(t1)
    mp_2 = reader.get_frame(t2)

# Reconstruct images from MPs
rec_1 = imagerep.reconstruct_mp_image(mp_1)
rec_2 = imagerep.reconstruct_mp_image(mp_2)
  
plt.figure(figsize=(10, 10))

plt.subplot(221)
plot_maxproj(img_1)
plt.title(f'frame: {t1}')
plt.axis('off')

plt.subplot(222)
plot_maxproj(img_2)
plt.title(f'frame: {t2}')
plt.axis('off');

plt.subplot(223)
plot_maxproj(rec_1)
plt.title('MP recon')
plt.axis('off')

plt.subplot(224)
plot_maxproj(rec_2)
plt.title('MP recon')
plt.axis('off');<jupyter_output><empty_output><jupyter_text>Compute registration for images<jupyter_code>degree = 2
n_iter = 50

model, debug = gw_registration(mp_2, mp_1, degree=degree, n_iter=n_iter)

print('beta (est):')
print(model.beta)<jupyter_output>beta (est):
[[-6.37500970e+03  1.46780019e+01  2.70073771e+01 -2.37063720e+01
  -9.45765201e-03 -2.64565759e-02  4.45074674e-02 -2.71933612e-02
   5.97393074e-02 -4.43557853e-01]
 [-2.95479814e+03  7.75364064e+00  1.42227020e+01 -4.11210531e+01
  -4.99815740e-03 -1.78396029e-02  5.07123304e-02 -1.38888059e-02
   8.61879211e-02 -1.79823225e-01]
 [ 4.57879308e+01  2.35539509e-01 -2.67878050e-01 -2.35965444e+00
  -2.35927152e-04 -3.34157564e-04  1.09924379e-03  4.24452562e-04
   4.16954820e-03  2.10195408e-02]]<jupyter_text>Compute pullback with identified MP component<jupyter_code># Transform MP points for second image using mapping
pb_pts = model.predict(mp_2.pts).astype(int)

# Reconstruct image from transformed points
mp_pb = imagerep.ImageMP(pb_pts, mp_2.wts, mp_2.cov, mp_2.img_shape)
rec_pb = imagerep.reconstruct_mp_image(mp_pb)<jupyter_output><empty_output><jupyter_text>Plot pullback and MP components<jupyter_code>plt.figure(figsize=(15, 15))

plt.subplot(131)
plot_maxproj(rec_1)
plt.title(f'frame: {t1}')
        
plt.subplot(132)
plot_maxproj(rec_2)
plt.title(f'frame: {t2}')
        
plt.subplot(133)
plot_maxproj(rec_pb)
plt.title(f'frame: {t2} (pullback)');
def plot_mp(idx):
    
    c_pt = mp_2.pts[idx]
    c_pb = mp_pb.pts[idx]
   
    fig, (ax_1, ax_2, ax_3) = plt.subplots(1, 3, figsize=(15, 15))

    plot_maxproj(rec_1, ax_1)
    ax_1.set_title(f'frame: {t1}')
        
    plot_maxproj(rec_2, ax_2)
    ax_2.plot(c_pt[0], c_pt[1], color='red', marker='*')
    ax_2.set_title(f'frame: {t2}')
        
    plot_maxproj(rec_pb, ax_3)
    ax_3.plot(c_pb[0], c_pb[1], color='red', marker='*')
    ax_3.set_title(f'frame: {t2} (pullback)');


ipyw.interact(
    plot_mp, 
    idx=ipyw.IntSlider(
        min=0, max=mp_1.pts.shape[0], step=1, 
        continuous_update=False, description='MP:'
    )
);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1354998622968879, 'fraction_numerical': 0.12586064445056458, 'mean_word_length': 5.023217247097844, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '144920', 'license': 'MIT', 'n_tokens_mistral': 1844, 'n_tokens_neox': 1576, 'n_words': 256, 'path': 'python/notebooks/image_registration/two_frame/10_plot_correspondence.ipynb', 'repo_name': 'evarol/ot_tracking', 'size': '3631'}