constitch.alignment
index
/home/nicho/starcall-docs/constitch/alignment.py

 
Modules
       
itertools
math
numpy
numba
skimage
sys
time

 
Classes
       
builtins.object
Aligner
FFTAligner
PaddedFFTAligner
FeatureAligner
PCCAligner

 
class Aligner(builtins.object)
    Abstract class that defines an algorithm for aligning two images onto each other.
 
  Methods defined here:
align(self, constraint, precalc1=None, precalc2=None)
Performs the alignment of two images, finding the pixel offset that best aligns the
two images. The offset should be from image1 to image2. The return value should be a Constraint
object, with at least the dx, dy fields filled in to represent the offset of image2 needed
to align the two images. If the function finds the images don't overlap, None should be returned.
If a constraint is specified, this method should only return a constraint that fits within the error
of given constraint, meaning within constraint.error pixels from the (constraint.dx, constraint.dy)
offset.
precalculate(self, image, box)
Performs an arbitrary precalculation step specific to the alignment algorithm.
This would be a step in the algorithm that only has to be run once per image,
and can be cached for each calculation done with the same image. The result of
this function will be passed to the align function as the precalc1 or precalc2 argument
precalculate_if_needed(self, constraint, precalc1, precalc2)
resize_if_needed(self, image, box=None, downscale_factor=None, padding=None)
# Helper functions for subclasses:

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class FFTAligner(Aligner)
    FFTAligner(num_peaks=2, precalculate_fft=True, downscale_factor=None, upscale_factor=1)
 
An aligner that uses the phase cross correlation algorithm to estimate the proper alignment of two images.
The algorithm is the same as described in Kuglin, Charles D. "The phase correlation image alignment method."
http://boutigny.free.fr/Astronomie/AstroSources/Kuglin-Hines.pdf
 
 
Method resolution order:
FFTAligner
Aligner
builtins.object

Methods defined here:
__init__(self, num_peaks=2, precalculate_fft=True, downscale_factor=None, upscale_factor=1)
num_peaks: int default 2
    Sets the number of peaks in the resulting phase cross correlation image to be checked. Sometimes the highest
    peak is not actually the best offset for alignment so checking multiple peaks can help find the one that
    has the highest ncc score. Increasing this will also increase processing time, possibly dramatically.
precalculate_fft: bool default True
    Whether or not the FFT of the images should be precalculated and cached or be calculated every time.
    The FFT is able to be precalculated however it does take up a large amount of memory, so if memory
    is a limiting factor setting this to False can help
downscale_factor: int, optional
    This algorithm is rather slow when calculating large numbers of constraints, and one way to improve
    speed is by downscaling the images before running the algorithm. This will improve runtime at the expense
    of precision, the constraints calculated will have a nonzero error value. Also, if the downscale factor
    is large enough the algorithm can begin to fail and not find any overlap, in general the largest recommended
    value is around 32, but it depends on how large the features in your images are.
align(self, constraint, precalc1=None, precalc2=None)
Performs the alignment of two images, finding the pixel offset that best aligns the
two images. The offset should be from image1 to image2. The return value should be a Constraint
object, with at least the dx, dy fields filled in to represent the offset of image2 needed
to align the two images. If the function finds the images don't overlap, None should be returned.
If a constraint is specified, this method should only return a constraint that fits within the error
of given constraint, meaning within constraint.error pixels from the (constraint.dx, constraint.dy)
offset.
align_full(self, constraint, precalc1=None, precalc2=None)
precalculate(self, image, box=None)
Performs an arbitrary precalculation step specific to the alignment algorithm.
This would be a step in the algorithm that only has to be run once per image,
and can be cached for each calculation done with the same image. The result of
this function will be passed to the align function as the precalc1 or precalc2 argument

Methods inherited from Aligner:
precalculate_if_needed(self, constraint, precalc1, precalc2)
resize_if_needed(self, image, box=None, downscale_factor=None, padding=None)
# Helper functions for subclasses:

Data descriptors inherited from Aligner:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class FeatureAligner(Aligner)
    FeatureAligner(num_features=2000)
 

 
 
Method resolution order:
FeatureAligner
Aligner
builtins.object

Methods defined here:
__init__(self, num_features=2000)
Initialize self.  See help(type(self)) for accurate signature.
align(self, constraint, precalc1=None, precalc2=None)
Performs the alignment of two images, finding the pixel offset that best aligns the
two images. The offset should be from image1 to image2. The return value should be a Constraint
object, with at least the dx, dy fields filled in to represent the offset of image2 needed
to align the two images. If the function finds the images don't overlap, None should be returned.
If a constraint is specified, this method should only return a constraint that fits within the error
of given constraint, meaning within constraint.error pixels from the (constraint.dx, constraint.dy)
offset.
precalculate(self, image, box=None)
Performs an arbitrary precalculation step specific to the alignment algorithm.
This would be a step in the algorithm that only has to be run once per image,
and can be cached for each calculation done with the same image. The result of
this function will be passed to the align function as the precalc1 or precalc2 argument

Methods inherited from Aligner:
precalculate_if_needed(self, constraint, precalc1, precalc2)
resize_if_needed(self, image, box=None, downscale_factor=None, padding=None)
# Helper functions for subclasses:

Data descriptors inherited from Aligner:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class PCCAligner(Aligner)
    PCCAligner(**kwargs)
 
An aligner that invokes the skimage.registration.phase_cross_correlation method
An alternative to the FFTAligner which implements the same algorithm
 
 
Method resolution order:
PCCAligner
Aligner
builtins.object

Methods defined here:
__init__(self, **kwargs)
Initialize self.  See help(type(self)) for accurate signature.
align(self, constraint, precalc1=None, precalc2=None)
Performs the alignment of two images, finding the pixel offset that best aligns the
two images. The offset should be from image1 to image2. The return value should be a Constraint
object, with at least the dx, dy fields filled in to represent the offset of image2 needed
to align the two images. If the function finds the images don't overlap, None should be returned.
If a constraint is specified, this method should only return a constraint that fits within the error
of given constraint, meaning within constraint.error pixels from the (constraint.dx, constraint.dy)
offset.

Methods inherited from Aligner:
precalculate(self, image, box)
Performs an arbitrary precalculation step specific to the alignment algorithm.
This would be a step in the algorithm that only has to be run once per image,
and can be cached for each calculation done with the same image. The result of
this function will be passed to the align function as the precalc1 or precalc2 argument
precalculate_if_needed(self, constraint, precalc1, precalc2)
resize_if_needed(self, image, box=None, downscale_factor=None, padding=None)
# Helper functions for subclasses:

Data descriptors inherited from Aligner:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class PaddedFFTAligner(FFTAligner)
    PaddedFFTAligner(num_peaks=2, precalculate_fft=True, downscale_factor=None, upscale_factor=1)
 

 
 
Method resolution order:
PaddedFFTAligner
FFTAligner
Aligner
builtins.object

Methods defined here:
align(self, constraint, precalc1=None, precalc2=None)
Performs the alignment of two images, finding the pixel offset that best aligns the
two images. The offset should be from image1 to image2. The return value should be a Constraint
object, with at least the dx, dy fields filled in to represent the offset of image2 needed
to align the two images. If the function finds the images don't overlap, None should be returned.
If a constraint is specified, this method should only return a constraint that fits within the error
of given constraint, meaning within constraint.error pixels from the (constraint.dx, constraint.dy)
offset.
precalculate(self, image, box=None)
Performs an arbitrary precalculation step specific to the alignment algorithm.
This would be a step in the algorithm that only has to be run once per image,
and can be cached for each calculation done with the same image. The result of
this function will be passed to the align function as the precalc1 or precalc2 argument

Methods inherited from FFTAligner:
__init__(self, num_peaks=2, precalculate_fft=True, downscale_factor=None, upscale_factor=1)
num_peaks: int default 2
    Sets the number of peaks in the resulting phase cross correlation image to be checked. Sometimes the highest
    peak is not actually the best offset for alignment so checking multiple peaks can help find the one that
    has the highest ncc score. Increasing this will also increase processing time, possibly dramatically.
precalculate_fft: bool default True
    Whether or not the FFT of the images should be precalculated and cached or be calculated every time.
    The FFT is able to be precalculated however it does take up a large amount of memory, so if memory
    is a limiting factor setting this to False can help
downscale_factor: int, optional
    This algorithm is rather slow when calculating large numbers of constraints, and one way to improve
    speed is by downscaling the images before running the algorithm. This will improve runtime at the expense
    of precision, the constraints calculated will have a nonzero error value. Also, if the downscale factor
    is large enough the algorithm can begin to fail and not find any overlap, in general the largest recommended
    value is around 32, but it depends on how large the features in your images are.
align_full(self, constraint, precalc1=None, precalc2=None)

Methods inherited from Aligner:
precalculate_if_needed(self, constraint, precalc1, precalc2)
resize_if_needed(self, image, box=None, downscale_factor=None, padding=None)
# Helper functions for subclasses:

Data descriptors inherited from Aligner:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
Functions
       
calc_pcm(fft1, fft2)
calculate_offset = calculate_offset_slow(image1, image2, shape1=None, shape2=None, fft1=None, fft2=None, num_peaks=2, max_peaks=2, score_threshold=0.1)
calculate_offset_fast(image1, image2, shape1=None, shape2=None, fft1=None, fft2=None, num_peaks=2)
calculate_offset_slow(image1, image2, shape1=None, shape2=None, fft1=None, fft2=None, num_peaks=2, max_peaks=2, score_threshold=0.1)
extract_overlap_subregion(image, y, x)
Extract the overlapping subregion of the image.
 
Parameters
---------
image : np.ndarray
    the image (the dimension must be 2)
y : Int
    the y (second last dim.) position
x : Int
    the x (last dim.) position
Returns
-------
subimage : np.ndarray
    the extracted subimage
find_peaks(fft, image1, image2, num_peaks)
find_peaks_estimate(fft, image1, image2, num_peaks, estimate, search_range)
image_diff_sizes(image1, image2)
interpret_translation(image1, image2, yins, xins, ymin, ymax, xmin, xmax, num_peaks=2, max_peaks=None, ncc_threshold=None)
Interpret the translation to find the translation with heighest ncc.
    the first image (the dimension must be 2)
yins : IntArray
    the y positions estimated by PCM
xins : IntArray
    the x positions estimated by PCM
ymin : Int
    the minimum value of y (second last dim.)
ymax : Int
    the maximum value of y (second last dim.)
xmin : Int
    the minimum value of x (last dim.)
xmax : Int
    the maximum value of x (last dim.)
num_peaks : Int
    the number of peaks to check
max_peaks : Int
    a limit on the number of peaks to check, only used
    if ncc_threshold is declared.
ncc_threshold : Float
    a threshold of the score of offsets (the ncc) that is needed to
    return an offset. If this is specified and after checking
    num_peaks peaks the highest score found is below this value,
    it will continue checking more peaks until the score is above
    the value or max_peaks is reached.
 
Returns
-------
_ncc : Float
    the highest ncc
x : Int
    the selected x position
y : Int
    the selected y position
multi_peak_max(PCM)
Find the first to n th largest peaks in PCM.
 
Returns:
poses: np.ndarray, N by 2
    the indices of the peaks
values : np.ndarray
    the values of the peaks
ncc = ncc_slow(image1, image2)
Compute the normalized cross correlation for two images.
ncc_fast(image1, image2)
Compute the normalized cross correlation for two images.
ncc_slow(image1, image2)
Compute the normalized cross correlation for two images.
pcm(image1, image2)
Compute peak correlation matrix for two images.
pcm_fft(F1, F2)
Compute peak correlation matrix for two images, with the fft already applied
refine_peak(image_product, upsample_factor, shift)
score_offset(image1, image2, dx, dy)