helper

Overview

The helper module contains utility functions to assist with processing wave values, normalizing data, and performing feature-based image registration.

Functions

wizard._utils.helper.find_nex_greater_wave(waves, wave_1, maximum_deviation=5)[source]

Find the next greater wave value within a specified deviation.

This function searches for the smallest wave value greater than or equal to wave_1 that exists in the waves list, within a deviation range up to maximum_deviation. If no such wave is found, the function returns -1.

Parameters:
  • waves (list[int]) – A list of integer wave values to search within.

  • wave_1 (int) – The reference wave value to find the next greater wave after.

  • maximum_deviation (int, optional) – The maximum positive offset from wave_1 to consider (default is 5).

Returns:

The next greater wave value found within the range, or -1 if none exists.

Return type:

int

Raises:

None

Notes

The function stops searching once it finds the first match within the allowed range.

Examples

>>> find_nex_greater_wave([400, 405, 410, 415], 403)
405
>>> find_nex_greater_wave([400, 405, 410, 415], 416)
-1
wizard._utils.helper.find_nex_smaller_wave(waves, wave_1, maximum_deviation=5)[source]

Find the next smaller wave value within a specified deviation.

This function searches for the largest wave value smaller than or equal to wave_1 that exists in the waves list, within a deviation range up to maximum_deviation. If no such wave is found, the function returns -1.

Parameters:
  • waves (list[int]) – A list of integer wave values to search within.

  • wave_1 (int) – The reference wave value to find the next smaller wave before.

  • maximum_deviation (int, optional) – The maximum negative offset from wave_1 to consider (default is 5).

Returns:

The next smaller wave value found within the range, or -1 if none exists.

Return type:

int

Raises:

None

Notes

The function stops searching once it finds the first match within the allowed range.

Examples

>>> find_nex_smaller_wave([390, 395, 400, 405], 402)
400
>>> find_nex_smaller_wave([390, 395, 400, 405], 389)
-1
wizard._utils.helper.normalize_spec(spec)[source]

Normalize a spectrum to the range [0, 1].

This function scales the input spectral array so that its values lie between 0 and 1. If the spectrum has constant values (i.e., no variation), it is returned unchanged. The function ensures numerical stability using np.clip.

Parameters:

spec (np.ndarray) – A one-dimensional NumPy array representing the spectrum to normalize.

Returns:

A normalized NumPy array with values scaled to the [0, 1] range, or the original array if it has no dynamic range.

Return type:

np.ndarray

Raises:

None

Notes

Normalization is only performed if the minimum and maximum values differ.

Examples

>>> import numpy as np
>>> normalize_spec(np.array([2.0, 4.0, 6.0]))
array([0. , 0.5, 1. ])
>>> normalize_spec(np.array([5.0, 5.0, 5.0]))
array([5., 5., 5.])
wizard._utils.helper.feature_regestration(o_img, a_img, max_features=5000, match_percent=0.1)[source]

Perform feature-based registration of two grayscale images.

This function aligns a moving image (a_img) to a reference image (o_img) using ORB keypoints and brute-force Hamming descriptor matching. It returns the aligned image and the computed homography transformation matrix.

Parameters:
  • o_img (np.ndarray) – A 2D NumPy array representing the reference (original) grayscale image.

  • a_img (np.ndarray) – A 2D NumPy array representing the image to be aligned (affine-transformed).

  • max_features (int, optional) – The maximum number of ORB features to detect (default is 5000).

  • match_percent (float, optional) – The fraction of best matches to use when computing the homography (default is 0.1).

Returns:

A tuple containing: - The aligned image (np.ndarray) warped to match the reference. - The homography matrix (np.ndarray) used for the transformation.

Return type:

tuple[np.ndarray, np.ndarray]

Raises:

ValueError – If feature matching fails or not enough good matches are found to compute homography.

Notes

Images are automatically normalized and converted to 8-bit if needed. The function uses RANSAC to compute a robust homography estimate from feature correspondences.

Examples

>>> import numpy as np
>>> import cv2
>>> ref_img = cv2.imread("reference.png", cv2.IMREAD_GRAYSCALE)
>>> mov_img = cv2.imread("moving.png", cv2.IMREAD_GRAYSCALE)
>>> aligned, H = feature_regestration(ref_img, mov_img)
>>> print(H.shape)
(3, 3)