<jupyter_start><jupyter_text>Straightened video created using registration  Imports and magics<jupyter_code>%load_ext autoreload
%autoreload 2
%matplotlib inline
from functools import partial

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from IPython.display import HTML

from otimage import imagerep, imagereg, 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># Frames to load
t_start = 0
n_frames = 5
t_vals = range(t_start, t_start + n_frames)

# Load frames from dataset
img_path = '/home/mn2822/Desktop/WormOT/data/zimmer/raw/mCherry_v00065-00115.hdf5'
with io.ZimmerReader(img_path) as reader:
    img = [reader.get_frame(t) for t in t_vals]

# Load MP components
mp_path = '/home/mn2822/Desktop/WormOT/data/zimmer/mp_components/mp_0000_0050.mat'
with io.MPReader(mp_path) as reader:
    mps = [reader.get_frame(t) for t in t_vals]

# Reconstruct images from MPs
mp_img = [imagerep.reconstruct_mp_image(mp) for mp in mps]

# Indices to plot
k1 = 0
k2 = -1
  
plt.figure(figsize=(10, 10))

plt.subplot(221)
plot_maxproj(img[k1])
plt.title(f'frame: {t_vals[k1]}')
plt.axis('off')

plt.subplot(222)
plot_maxproj(img[k2])
plt.title(f'frame: {t_vals[k2]}')
plt.axis('off');

plt.subplot(223)
plot_maxproj(mp_img[k1])
plt.title('MP recon')
plt.axis('off')

plt.subplot(224)
plot_maxproj(mp_img[k2])
plt.title('MP recon')
plt.axis('off');<jupyter_output><empty_output><jupyter_text>Compute registrations for images  Choose registration method<jupyter_code>reg = partial(imagereg.ot_registration, degree=3, n_iter=50)<jupyter_output><empty_output><jupyter_text>Register all frames to first frame<jupyter_code>models = [None] * (n_frames - 1)
debug = [None] * (n_frames - 1)

# Compute mapping from nth frame to first frame
for t in range(n_frames - 1):
    models[t], debug[t] = reg(mps[t + 1], mps[0])<jupyter_output><empty_output><jupyter_text>Reconstruct frames using mappings learned using both methods<jupyter_code>rec = [mp_img[0]] + [None] * (n_frames - 1)

for t in range(1, n_frames):
    
    pb_pts = models[t - 1].predict(mps[t].pts)
    pb_mp = imagerep.ImageMP(pb_pts, mps[t].wts, mps[t].cov, mps[t].img_shape)
    rec[t] = imagerep.reconstruct_mp_image(pb_mp)<jupyter_output><empty_output><jupyter_text>Plot single frame next to reconstruction<jupyter_code>t = 1

plt.figure(figsize=(15, 15))

plt.subplot(121)
plot_maxproj(mp_img[t])
plt.title(f'frame: {t}')
plt.axis('off')
        
plt.subplot(122)
plot_maxproj(rec[t])
plt.title(f'reconstruction')
plt.axis('off');<jupyter_output><empty_output><jupyter_text>Create video of all frame reconstructions<jupyter_code>fig, (ax_1, ax_2) = plt.subplots(1, 2, figsize=(15, 15))

ax_1.set_title('frame: 0')
ax_2.set_title('straightened')

ax_1.axis('off')
ax_2.axis('off')

def animate(t):
    
    ax_1.set_title(f'frame: {t}')
    
    plot_maxproj(mp_img[t], ax=ax_1, animated=True)
    plot_maxproj(rec[t], ax=ax_2, animated=True)
    
ani = animation.FuncAnimation(fig, animate, n_frames)
#ani.save('video_straight_ot.mp4')

# Prevent double-display of animation
plt.close()

# Display animation in notebook
HTML(ani.to_html5_video())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1341611144760751, 'fraction_numerical': 0.029376135675348275, 'mean_word_length': 6.227571115973742, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '123117', 'license': 'MIT', 'n_tokens_mistral': 1397, 'n_tokens_neox': 1296, 'n_words': 254, 'path': 'python/notebooks/image_registration/multi_frame/05_register_straight.ipynb', 'repo_name': 'evarol/ot_tracking', 'size': '3302'}