Utility functions

Filtering

Functions for filtering individual images.

PyLorentz.utils.filter.filter_hotpix(image, thresh=30, show=False, maxiters=3, kernel_size=3, fast=False, _current_iter=0, verbose=False)[source]

Look for pixel values with an intensity > thresh std outside of the mean of surrounding pixels. If found, replace with the median value of those pixels.

Parameters:
  • image (np.ndarray) – The input image.

  • thresh (float) – Threshold for identifying hot pixels. Default is 30.

  • show (bool) – Whether to display the image with hot pixels identified. Default is False.

  • maxiters (int) – Maximum number of iterations for filtering. Default is 3.

  • kernel_size (int) – Size of the kernel used for local mean calculation. Default is 3.

  • fast (bool) – Whether to use a faster, less accurate method. Default is False.

  • _current_iter (int) – Current iteration count (for internal use). Default is 0.

  • verbose (bool) – Whether to print verbose messages. Default is False.

Returns:

The filtered image.

Return type:

np.ndarray

PyLorentz.utils.filter.extract_patches(array, indices, patch_size=3)[source]

Extract patches from an array around the given indices.

Parameters:
  • array (np.ndarray) – The input array.

  • indices (np.ndarray) – The indices around which to extract patches.

  • patch_size (int) – The size of the patches to extract. Default is 3.

Returns:

The extracted patches.

Return type:

np.ndarray

PyLorentz.utils.filter.bandpass_filter(image, sampling=1, q_lowpass=None, q_highpass=None, filter_type='butterworth', butterworth_order=2, show=False)[source]

Apply a bandpass filter to an image.

Parameters:
  • image (np.ndarray) – The input image.

  • sampling (float) – Scale of the image in pix/nm. Default is 1.

  • q_lowpass (Optional[float]) – Low-pass filter cutoff frequency. Default is None.

  • q_highpass (Optional[float]) – High-pass filter cutoff frequency. Default is None.

  • filter_type (str) – Type of filter to use (“butterworth” or “gaussian”). Default is “butterworth”.

  • butterworth_order (int) – Order of the Butterworth filter. Default is 2.

Returns:

The filtered image.

Return type:

np.ndarray

Other utilties

PyLorentz.utils.utils.dist4(dim, shifted=True)[source]

4-fold symmetric distance map (center is 0) even at small radii centered in the middle (i.e. fft shifted) by default

Return type:

ndarray

PyLorentz.utils.utils.circ4(dim, rad)[source]

4-fold symmetric circle even at small dimensions

PyLorentz.utils.utils.norm_image(image)[source]

Normalize image intensities to between 0 and 1. Returns copy

Return type:

np.ndarray

PyLorentz.utils.utils.tukey2d(shape, alpha=0.5, sym=True, shrink=0, shifted=False)[source]

Create a 2D Tukey window.

Parameters:
  • shape – Shape of the window (height, width).

  • alpha – Shape parameter of the Tukey window.

  • sym – If True, makes the window symmetric.

  • shrink – N pix extra to shrink the window on each side (default 0)

  • shifted – If True, will corner-center the window with fftshift

Returns:

2D Tukey window.

shrink = N pix to extra shrink the window on each side