Coalignment using Template Matching#

A common approach to coaligning a time series of images is to take a representative template that contains the features you are interested in, and match that to your images. The location of the best match tells you where the template is in your image. The images are then shifted to the location of the best match. This aligns your images to the position of the features in your representative template.

This example demonstrates how to coalign maps in a MapSequence using the mapsequence_coalign_by_match_template() function. The implementation of this functionality requires the installation of the image processing library scikit-image.

import matplotlib.pyplot as plt
import sunpy.data.sample
from sunpy.map import Map

from sunkit_image import coalignment

Create a MapSequence using sample data.

mc = Map(
    [
        sunpy.data.sample.AIA_193_CUTOUT01_IMAGE,
        sunpy.data.sample.AIA_193_CUTOUT02_IMAGE,
        sunpy.data.sample.AIA_193_CUTOUT03_IMAGE,
        sunpy.data.sample.AIA_193_CUTOUT04_IMAGE,
        sunpy.data.sample.AIA_193_CUTOUT05_IMAGE,
    ],
    sequence=True,
)

Plot an animation of the MapSequence that we can compare with the coaligned MapSequence.

plt.figure()
anim = mc.plot()
AIA $193 \; \mathrm{\mathring{A}}$ 2011-06-07 06:33:07

To coalign the MapSequence, apply the mapsequence_coalign_by_match_template() function.

coaligned = coalignment.mapsequence_coalign_by_match_template(mc)

This returns a new MapSequence coaligned to a template extracted from the center of the first map in the MapSequence, with the dimensions clipped as required.

For a full list of options and functionality of the coalignment algorithm, see mapsequence_coalign_by_match_template.

Now, let’s plot an animation of the coaligned MapSequence to compare with the original.

plt.figure()
anim_coalign = coaligned.plot()
AIA $193 \; \mathrm{\mathring{A}}$ 2011-06-07 06:33:07

If you just want to calculate the shifts required to compensate for solar rotation relative to the first map in the MapSequence without applying the shifts, use calculate_match_template_shift():

shifts = coalignment.calculate_match_template_shift(mc)

This is the function used to calculate the shifts in mapsequence_coalign_by_match_template(). The shifts calculated here can be passed directly to the coalignment function.

coaligned_shifts = coalignment.mapsequence_coalign_by_match_template(mc, shift=shifts)

plt.figure()
anim_coaligned_shifts = coaligned_shifts.plot()

plt.show()
AIA $193 \; \mathrm{\mathring{A}}$ 2011-06-07 06:33:07

Total running time of the script: (0 minutes 11.880 seconds)

Gallery generated by Sphinx-Gallery