ncempy.algo package

ncempy.algo.centroid(m)[source]

Find the centroid based on the raw moments.

Parameters:

m (ndarray) – The output from moments()

Returns:

The position of the centroid as a 2-tuple

Return type:

tuple, 2 elements (X,Y)

ncempy.algo.image_correlate(image, reference, real_filter=1, k_filter=1, shift_func='shift', verbose=False)[source]

Align image to reference by cross-correlation. Outputs shifts and shifted images. Uses the real FFT for ~2x speed improvement. The k_filter must have a shape that matches the np.fft.rfft2() of image and reference. Uses scipy.ndimage.shift() or np.roll to move the image. Use ‘roll’ to avoid losing data off the edge for multiple shifting operations. Use shift to avoid wrap around problems and when there is only one shifting operation.

Note

image, reference and real_filter must all have the same shape (N, M). k_filter must have a shape that matches the np.fft.rfft2() of the other inputs: (N, M/2+1)

Parameters:
  • image (ndarray) – A image as a 2D ndarray.

  • reference (ndarray) – The reference image to align to.

  • real_filter (ndarray, optional, default = 1) – A real space filter applied to image and reference before calculating the shift.

  • k_filter (ndarray, optional, default = 1) – A Fourier space filter applied to the fourier transform of image and reference before calculating the cross-correlation.

  • shift_func (str, default is 'shift') – The function to use to shift the images. ‘roll’ uses np.roll and ‘shift’ uses ndimage.shift.

  • verbose (bool) – Plots the cross-correlation using matplotlib for debugging purposes.

Returns:

A tuple containing the shifted image and the shifts applied.

Return type:

tuple, (ndarray, tuple)

ncempy.algo.image_cross_corr(image, reference, real_filter=1, k_filter=1)[source]

Calculate image cross-correlation. See imageCrossCorRealShift and other similar functions to calculate the shift and apply the shift.

Parameters:
  • image (ndarray) – The source image to align to. Should be even sized.

  • reference (ndarray) – The reference image to align to the source image. Should be even sized.

  • real_filter (ndarray, optional, default 1) – Filter to apply to each image in real space before FFT.

  • k_filter (ndarray, optional default 1) – Filter to apply to each image in FFT space

Returns:

Cross correlation of image and reference.

Return type:

ndarray

ncempy.algo.image_phase_correlate(image, reference, real_filter=1, k_filter=1, shift_func='shift', verbose=False)[source]

Align image to reference by phase-correlation. Outputs shifted images and shift. Uses np.fft.rfft2 for ~2x speed improvement. Uses scipy.ndimage.shift() to shift the image and remove border pixels.

NOT WORKING OR TESTED YET

Note

image, reference and real_filter must all have the same shape (N, M). k_filter must have a shape that matches the np.fft.rfft2() of the other inputs: (N, M/2+1)

Parameters:
  • image (ndarray) – A image as a 2D ndarray.

  • reference (ndarray) – The reference image to align to.

  • real_filter (ndarray, optional, default = 1) – A real space filter applied to image and reference before calculating the shift.

  • k_filter (ndarray, optional, default = 1) – A Fourier space filter applied to the fourier transform of image and reference before calculating the cross-correlation.

  • shift_func – The function to use to shift the images. ‘roll’ uses np.roll and ‘shift’ uses ndimage.shift.

Returns:

A tuple containing the shifted image and the shifts applied.

Return type:

tuple, (ndarray, tuple)

ncempy.algo.moment_angle(mc)[source]

Calculate the orientation of the object from its central moments

Parameters:

mc (ndarray) – The array of the central moments. Output from moments_central

Returns:

The angle in radians.

Return type:

float

ncempy.algo.moments(im, order=3)[source]

Calculate the image raw moments.

Parameters:
  • im (ndarray) – A 2D array of the image to calculate the moments for

  • order (int, default=3) – The order to calculate the moments to.

Returns:

An ndarray of (order+1, order+1)

Return type:

ndarray

ncempy.algo.moments_central(im, cent=None, order=3)[source]

Calculate the image central moments.

Parameters:
  • im (ndarray) – A 2D array of the image to calculate the central moments for

  • cent (tuple, 2 values, optional) – The centroid calculated from the image raw moments. If it is not supplied then it is calculated by moments()

  • order (int, default = 3) – The order to calculate the orders to.

Returns:

An ndarray of size (order + 1, order +1)

Return type:

ndarray

ncempy.algo.rebin(im, f, funcType='sum')[source]

Rebin a 2D array. From stackoverflow: https://stackoverflow.com/questions/4624112/grouping-2d-numpy-array-in-average

This should be used carefully. There is not antialiasing applied which could produce odd effects for some data sets (such as high resolution data)

Parameters:
  • im (ndarray) – 2D array to reduce

  • f (int) – The factor to rebin by. Must be integer

  • funcType (str) – The type of reduction. mean or sum are implemented.

Returns:

The 2D array with the new size.

Return type:

ndarray

ncempy.algo.rotateImage(im, theta, pad=False)[source]

Use three shears in Fourier space to exactly (and reversibly) rotate an image.

Currently only works for square images.

Parameters:
  • im (ndarray, 2D) – The image to rotate

  • theta (float) – The angle to rotate by in radians

  • pad (bool) – Add padding to the image before rotating

ncempy.algo.shearImage(im, dim, shear_factor)[source]

Exact shear of an image using the frequency space of the FFT of the image.

Currently only works for square images.

Parameters:
  • im (ndarray, 2D) – The image to shear.

  • dim (int) – The axis to shear along

  • shear_factor (float) – The amount of shear.

Returns:

The sheared image.

Return type:

ndarray

ncempy.algo.shiftImage(im, shift)[source]

Exact shear of an image using the frequency space of the FFT of the image.

Parameters:
  • im (ndarray, 2D) – The image to shear.

  • shift (tuple of floats) – The number of pixels to shift in each direction

Returns:

The shifted image.

Return type:

ndarray

ncempy.algo.stack_align(stack, align_type='static', real_filter=1, k_filter=1, shift_func='shift')[source]

Align a series of images by cross-correlation. All images are aligned to the first image Uses image_correlate which is based on simple cross correlation.

Notes

You should probably use ncempy.algo.stack_align since it uses mutlicorr and is more functional.

Parameters:
  • stack (ndarray, 3D) – The stack of images to align. Shape [num, Y, X]

  • real_filter (ndarray, optional, default = 1) – A real space filter to apply before cross-correlation of each image. Shape must be [Y, X]

  • k_filter (ndarray, optional, default = 1) – A Fourier space filter to apply before cross-correlation. Shape must be [Y, X/2 + 1]

  • shift_func (str, default is 'shift') – The function to use to shift the images. ‘roll’ uses np.roll and ‘shift’ uses ndimage.shift.

  • align_type (str) – static or dynamic alignment. Static aligns all images to the first image. Dynamic aligns each image to the previous image starting with the first image

Returns:

A tuple containing the aligned images as a 3D ndarray of shape [num, Y, X] and shifts as a 2D ndarray of shape [num, 2]

Return type:

tuple, aligned stack, shifts

Submodules