stouputils.image.numpy_to_obj module#

extract_verts_faces_from_segment(
array: NDArray[Any],
spacing: tuple[float, float, float],
threshold: float,
step_size: int,
pad_array: bool,
) tuple[NDArray[np.floating], NDArray[np.integer]][source]#

Extract vertices and faces from a single segmentation array using marching cubes.

Parameters:
  • array (NDArray) – 3D numpy array containing the segmentation mask.

  • spacing (tuple) – Voxel spacing along each axis, passed to marching cubes.

  • threshold (float) – Threshold level for marching cubes (0.5 for binary data).

  • step_size (int) – Step size for marching cubes (higher = simpler mesh, faster generation).

  • pad_array (bool) – If True, pad array with zeros to ensure closed volumes for border cells.

Returns:

Vertices and faces of the extracted mesh.

Return type:

tuple[NDArray[np.floating], NDArray[np.integer]]

numpy_to_obj(
path: str,
array: NDArray[Any],
spacing: tuple[float, float, float] = (1.0, 1.0, 1.0),
threshold: float = 0.5,
step_size: int = 1,
pad_array: bool = True,
verbose: int = 0,
) None[source]#

Generate a ‘.obj’ file from a numpy array for 3D visualization using marching cubes.

Parameters:
  • path (str) – Path to the output .obj file.

  • array (NDArray) – Numpy array to be dumped (must be 3D).

  • spacing (tuple) – Voxel spacing along each axis, passed to marching cubes.

  • threshold (float) – Threshold level for marching cubes (0.5 for binary data).

  • step_size (int) – Step size for marching cubes (higher = simpler mesh, faster generation).

  • pad_array (bool) – If True, pad array with zeros to ensure closed volumes for border cells.

  • verbose (int) – Verbosity level (0 = no output, 1 = some output, 2 = full output).

Examples

> array = np.random.rand(64, 64, 64) > 0.5
> numpy_to_obj("output_mesh.obj", array, threshold=0.5, step_size=2, pad_array=True, verbose=1)

> array = my_3d_data
> numpy_to_obj("output_mesh.obj", array, spacing=(1.0, 1.0, 2.5), threshold=0.3)