topostats.tracing.skeletonize#
Skeletonize molecules.
Attributes#
Classes#
Class skeletonising images. |
|
Skeletonise a binary array following Zhang's algorithm (Zhang and Suen, 1984). |
Module Contents#
- topostats.tracing.skeletonize.LOGGER#
- class topostats.tracing.skeletonize.getSkeleton(image: numpy.typing.NDArray, mask: numpy.typing.NDArray, method: str = 'zhang', height_bias: float = 0.6)[source]#
Class skeletonising images.
- Parameters:
image (npt.NDArray) – Image used to generate the mask.
mask (npt.NDArray) – Binary mask of features.
method (str) – Method for skeletonizing. Options ‘zhang’ (default), ‘lee’, ‘medial_axis’, ‘thin’ and ‘topostats’.
height_bias (float) – Ratio of lowest intensity (height) pixels to total pixels fitting the skeletonisation criteria. 1 is all pixels smiilar to Zhang.
- image#
- mask#
- method = 'zhang'#
- height_bias = 0.6#
- get_skeleton() numpy.typing.NDArray [source]#
Skeletonise molecules.
- Returns:
Skeletonised version of the binary mask (possibly using criteria from the image).
- Return type:
npt.NDArray
- _get_skeletonize() collections.abc.Callable [source]#
Determine which skeletonise method to use.
- Returns:
Returns the function appropriate for the required skeletonizing method.
- Return type:
Callable
- static _skeletonize_zhang(mask: numpy.typing.NDArray) numpy.typing.NDArray [source]#
Use scikit-image implementation of the Zhang skeletonisation method.
- Parameters:
mask (npt.NDArray) – Binary array to skeletonise.
- Returns:
Mask array reduced to a single pixel thickness.
- Return type:
npt.NDArray
- static _skeletonize_lee(mask: numpy.typing.NDArray) numpy.typing.NDArray [source]#
Use scikit-image implementation of the Lee skeletonisation method.
- Parameters:
mask (npt.NDArray) – Binary array to skeletonise.
- Returns:
Mask array reduced to a single pixel thickness.
- Return type:
npt.NDArray
- static _skeletonize_medial_axis(mask: numpy.typing.NDArray) numpy.typing.NDArray [source]#
Use scikit-image implementation of the Medial axis skeletonisation method.
- Parameters:
mask (npt.NDArray) – Binary array to skeletonise.
- Returns:
Mask array reduced to a single pixel thickness.
- Return type:
npt.NDArray
- static _skeletonize_thin(mask: numpy.typing.NDArray) numpy.typing.NDArray [source]#
Use scikit-image implementation of the thinning skeletonisation method.
- Parameters:
mask (npt.NDArray) – Binary array to skeletonise.
- Returns:
Mask array reduced to a single pixel thickness.
- Return type:
npt.NDArray
- static _skeletonize_topostats(image: numpy.typing.NDArray, mask: numpy.typing.NDArray, height_bias: float = 0.6) numpy.typing.NDArray [source]#
Use scikit-image implementation of the Zhang skeletonisation method.
This method is based on Zhang’s method but produces different results (less branches but slightly less accurate).
- Parameters:
image (npt.NDArray) – Original image with heights.
mask (npt.NDArray) – Binary array to skeletonise.
height_bias (float) – Ratio of lowest intensity (height) pixels to total pixels fitting the skeletonisation criteria. 1 is all pixels smiilar to Zhang.
- Returns:
Masked array reduced to a single pixel thickness.
- Return type:
npt.NDArray
- class topostats.tracing.skeletonize.topostatsSkeletonize(image: numpy.typing.NDArray, mask: numpy.typing.NDArray, height_bias: float = 0.6)[source]#
Skeletonise a binary array following Zhang’s algorithm (Zhang and Suen, 1984).
Modifications are made to the published algorithm during the removal step to remove a fraction of the smallest pixel values opposed to all of them in the aforementioned algorithm. All operations are performed on the mask entered.
- Parameters:
image (npt.NDArray) – Original 2D image containing the height data.
mask (npt.NDArray) – Binary image containing the object to be skeletonised. Dimensions should match those of ‘image’.
height_bias (float) – Ratio of lowest intensity (height) pixels to total pixels fitting the skeletonisation criteria. 1 is all pixels smiilar to Zhang.
- image#
- mask#
- height_bias = 0.6#
- skeleton_converged = False#
- p2 = None#
- p3 = None#
- p4 = None#
- p5 = None#
- p6 = None#
- p7 = None#
- p8 = None#
- p9 = None#
- counter = 0#
- do_skeletonising() numpy.typing.NDArray [source]#
Perform skeletonisation.
- Returns:
The single pixel thick, skeletonised array.
- Return type:
npt.NDArray
- _do_skeletonising_iteration() None [source]#
Obtain the local binary pixel environment and assess the local pixel values.
This determines whether to delete a point according to the Zhang algorithm.
Then removes ratio of lowest intensity (height) pixels to total pixels fitting the skeletonisation criteria. 1 is all pixels smiilar to Zhang.
- _delete_pixel_subit1(point: list) bool [source]#
Check whether a single point (P1) should be deleted based on its local binary environment.
2 ≤ B(P1) ≤ 6, where B(P1) is the number of non-zero neighbours of P1.
(b) A(P1) = 1, where A(P1) is the # of 01’s around P1. (C) P2 * P4 * P6 = 0 (d) P4 * P6 * P8 = 0
- Parameters:
point (list) – List of [x, y] coordinate positions.
- Returns:
Indicates whether to delete depending on whether the surrounding points have met the criteria of the binary thin a, b returncount, c and d checks below.
- Return type:
bool
- _delete_pixel_subit2(point: list) bool [source]#
Check whether a single point (P1) should be deleted based on its local binary environment.
2 ≤ B(P1) ≤ 6, where B(P1) is the number of non-zero neighbours of P1.
(b) A(P1) = 1, where A(P1) is the # of 01’s around P1. (c’) P2 * P4 * P8 = 0 (d’) P2 * P6 * P8 = 0
- Parameters:
point (list) – List of [x, y] coordinate positions.
- Returns:
Whether surrounding points have met the criteria of the binary thin a, b returncount, csharp and dsharp checks below.
- Return type:
bool
- _binary_thin_check_a() bool [source]#
Check the surrounding area to see if the point lies on the edge of the grain.
Condition A protects the endpoints (which will be < 2)
- Returns:
If point lies on edge of graph and isn’t an endpoint.
- Return type:
bool
- _binary_thin_check_b_returncount() int [source]#
Count local area 01’s in order around P1.
??? What does this mean?
- Returns:
The number of 01’s around P1.
- Return type:
int
- _binary_thin_check_c() bool [source]#
Check if p2, p4 or p6 is 0.
- Returns:
If p2, p4 or p6 is 0.
- Return type:
bool
- _binary_thin_check_d() bool [source]#
Check if p4, p6 or p8 is 0.
- Returns:
If p4, p6 or p8 is 0.
- Return type:
bool
- _binary_thin_check_csharp() bool [source]#
Check if p2, p4 or p8 is 0.
- Returns:
If p2, p4 or p8 is 0.
- Return type:
bool
- _binary_thin_check_dsharp() bool [source]#
Check if p2, p6 or p8 is 0.
- Returns:
If p2, p6 or p8 is 0.
- Return type:
bool
- final_skeletonisation_iteration() None [source]#
Remove “hanging” pixels.
- Examples of such pixels are:
[0, 0, 0] [0, 1, 0] [0, 0, 0] [0, 1, 1] [0, 1, 1] [0, 1, 1]
case 1: [0, 1, 0] or case 2: [0, 1, 0] or case 3: [1, 1, 0]
This is useful for the future functions that rely on local pixel environment to make assessments about the overall shape/structure of traces.
- _binary_final_thin_check_a() bool [source]#
Assess if local area has 4-connectivity.
- Returns:
Logical indicator of whether if any neighbours of the 4-connections have a near pixel.
- Return type:
bool
- _binary_final_thin_check_b() bool [source]#
Assess if local area 4-connectivity is connected to multiple branches.
- Returns:
Logical indicator of whether if any neighbours of the 4-connections have a near pixel.
- Return type:
bool
- binary_thin_check_diag() bool [source]#
Check if opposite corner diagonals are present.
- Returns:
Whether a diagonal exists.
- Return type:
bool
- static get_local_pixels_binary(binary_map: numpy.typing.NDArray, x: int, y: int) numpy.typing.NDArray [source]#
Value of pixels in the local 8-connectivity area around the coordinate (P1) described by x and y.
P1 must not lie on the edge of the binary map.
- [[p7, p8, p9], [[0,1,2],
[p6, P1, p2], -> [3,4,5], -> [0,1,2,3,5,6,7,8] [p5, p4, p3]] [6,7,8]]
delete P1 to only get local area.
- Parameters:
binary_map (npt.NDArray) – Binary mask of image.
x (int) – X coordinate within the binary map.
y (int) – Y coordinate within the binary map.
- Returns:
Flattened 8-long array describing the values in the binary map around the x,y point.
- Return type:
npt.NDArray
- static sort_and_shuffle(arr: numpy.typing.NDArray, seed: int = 23790101) tuple[numpy.typing.NDArray, numpy.typing.NDArray] [source]#
Sort array in ascending order and shuffle the order of identical values are the same.
- Parameters:
arr (npt.NDArray) – A flattened (1D) array.
seed (int) – Seed for random number generator.
- Returns:
npt.NDArray – An ascending order array where identical value orders are also shuffled.
npt.NDArray – An ascending order index array of above where identical value orders are also shuffled.