mesh Module¶
The mesh module provides low-level functions for mesh processing, contour generation, and mesh creation from 3D images.
Contour Generation¶
- contour(image, iterations=25, smoothing=1, masking_factor=0.75, crop=True, resolution=None, clahe_window=None, clahe_clip_limit=None, gaussian_sigma=None, gaussian_iterations=5, fill_slices=True, chan_vese_lambda1=1, chan_vese_lambda2=1, register_stack=True, target_resolution=None, fill_inland_threshold=None, return_resolution=False, verbose=True)[source]¶
Generate binary contour from 3D image using morphological active contours.
Performs image preprocessing (CLAHE, Gaussian filtering), stack registration, and morphological Chan-Vese segmentation to extract object contour.
- Parameters:
image (
str|ndarray[tuple[Any,...],dtype[Any]]) – Input 3D image array or file pathiterations (
int, default:25) – Number of morphological Chan-Vese iterationssmoothing (
int, default:1) – Smoothing iterations per cyclemasking_factor (
float|ndarray[tuple[Any,...],dtype[bool]], default:0.75) – Initial mask threshold (fraction of Otsu threshold)crop (
bool, default:True) – Automatically crop image to contentresolution (
Sequence[float] |None, default:None) – Spatial resolution in XYZ (micrometers). Auto-detected if Noneclahe_window (
Sequence[int] |None, default:None) – CLAHE window size. Auto-calculated if Noneclahe_clip_limit (
int|None, default:None) – CLAHE clip limit. Auto-calculated if Nonegaussian_sigma (
list[float] |None, default:None) – Gaussian filter sigma. Auto-calculated if Nonegaussian_iterations (
int, default:5) – Number of Gaussian smoothing iterationsfill_slices (
bool, default:True) – Fill holes in XY sliceschan_vese_lambda1 (
float, default:1) – Morphological Chan-Vese lambda1 parameterchan_vese_lambda2 (
float, default:1) – Morphological Chan-Vese lambda2 parameterregister_stack (
bool, default:True) – Apply stack registration for slice alignmenttarget_resolution (
list[float] |None, default:None) – Target resolution for resamplingfill_inland_threshold (
float|None, default:None) – Distance threshold for filling inland regionsreturn_resolution (
bool, default:False) – Return resolution along with contourverbose (
bool, default:True) – Print processing progress
- Return type:
ndarray[tuple[Any,...],dtype[bool]] |tuple[ndarray[tuple[Any,...],dtype[bool]],Sequence[float]]- Returns:
Binary contour array, and optionally resolution tuple
Mesh Creation¶
- create_mesh(contour, resolution=None, step_size=1)[source]¶
Generate mesh from binary 3D contour using marching cubes.
- Parameters:
- Return type:
- Returns:
PhenoMesh mesh
Mesh Smoothing¶
Mesh Remeshing¶
- remesh(mesh, n_clusters, subdivisions=3)[source]¶
Regularise the mesh faces using the ACVD algorithm.
Mesh Repair¶
- repair(mesh)[source]¶
Repair a mesh using MeshFix.
- Return type:
PolyData- Parameters:
mesh (PolyData)
Mesh Filtering¶
- filter_by_curvature(mesh, curvature_threshold, curvatures=None)[source]¶
Remove mesh vertices outside curvature threshold range.
- Parameters:
mesh (
PolyData) – PyVista PolyData meshcurvature_threshold (
tuple[float,float] |float) – Tuple (min, max) defining valid curvature range, or single value for symmetric rangecurvatures (
ndarray[tuple[Any,...],dtype[floating[Any]]] |None, default:None) – Optional curvature array. If None, computes mean curvature
- Return type:
PolyData- Returns:
Filtered PyVista PolyData mesh
- remove_by_normals(mesh, threshold_angle=0, flip=False, angle='polar')[source]¶
Remove points based on the point normal angle.
- Parameters:
mesh (
PolyData) – Mesh on which to operate.threshold_angle (
float, default:0) – Threshold for the polar angle (theta). Values smaller than this will be removed. Default = 0.flip (
bool, default:False) – Flag to flip normal orientation. Default = False.angle (
str, default:'polar') – Type of angle to use (‘polar’ or ‘azimuth’).
- Return type:
PolyData- Returns:
Mesh with the resulting vertices removed.
- remove_bridges(mesh, verbose=True)[source]¶
Remove triangles where all vertices are part of the mesh boundary.
- Parameters:
mesh (
PolyData) – Mesh to operate on.verbose (
bool, default:True) – Flag to print processing steps.
- Return type:
PolyData- Returns:
Mesh after bridge removal.
- remove_tongues(mesh, radius, threshold=6, hole_edges=100, verbose=True)[source]¶
Remove “tongues” in mesh.
All boundary points within a given radius are considered. The ones where the fraction of the distance along the boundary, as divided by the euclidean distance, is greater than the given threshold.
- Parameters:
mesh (
PolyData) – Mesh to operate on.radius (
float) – Radius for boundary point neighbourhood.threshold (
float, default:6) – Threshold for fraction between boundary distance and euclidean distance.hole_edges (
int, default:100) – Size of holes to fill after tongue removal.verbose (
bool, default:True) – Print processing steps.
- Return type:
PolyData- Returns:
Resulting mesh.
- remove_inland_under(mesh, contour, threshold, resolution=None, invert=False)[source]¶
Remove the part of the mesh that is under the contour.
- Parameters:
mesh (
PolyData) – Mesh to remove the inland part from.contour (
ndarray[tuple[Any,...],dtype[bool]]) – Contour to use for the removal.threshold (
int) – Threshold distance from the contour XY periphery.resolution (
list[float] |None, default:None) – Resolution of the image. Default is [1, 1, 1].invert (
bool, default:False) – Invert the mesh normals. Default is False.
- Return type:
PolyData- Returns:
Mesh with the inland part removed.
Mesh Processing¶
- process(mesh, hole_repair_threshold=100, downscaling=0.01, upscaling=2, threshold_angle=60, top_cut='center', tongues_radius=None, tongues_ratio=4, smooth_iterations=200, smooth_relaxation=0.01, curvature_threshold=0.4, inland_threshold=None, contour=None)[source]¶
Convenience function for postprocessing a mesh.
- Parameters:
mesh (
PolyData) – Mesh to process.hole_repair_threshold (
int, default:100) – Threshold for the hole repair algorithm.downscaling (
float, default:0.01) – Downscaling factor for the mesh.upscaling (
float, default:2) – Upscaling factor for the mesh.threshold_angle (
float, default:60) – Threshold for the polar angle (theta).top_cut (
str|tuple[float,float,float], default:'center') – Top cut location.tongues_radius (
float|None, default:None) – Radius of the tongues.tongues_ratio (
float, default:4) – Ratio of the tongues.smooth_iterations (
int, default:200) – Number of smoothing iterations.smooth_relaxation (
float, default:0.01) – Smoothing relaxation factor.curvature_threshold (
float, default:0.4) – Threshold for the curvature.inland_threshold (
float|None, default:None) – Threshold for the inland removal.contour (
ndarray[tuple[Any,...],dtype[bool]] |None, default:None) – Contour to use for the inland removal.
- Return type:
PolyData- Returns:
Processed mesh.
- ecft(mesh, hole_edges=300, inplace=False)¶
Perform ExtractLargest, Clean, FillHoles, and TriFilter operations.
- erode(mesh, iterations=1)[source]¶
Erode the mesh by removing boundary points iteratively.
- Return type:
PolyData- Parameters:
mesh (PolyData)
iterations (int)
Mesh Analysis¶
- fit_paraboloid_mesh(mesh, return_coord=False)[source]¶
Fit a paraboloid to a mesh.
- Parameters:
mesh (
PolyData) – Mesh to fit paraboloid to.return_coord (
bool, default:False) – If True, return apex coordinates as well.
- Return type:
ndarray[tuple[Any,...],dtype[floating[Any]]] |tuple[ndarray[tuple[Any,...],dtype[floating[Any]]],ndarray[tuple[Any,...],dtype[floating[Any]]]]- Returns:
Parameters for the paraboloid, and optionally the apex coordinates.
- correct_normal_orientation(mesh, relative='x', inplace=False)[source]¶
Correct the orientation of the normals of a mesh.
- correct_bad_mesh(mesh, verbose=True)[source]¶
Correct a bad (non-manifold) mesh.
- Parameters:
mesh (
PolyData) – Input mesh.verbose (
bool, default:True) – Flag to print out operation procedure.
- Return type:
PolyData- Returns:
Corrected mesh.
Vertex Operations¶
- get_vertex_neighbors(mesh, index, include_self=True)[source]¶
Get the indices of the vertices connected to a given vertex.
Edge Operations¶
- get_boundary_edges(mesh)[source]¶
Get boundary edges.
- Return type:
PolyData- Parameters:
mesh (PolyData)
- get_feature_edges(mesh, angle=30)[source]¶
Get feature edges defined by given angle.
- Return type:
PolyData- Parameters:
mesh (PolyData)
angle (float)
Labeling¶
- label_from_image(mesh, segm_img, resolution=None, background=0, mode='point', inplace=False)[source]¶
Label mesh vertices or faces using nearest voxel in segmented image.
- Parameters:
mesh (
PolyData) – PyVista PolyData mesh to labelsegm_img (
ndarray[tuple[Any,...],dtype[integer[Any]]]) – 3D segmented image with integer labelsresolution (
list[float] |None, default:None) – Spatial resolution of segmented imagebackground (
int, default:0) – Background label value to ignoremode (
str, default:'point') – Labeling mode (‘point’ for vertices, ‘face’ for cell centers)inplace (
bool, default:False) – Modify mesh in place
- Return type:
- Returns:
Label array or None if inplace=True
- project_to_surface(mesh, int_img, distance_threshold, mask=None, resolution=None, fct=<function sum>, background=0)[source]¶
Project image intensity values onto mesh surface.
Aggregates intensity values within distance threshold of each vertex.
- Parameters:
mesh (
PolyData) – PyVista PolyData meshint_img (
ndarray[tuple[Any,...],dtype[Any]]) – 3D intensity image arraydistance_threshold (
float) – Maximum distance from surface for projectionmask (
ndarray[tuple[Any,...],dtype[bool]] |None, default:None) – Optional mask array for imageresolution (
list[float] |None, default:None) – Spatial resolution of intensity imagefct (
Callable[...,Any], default:<function sum at 0x7fdc25a994b0>) – Aggregation function (currently only np.sum supported)background (
float, default:0) – Background value to ignore in image
- Return type:
- Returns:
Array of projected values, one per mesh vertex