Alignment

aligning images

The smtools.algnment module is designed to align images that were split into separate channels during data collection. It relies on using fluorescent particles that appear in both channels. To see how it works see our walkthrough here . Also finding maxima and fitting gaussians.

import smtools.testdata as test
import smtools.alignment as al
import matplotlib.pyplot as plt
dx, dy, params = al.inspect_global_fit(test.image_stack(), showplot=False)
im = test.image_stack()[0]
im_old = al.overlay(im)
im_adj_image = al.align_by_offset(im,dx,dy)
im_new = al.overlay(im_adj_image)
fig = plt.figure(figsize=(12,12))
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212,sharex=ax1)
ax1.set_title('Original Image', fontsize = "18")
ax2.set_title('Aligned Image', fontsize = "18")
ax1.imshow(im_old)
ax2.imshow(im_new)
plt.show()
_images/alignment.png

Alignment module

The alignment module contains functions used in aligning two channel data with fluorescent dyes. See our walkthrough of the alignment module’s usage.

alignment.get_offset_distribution(Image, bbox=9, splitstyle='hsplit', fsize=10)
This function in order:
  • splits the image into channels
  • locates and fits all of the points in each channel
  • pairs up associated points from each channel, uses cDKTree
  • and determines their offset
Parameters:
  • Image – 2D image array
  • bbox – int, passed to point_fitting.fit_routine, size of ROI around each point to apply gaussian fit. Default is 9.
  • splitstyle – string, passed to im_split; accepts “hsplit”, “vsplit”. Default is “hsplit”
  • fsize – int, passed to point_fitting.find_maxima, size of average filters used in maxima determination. Default is 10.
Returns:

Two lists containing all of the measured x- and y- offsets

Example:
>>> from smtools.alignment import get_offset_distribution
>>> import smtools.testdata as test
>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> im = test.image_stack()[0]
>>> x_dist, y_dist = get_offset_distribution(im)
>>> print(np.mean(x_dist), np.mean(y_dist))
-1.9008888233326608 -2.042675546813981
alignment.plot_assigned_maxima(Image, splitstyle='hsplit', fsize=10)

This function spits out a matplotlib plot with lines drawn between each of the assigned pairs of maxima. The purpose of this function is more for a sanity check than anything useful.

Parameters:
  • Image – 2D image array
  • splitstyle – string, passed to im_split; accepts “hsplit”, “vsplit”. Default is “hsplit”
  • fsize – int, passed to point_fitting.find_maxima, size of average filters used in maxima determination. Default is 10.
Returns:

fancy plot of assigned points.

Example:
>>> from smtools.alignment import plot_assigned_maxima
>>> import smtools.testdata as test
>>> im = test.image_stack()[0]
>>> plot_assigned_maxima(im)
alignment.inspect_global_fit(im_stack, bbox=9, fsize=10, binwidth=0.1, init_params=None, splitstyle='hsplit', showplot=True)

Basic alignment function. Accepts a 1D list of image arrays, then splits the images, locates corresponding maxima in each channel and then calculates the best shift in x and y to align each maxima pair. If showplot is set to True, this function also produces a pair of histograms of all the measured offsets and resulting fits to those data.

Parameters:
  • im_stack – 1D list of image arrays to be used in determination of the offset
  • bbox – int, passed to point_fitting.fit_routine, size of ROI around each point to apply gaussian fit. Default is 9.
  • fsize – int, passed to point_fitting.find_maxima, size of average filters used in maxima determination. Default is 10.
  • binwidth – float, passed to make_bins; resolution of histogram for fitting
  • init_params – 1D array, initial conditions passed to scipy.optimize.curve_fit. must be length 5, p0 = [loc, scale, shape, amplitude, baseline]
  • splitstyle – string, passed to im_split; orientation of channels, vertical or horizontal
  • showplot – bool, if True, will generate a plot of the distribution and fit
Returns:

tuple containing optimal parameters and covariance matrix from fit. (popt_x, pcov_x, popt_y, pcov_y)

Example:
>>> from smtools.alignment import inspect_global_fit
>>> import smtools.testdata as test
>>> params = inspect_global_fit(test.image_stack())
>>> print(params[0][0],params[2][0])
5.612082237088681 -2.651765063702885
alignment.inspect_individual_fits(im_stack, bbox=9, fsize=10, binwidth=0.1, init_params=None, splitstyle='hsplit')

This function provides a method to plot each individual offset distributions of images passed to the function. Common usage is to get a sense of how similar groups of images are.

Parameters:
  • im_stack – 1D list of image arrays to be used in determination of the offset
  • bbox – int, passed to point_fitting.fit_routine, size of ROI around each point to apply gaussian fit. Default is 9.
  • fsize – int, passed to point_fitting.find_maxima, size of average filters used in maxima determination. Default is 10.
  • binwidth – float, passed to make_bins; resolution of histogram for fitting
  • init_params – 1D array, initial conditions passed to scipy.optimize.curve_fit. must be length 5, p0 = [loc, scale, shape, amplitude, baseline]
  • splitstyle – string, passed to im_split; orientation of channels, vertical or horizontal
Returns:

produces a plot of histograms and fits for each individual image passed in im_stack. Also returns list of tuples, each contains optimal parameters and covariance matrix from fit. If no fit was found, returns an empty list. The lists alternate between x and y-offset fits.

Example:
>>> from smtools.alignment import inspect_individual_fits
>>> import smtools.testdata as test
>>> params = inspect_individual_fits(test.image_stack())
alignment.align_by_offset(Image, shift_x, shift_y, splitstyle='hsplit', shift_channel=1)

This function shifts one channel of the array based supplied offset values. Retains the single image structure.

Parameters:
  • Image – 2D image array
  • shift_x – float, offset in x
  • shift_y – float, offset in y
  • splitstyle – string, passed to im_split; accepts “hsplit”, “vsplit”. Default is “hsplit”
  • shift_channel – int, which channel to shift by offsets, default is channel 1.
Returns:

2D image array of aligned image

Example:
>>> from smtools.alignment import find_global_offset,
align_by_offset
>>> import smtools.testdata as test
>>> import matplotlib.pyplot as plt
>>> im = test.image_stack()
>>> dx, dy = find_global_offset(im)
>>> new_image = align_by_offset(im[0], dx, dy)
>>> plt.imshow(new_image), plt.show()
alignment.overlay(Image, splitstyle='hsplit', rot=True, invert=False)

Overlays the two channels derived from Image. Converts Image to an 8-bit RGB array, with one channel colored magenta and the other green.

Parameters:
  • Image – 2D image array
  • splitstyle – string, passed to im_split; accepts “hsplit”, “vsplit”. Default is “hsplit”
  • rot – bool, if True, image is rotated 90 degrees
  • invert – bool, if True, inverts the channel color assignment.
Returns:

8-bit RGB image

Example:
>>> from smtools.alignment import overlay
>>> import smtools.testdata as test
>>> import matplotlib.pyplot as plt
>>> im = test.image_stack()
>>> dx, dy = find_global_offset(im)
>>> aligned_image = align_by_offset(im[0], dx, dy)
>>> overlayed = overlay(aligned_image)
>>> plt.imshow(overlayed), plt.show()