DIST -> Utils
Build point cloud from image
- class tardis_em.dist_pytorch.utils.build_point_cloud.BuildPointCloud
MAIN MODULE FOR SEMANTIC MASK IMAGE DATA TRANSFORMATION INTO POINT CLOUD
Module build point cloud from semantic mask based on skeletonization in 3D. Optionally, user can trigger point cloud correction with Euclidean distance transformation to correct for skeletonization artefact. It will not fix issue with heavily overlapping objects.
- The workflows follows: optional(edt_2d -> edt_thresholds -> edt_binary) ->
skeletonization_3d -> output point cloud -> (down-sampling)
- static check_data(image: str | ndarray) ndarray | None
Check image data and correct it if needed to uint8 type.
- Parameters:
image (np.ndarray, str) – 2D/3D image array
- Returns:
Check and converted to uint8 data
- Return type:
np.ndarray
- build_point_cloud(image: str | ndarray, down_sampling: float | None = None, as_2d=False) Tuple[ndarray, ndarray] | ndarray
Build point cloud data from semantic mask.
- Parameters:
image (np.ndarray) – Predicted semantic mask.
down_sampling (float, None) – If not None, down-sample point cloud with open3d.
as_2d – Treat data as 2D not 3D.
- Returns:
Point cloud of 2D/3D semantic objects.
- Return type:
Union[Tuple[ndarray, ndarray], np.ndarray]
- tardis_em.dist_pytorch.utils.build_point_cloud.draw_line(p0: ndarray, p1: ndarray, line_id: int) ndarray
Draws a straight line in 3D space between two points.
Args: p0 (tuple): (z0, y0, x0) representing the start point of the line. p1 (tuple): (z1, y1, x1) representing the end point of the line. line_id (int): An identifier for the line.
- Returns:
A numpy array containing points (line_id, x, y, z) along the line.
- Return type:
np.ndarray
- tardis_em.dist_pytorch.utils.build_point_cloud.quadratic_bezier(p0: ndarray, p1: ndarray, p2: ndarray, t: float) list
Calculate a point on a quadratic Bézier curve.
- Parameters:
p0 (np.ndarray) – (z0, y0, x0) representing the start point of the Bézier curve.
p1 (np.ndarray) – (z1, y1, x1) representing the control point of the Bézier curve.
p2 (np.ndarray) – (z2, y2, x2) representing the end point of the Bézier curve.
t (float) – A float between 0 and 1, representing the parameter of the curve.
- Returns:
A list of integers [x, y, z], representing the calculated point on the curve.
- Return type:
list
- tardis_em.dist_pytorch.utils.build_point_cloud.draw_curved_line(p0: ndarray, p1: ndarray, p2: ndarray, line_id: int) ndarray
Draw a quadratic Bézier curve in a 3D array.
- Parameters:
p0 (np.ndarray) – (z0, y0, x0) representing the start point of the curve.
p1 (np.ndarray) – (z1, y1, x1) representing the control point of the curve.
p2 (np.ndarray) – (z2, y2, x2) representing the end point of the curve.
line_id (int) – An identifier for the curve.
- Returns:
A numpy array containing points [line_id, z, y, x] along the curve.
- Return type:
np.ndarray
- tardis_em.dist_pytorch.utils.build_point_cloud.draw_circle(center: ndarray, radius: float, circle_id: int, _3d=False, size=None) ndarray
Draw a circle in a 3D space.
- Parameters:
center (np.ndarray) – (z0, y0, x0) representing the center of the circle.
radius (float) – The radius of the circle.
circle_id (int) – An identifier for the circle.
_3d (bool) – True if 3D object.
size – Size of drawn mask
- Returns:
A numpy array containing points [circle_id, z, y, x] on the circle.
- Return type:
np.ndarray
- tardis_em.dist_pytorch.utils.build_point_cloud.draw_sphere(center, radius, sheet_id)
- tardis_em.dist_pytorch.utils.build_point_cloud.draw_sheet(center: ndarray, size: tuple, sheet_id: int) ndarray
Generate n points on a 3D sheet.
- Parameters:
center (np.ndarray) – (z0, y0, x0) representing the center of the circle.
size (tuple) – Max size to fit sheet.
sheet_id (int) – Sheet unique id.
- Returns:
A numpy array containing points [x, y, z] on the sheet.
- Return type:
np.ndarray
- tardis_em.dist_pytorch.utils.build_point_cloud.create_simulated_dataset(size, sim_type: str)
Segment point cloud from graph
- class tardis_em.dist_pytorch.utils.segment_point_cloud.PropGreedyGraphCut(threshold=<class 'float'>, connection=2, smooth=False)
PROBABILITY DRIVEN GREEDY GRAPH CUT
Perform graph cut on predicted point cloud graph representation using in-coming and out-coming edges probability.
- Parameters:
threshold (float) – Edge connection threshold.
connection (int) – Max allowed number of connections per node.
smooth (bool) – If True, smooth splines.
- preprocess_connections(adj_matrix)
Preprocess the adjacency matrix to first ensure mutual connections and then limit each node to its top 8 connections based on connection probability.
- patch_to_segment(graph: list, coord: ndarray | list, idx: list, prune: int, sort=True, visualize: str | None = None) ndarray
Point cloud instance segmentation from graph representation
From each point cloud (patch) first build adjacency matrix. Matrix is then used to iteratively search for new segments. For each initial node algorithm search for 2 edges with the highest prop. with given threshold. For each found edges, algorithm check if found node creates an edge with previous node within 2 highest probability. If not alg. search for new edge that fulfill the statement.
- Parameters:
graph (list) – Graph patch output from Dist.
coord (np.ndarray) – Coordinates for each unsorted point idx.
idx (list) – idx of point included in the segment.
prune (int) – Remove splines composed of number of node given by prune.
sort (bool) – If True sort output.
visualize (str, None) – If not None, visualize output with open3D.
General Utils
- tardis_em.dist_pytorch.utils.utils.pc_median_dist(pc: ndarray, avg_over=False, box_size=0.15) float
Calculate the median distance between KNN points in the point cloud.
- Parameters:
pc (np.ndarray) – 2D/3D array of the point clouds.
avg_over (bool) – If True, calculate the median position of all points and calculate average k-NN for a selected set of points in that area (speed up for big point clouds).
box_size (float) – Boundary box size for ‘avg_over’.
- Returns:
Median distance between points in given point cloud.
- Return type:
float
- tardis_em.dist_pytorch.utils.utils.point_in_bb(points: ndarray, min_x: int, max_x: int, min_y: int, max_y: int, min_z: float32 | None = None, max_z: float32 | None = None) ndarray
Compute a bounding_box filter on the given points
- Parameters:
points (np.ndarray) – (n,3) array.
min_i (int) – The bounding box limits for each coordinate. If some limits are missing, the default values are - infinite for the min_i and infinite for the max_i.
max_i (int) – The bounding box limits for each coordinate. If some limits are missing, the default values are - infinite for the min_i and infinite for the max_i.
- Returns:
- The boolean mask indicates wherever a point should be
kept or not.
- Return type:
np.ndarray[bool]
- class tardis_em.dist_pytorch.utils.utils.DownSampling(voxel=None, threshold=None, labels=True, KNN=False)
Base down sampling wrapper
- static pc_down_sample(coord: ndarray, sampling, rgb=None) Tuple[ndarray, ndarray] | ndarray
- class tardis_em.dist_pytorch.utils.utils.VoxelDownSampling(**kwargs)
Wrapper for down sampling of the point cloud using voxel grid (Based on Open3d library)
- pc_down_sample(coord: ndarray, sampling: float, rgb: ndarray | None = None) Tuple[ndarray, ndarray] | ndarray
This function takes a set of 3D points and a voxel size and returns the centroids of the voxels in which the points are located.
- Parameters:
coord (np.ndarray) – A numpy array of shape (N, 3) containing the 3D coordinates
points. (of the input)
rgb (np.ndarray) – A numpy array of shape (N, 3) containing RGB values of each point.
sampling (float) – The size of each voxel in each dimension.
- Returns:
A numpy array of shape (M, 3) containing the centroids of the voxels in which the points are located where M is the number of unique voxels.
- Return type:
voxel_centers (np.ndarray)
- class tardis_em.dist_pytorch.utils.utils.RandomDownSampling(**kwargs)
Wrapper for random sampling of the point cloud
- static pc_down_sample(coord: ndarray, sampling, rgb: ndarray | None = None) Tuple[ndarray, ndarray] | ndarray
Random picked point to down sample point cloud. Return correctly preserve ID class.
- Parameters:
coord – Array of point to downs-sample
rgb – Extra node feature like e.g. RGB values do sample with coord.
sampling – Lambda function to calculate down-sampling rate or fixed float ratio.
- Returns:
Down-sample array of points.
- Return type:
random_sample (np.ndarray)
- tardis_em.dist_pytorch.utils.utils.check_model_dict(model_dict: dict) dict
Check and rebuild model structure dictionary to ensure back-compatibility.
- Parameters:
model_dict (dict) – Model structure dictionary.
- Returns:
Standardize model structure dictionary.
- Return type:
dict