title: How to properly correct for XY image drift using a python script?
body: <p>I'm new at coding and this is my first post!
  As a first serious task, I'm trying to implement a simple image drift correction routine in python (so I do not need to rely on ImageJ plugins) using skimage features such as register_translation and fourier_shift.</p>
  
  <p>Below you can find what I've done so far,
  but here's my main questions regarding the approach:</p>
  
  <p>Is the shift correction well applied?</p>
  
  <p>.one thing that is not clear to me when we apply the cross-correlation peak by an FFT to identify the relative shift, is how this approach distinguishes between 'artifact' image drift and real object movement? (i.e real pixel intensity shift).</p>
  
  <p>. I measured the drift for every two consecutive images and corrected the time-lapse accordingly. is there a better way to do it?</p>
  
  <p>. so far I think I managed to correct at least partially the drift in my movies, but the final output still shows 1 pixel drift in a random direction, and my tiff movies look like they are 'flickering' (due to the pixel). but i should apply the drift correction in a different way!?</p>
  
  <p>Looking forward for some insight, not only for my specific question but in this topic in general.</p>
  
  <pre><code># import the basics
  
  import numpy as np
  import matplotlib.pyplot as plt
  from skimage import data
  from skimage.feature import register_translation
  from scipy.ndimage import fourier_shift
  from skimage import io
  
  '''  register translation estimates the cross-correlation peak by an FFT
  i.e, identifies the relative shift between two similar-sized images
  using cross-correlation in Fourier space '''
  
  movie = mymovie
  shifts = []
  corrected_shift_movie = []
  
  for img in range(0,movie.shape[0]):
      if img &lt; movie.shape[0] - 1:
  
          shift, error, diffphase = register_translation(movie[0], movie[img + 1])
  
          img_corr = fourier_shift(np.fft.fftn(movie[img + 1]), shift)
          img_corr = np.fft.ifftn(img_corr)
  
          shifts.append(shift)
          corrected_shift_movie.append(img_corr.real)
  
  
  # for plotting the xy shifts over time
  
  shifts = np.array(shifts)
  corrected_shift_movie = np.array(corrected_shift_movie)
  
  x_drift = [shifts[i][0] for i in range(0,shifts.shape[0])]
  y_drift = [shifts[i][1] for i in range(0,shifts.shape[0])]
  
  plt.plot(x_drift, '--g' , label = ' X drift')
  plt.plot(y_drift, '--r' , label = ' Y drfit')
  plt.legend()
  
  # checking drift for the new corrected movie
  
  movie = corrected_shift_movie
  shifts_corr = []
  
  for img in range(0,movie.shape[0]):
      if img &lt; movie.shape[0] - 1:
  
          shift, error, diffphase = register_translation(movie[0], movie[img + 1])
          shifts_corr.append(shift)
  
  shifts_corr = np.array(shifts_corr)
  
  x_drift = [shifts_corr[i][0] for i in range(0,shifts_corr.shape[0])]
  y_drift = [shifts_corr[i][1] for i in range(0,shifts_corr.shape[0])]
  
  plt.plot(x_drift, '--g' , label = ' X drift')
  plt.plot(y_drift, '--r' , label = ' Y drfit')
  plt.legend()
  
  # saving the new corrected movie
  
  import tifffile as tiff
  movie_to_save = corrected_shift_movie
  
  with tiff.TiffWriter('drift_correction.tif', bigtiff=True) as tif:
          for new_image in range(movie_to_save.shape[0]):
              tif.save(movie_to_save[new_image], compress=0)
  
  </code></pre>