CorpusCallosum.segmentation.segmentation_postprocessing

CorpusCallosum.segmentation.segmentation_postprocessing.clean_cc_segmentation(seg_arr, max_connection_distance=3.0)[source]

Clean corpus callosum segmentation by removing non-connected components.

Parameters:
seg_arrnpt.NDArray[int]

Input segmentation array with CC (192) and fornix (250) labels.

max_connection_distancefloat, default=3.0

Maximum distance to connect components.

Returns:
clean_segnp.NDArray[int]

Cleaned segmentation array with only the largest connected component of CC and fornix.

masknpt.NDArray[bool]

Binary mask of the largest connected component.

CorpusCallosum.segmentation.segmentation_postprocessing.connect_nearby_components(seg_arr, max_connection_distance=3.0, plot=False)[source]

Connect nearby disconnected components that should be connected.

This function identifies disconnected components in the segmentation and creates minimal connections between components that are close to each other.

Parameters:
seg_arrnp.ndarray

Input binary segmentation array.

max_connection_distancefloat, default=3.0

Maximum distance to connect components.

plotbool, default=False

Whether to plot the segmentation with connected components.

Returns:
np.ndarray

Segmentation array with minimal connections added between nearby components.

Notes

The function: 1. Identifies connected components in the input segmentation 2. Finds boundaries between components 3. Creates minimal connections between nearby components 4. Returns the modified segmentation with added connections

CorpusCallosum.segmentation.segmentation_postprocessing.create_connection_line(point1, point2)[source]

Create a line of voxels connecting two points.

Uses a simplified 3D line algorithm to create a sequence of voxels that form a continuous path between the two points.

Parameters:
point1np.ndarray

Starting point coordinates, shape (3,).

point2np.ndarray

Ending point coordinates, shape (3,).

Returns:
list of int triplets

List of (x, y, z) coordinates forming the connection line.

Notes

The line is created by interpolating between the points using the maximum distance in any dimension as the number of steps.

CorpusCallosum.segmentation.segmentation_postprocessing.extract_largest_connected_component(seg_arr, max_connection_distance=3.0)[source]

Get the largest connected component from a binary segmentation array.

Parameters:
seg_arrnp.ndarray

Input binary segmentation array.

max_connection_distancefloat, optional

Maximum distance to connect components, by default 3.0.

Returns:
np.ndarray

Binary mask of the largest connected component.

Notes

The function first attempts to connect nearby disconnected components that should be connected, then finds the largest connected component. It uses minimal connections between close components before falling back to dilation if no connections are made.

CorpusCallosum.segmentation.segmentation_postprocessing.find_component_boundaries(labels_arr, component_id)[source]

Find boundary voxels of a connected component.

Parameters:
labels_arrnp.ndarray

Labeled array from connected components analysis.

component_idint

ID of the component to find boundaries for.

Returns:
np.ndarray

Array of shape (N, 3) containing boundary coordinates.

Notes

Uses 6-connectivity (face neighbors only) to determine boundaries. Boundary voxels are those that are part of the component but have at least one non-component neighbor.

CorpusCallosum.segmentation.segmentation_postprocessing.find_minimal_connection_path(boundary_coords1, boundary_coords2, max_distance=3.0)[source]

Find the minimal connection path between two component boundaries.

Parameters:
boundary_coords1np.ndarray

Boundary coordinates of first component, shape (N1, 3).

boundary_coords2np.ndarray

Boundary coordinates of second component, shape (N2, 3).

max_distancefloat, default=3.0

Maximum distance to consider for connection, by default 3.0.

Returns:
tuple[np.ndarray, np.ndarray] or None

If a valid connection is found:

  • point1 : Coordinates on first boundary

  • point2 : Coordinates on second boundary

None if no connection within max_distance is found.

Notes

Uses Euclidean distance to find the closest pair of points between the two boundaries.

CorpusCallosum.segmentation.segmentation_postprocessing.get_cc_volume_voxel(desired_width_mm, cc_mask, voxel_size)[source]

Calculate the volume of the corpus callosum in cubic millimeters.

This function calculates the volume of the corpus callosum (CC) in cubic millimeters. If the CC width is larger than desired_width_mm, the voxels on the edges are calculated as partial volumes to achieve the desired width.

Parameters:
desired_width_mmint

Desired width of the CC in millimeters.

cc_masknp.ndarray

Binary mask of the corpus callosum in LIA orientation.

voxel_sizetriplet of floats

LIA-oriented Voxel size in millimeters (x, y, z).

Returns:
float

Volume of the CC in cubic millimeters.

Raises:
ValueError

If CC width is smaller than desired width

AssertionError

If CC mask doesn’t have odd number of voxels in x dimension

Notes

The function assumes LIA orientation