pycroglia.core.skeletonize package
Subpackages
Submodules
- class pycroglia.core.skeletonize.msfm.DerivativeResult(tm=array([0., 0., 0., 0.]), order=array([0, 0, 0, 0], dtype=uint8))[source]
Bases:
objectContainer for derivative results in MSFM2D.
- class pycroglia.core.skeletonize.msfm.FirstOrderStencil(t, i, j, frozen)[source]
Bases:
objectFirst-order stencil for computing derivatives in the MSFM2D algorithm.
- class pycroglia.core.skeletonize.msfm.SecondOrderStencil(first_order, order, t, i, j, frozen)[source]
Bases:
objectSecond-order stencil for refining derivatives in the MSFM2D algorithm.
- Parameters:
- pycroglia.core.skeletonize.msfm.bounds_check(dims, i, j)[source]
Check if a 2D index is inside array bounds.
- Parameters:
- Returns:
True if (i, j) is within bounds, False otherwise.
- Return type:
- Raises:
AssertionError – If dims is not 2D.
- pycroglia.core.skeletonize.msfm.is_frozen2d(i, j, frozen)[source]
Check if a pixel is frozen in the fast marching method.
- pycroglia.core.skeletonize.msfm.msfm2d(speed_image, source_points, use_second=True, use_cross=True, skeletonize=False)[source]
Calculates the shortest distance from a list of points to all other pixels in an image using the Multistencil Fast Marching Method (MSFM).
This function implements the MSFM algorithm, which computes distance maps more accurately than the standard Fast Marching Method by incorporating second-order derivatives and cross-neighbor stencils.
- Parameters:
speed_image (np.ndarray) – The speed function image. All values
zero (must be strictly greater than)
negative (any values are zero or)
be (those regions will never)
reached (i.e., infinite time)
source_points (np.ndarray) – A list of starting points with
shape (N, 2)
zero. (distance is initialized to)
use_second (bool, optional) – If True, the method will use both
True. (computations. Defaults to)
use_cross (bool, optional) – If True, the method will include
diagonal (cross)
True.
skeletonize (bool, optional) – If True, also returns a
skeletonization (Euclidean distance image (used in)
False. (contexts). Defaults to)
- Returns:
A distance image T of the same shape as speed_image, where each pixel holds the shortest distance from any source point. If skeletonize is True, also returns a second image with Euclidean distances.
- Return type:
np.ndarray or Tuple[np.ndarray, np.ndarray]
- pycroglia.core.skeletonize.msfm.roots(coeffs)[source]
Compute roots of a quadratic equation with MSFM2D logic.
Solves the polynomial:
a * x^2 + b * x + c = 0
If a == 0, falls back to a linearized solution. The discriminant is clamped to non-negative values to avoid NaN results. Matches custom matlab implementation. :param coeffs: Coefficients [a, b, c]. :type coeffs: np.ndarray
- Returns:
Array of two roots. May contain np.inf if denominator is zero.
- Return type:
np.ndarray
- Parameters:
coeffs (ndarray)
- class pycroglia.core.skeletonize.shortest_path.ShortestPath(stepper_type=StepperType.RK4, step_size=0.5)[source]
Bases:
objectTraces the shortest path in a 2D or 3D distance map using numerical integration.
This class supports path tracing via numerical integration methods such as: - Runge-Kutta 4th order (RK4) - Euler’s method - Simple gradient descent
The tracing starts from a given point and optionally stops early if a specified target (source) point is reached within a threshold distance.
- Parameters:
stepper_type (StepperType)
step_size (float)
- type
The integration method to use.
- Type:
- calculate(distance_map, start_point, source_point=None)[source]
Traces the shortest path from a starting point in a 2D or 3D distance map.
This function iteratively follows the gradient of the distance map, using the selected stepping method to trace a path. If a source_point is given, the path terminates early if it gets sufficiently close to any source point.
- Parameters:
distance_map (np.ndarray) – A 2D or 3D distance field array.
start_point (np.ndarray) – A point [x, y] or [x, y, z] indicating where to start the path.
source_point (np.ndarray | None, optional) – An array of shape (N, D) where each row is a source point in the same dimension as start_point. If not provided, the path will be traced until it reaches a boundary or stagnates.
- Returns:
A NumPy array of shape (M, D) representing the traced shortest path. Each row is a point along the path.
A boolean flag indicating whether the path failed to reach a source point. If True, it did not reach any source point (e.g. due to early stop or divergence).
- Return type:
- pycroglia.core.skeletonize.skeleton.skeleton(image, stepper_type=StepperType.RK4)[source]
Computes the skeleton (centerlines) of a 2D or 3D binary object using the Multistencil Fast Marching (MSFM) distance transform.
This function returns subvoxel-accurate centerlines of the object by repeatedly tracing shortest paths from medial points to the object’s boundary.
- Parameters:
binary_image (np.ndarray) – A 2D or 3D binary image or volume representing the object.
stepper_type (factory.StepperType) – Algorithm used for raytracing.
image (ndarray)
- Returns:
- A list of (N x D) arrays, each representing one skeleton branch.
D is 2 for 2D input and 3 for 3D input.
- Return type:
list[np.ndarray]