Shape Analysis#
The functions used to calculate morphological parameters of the bacteria.
extend_medial_axis(medial_axis: np.array, boundary: np.array, max_iter: int = 50, error_threshold: float = 0.05, min_distance_to_boundary: int = 1, step_size: float or int = 1) -> np.array
#
Extend the medial axis to the boundary, using the boundary as a reference, then smooth it to get the final result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
medial_axis
|
np.array
|
The medial axis. |
required |
boundary
|
np.array
|
The boundary. |
required |
max_iter
|
int
|
The maximum number of iterations for extending the medial axis(default is 50). |
50
|
min_distance_to_boundary
|
int
|
The minimum distance to the boundary (default is 1). This determines when the extension stops. |
1
|
step_size
|
float or int
|
The step size to take when extending the medial axis (default is 1 px). |
1
|
Returns:
| Name | Type | Description |
|---|---|---|
extended_medial_axis |
np.array
|
The extended and smoothed medial axis. |
Source code in src/micromorph/bacteria/shape_analysis.py
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 | |
extend_medial_axis_roughly(medax, bnd, max_iter=500, min_distance_to_boundary=1, step_size=1)
#
Extend the medial axis to the boundary, without any smoothing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
medax
|
np.array
|
The medial axis. |
required |
bnd
|
np.array
|
The boundary. |
required |
max_iter
|
int
|
The maximum number of iterations (default is 50). |
500
|
min_distance_to_boundary
|
int
|
The minimum distance to the boundary (default is 1). |
1
|
step_size
|
int
|
The step size to take (default is 1). |
1
|
Returns:
| Name | Type | Description |
|---|---|---|
extended_medial_axis |
np.array
|
The extended medial axis. |
Source code in src/micromorph/bacteria/shape_analysis.py
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 | |
get_bacteria_boundary(mask: np.array, boundary_smoothing_factor: int or None = 7) -> np.array
#
Get the (smoothed) boundary of a segmented bacterium.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
np.array
|
The binary image (with a single bacterium only). |
required |
boundary_smoothing_factor
|
int or None
|
The number of frequencies to use for FFT smoothing (default is 7). |
7
|
Returns:
| Name | Type | Description |
|---|---|---|
boundary |
np.array
|
XY coordinates of the boundary (smoothed). |
Source code in src/micromorph/bacteria/shape_analysis.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
get_bacteria_length(medial_axis_extended: np.array, pxsize: float or int = 1.0) -> float
#
Calculate the length of a segmented bacterium.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
medial_axis_extended
|
np.array
|
The extended medial axis of the bacterium, whose arclength is calculated. |
required |
pxsize
|
float
|
The pixel size of the image. The final distance will be multiplied by this value (default is 1.0). |
1.0
|
Returns:
| Name | Type | Description |
|---|---|---|
total_distance |
float
|
Total distance covered by the extended medial axis (in px). |
Source code in src/micromorph/bacteria/shape_analysis.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
get_bacteria_widths(img: np.array, med_ax: np.array, n_lines: int = 5, pxsize: float or int = 1.0, psfFWHM: float or int = 0.25, fit_type: str or None = None, line_magnitude: float or int = 20) -> np.array
#
Calculate the width of a segmented bacterium.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
np.array
|
The original image. |
required |
med_ax
|
np.array
|
The medial axis which will be used to find profile lines to fit. |
required |
n_lines
|
int
|
The number of lines to fit (default is 5). |
5
|
pxsize
|
float or int
|
The pixel size of the image (default is 1.0). |
1.0
|
psfFWHM
|
float or int
|
The FWHM of the PSF (default is 5). |
0.25
|
fit_type
|
str or None
|
The type of fit to use (default is None). |
None
|
line_magnitude
|
float or int
|
The length of the line used to measure the profile (default is 20). |
20
|
Returns:
| Name | Type | Description |
|---|---|---|
all_widths |
np.array
|
Array of widths. |
Source code in src/micromorph/bacteria/shape_analysis.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |
get_medial_axis(mask: np.array) -> np.array
#
Get the medial axis of a segmented bacterium.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
np.array
|
The binary image (with a single bacterium only). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
medial_axis |
np.array
|
The medial axis of the bacterium. |
Source code in src/micromorph/bacteria/shape_analysis.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |
smooth_medial_axis(medial_axis: np.array, boundary: np.array, error_threshold: float = 0.05, max_iter: int = 50, spline_val: int = 1, spline_spacing=0.25) -> np.array
#
Smooth the medial axis of a segmented bacterium, using the boundary as a reference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
medial_axis
|
np.array
|
The medial axis of the bacterium. |
required |
boundary
|
np.array
|
The boundary of the bacterium. |
required |
error_threshold
|
float
|
The threshold for the error in the smoothing (default is 0.05). |
0.05
|
max_iter
|
int
|
The maximum number of iterations for the smoothing (default is 50). |
50
|
Returns:
| Name | Type | Description |
|---|---|---|
smoothed_medial_axis |
np.array
|
The smoothed medial axis. |
Source code in src/micromorph/bacteria/shape_analysis.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 | |
Utility Functions#
Collection of utility functions used to calculate morphological parameters of the bacteria, mostly to do with binary image processing.
apply_mask_to_image(img: np.array, mask: np.array, method: str = 'min') -> np.array
#
A function to apply a dilation to a mask, and then apply the mask to the image. This makes all the points outside the region of interest black.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
np.array
|
The image to be masked |
required |
mask
|
np.array
|
The mask to be applied to the image |
required |
Returns:
| Name | Type | Description |
|---|---|---|
img_masked |
np.array
|
The masked image |
Source code in src/micromorph/bacteria/utilities.py
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 | |
calculate_distance_along_line(coordinates: np.array) -> np.array
#
Function to calculate the distance along a line defined by a set of coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coordinates
|
np.array
|
An np.array with 2 columns and N rows (N being the number of points in the line). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
distances_cumulative |
np.array
|
An np.array with 1 column and N rows (N being the number of points in the line). |
Source code in src/micromorph/bacteria/utilities.py
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 | |
find_branchpoints(skeleton: np.array) -> np.array
#
Function to find the branchpoints of a binary image containing a skeleton.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
skeleton
|
np.array
|
The skeletonised binary image |
required |
Returns:
| Name | Type | Description |
|---|---|---|
branch_points |
np.array
|
A binary image with only the branchpoints of the skeleton |
Source code in src/micromorph/bacteria/utilities.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
find_closest_boundary_to_axis(medial_axis_coords: np.array, boundary_coords: np.array, start_position: int) -> np.array
#
A function to find the closest point on the boundary to the medial axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
medial_axis_coords
|
np.array
|
An np.array with 2 columns and N rows (N being the number of points in the medial axis). |
required |
boundary_coords
|
np.array
|
An np.array with 2 columns and M rows (M being the number of points in the boundary). |
required |
start_position
|
int
|
Either 1 or -1. If 1, the medial axis will be ordered from the first point to the last. If -1, the medial axis will be ordered from the last point to the first. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
closest_on_boundary |
np.array
|
An np.array with 2 columns and 1 row, the coordinates of the closest point on the boundary. |
Source code in src/micromorph/bacteria/utilities.py
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | |
find_closest_point(point: np.array, candidates: np.array) -> tuple[np.array, int]
#
Find the closest point to a given point, from a list of candidates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
point
|
np.array
|
A np.array with 2 columns and 1 row (x, y) |
required |
candidates
|
np.array
|
An np.array with 2 columns and N rows (N being the number of candidates) |
required |
Returns:
| Name | Type | Description |
|---|---|---|
closest_point |
np.array
|
The closest point |
min_index |
int
|
The index of the closest point in the candidates array |
Source code in src/micromorph/bacteria/utilities.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | |
find_endpoints(img: np.array) -> np.array
#
A function to find the endpoints of a binary image containing a line (i.e., a medial axis).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
np.array
|
The binary image. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
endpoints |
np.array
|
A binary image with only the endpoints of the line. |
Source code in src/micromorph/bacteria/utilities.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
find_furthest_point(point: np.array, candidates: np.array) -> tuple[np.array, int]
#
Find the furthest point from a given point, from a list of candidates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
point
|
np.array
|
A np.array with 2 columns and 1 row (x, y) |
required |
candidates
|
np.array
|
An np.array with 2 columns and N rows (N being the number of candidates) |
required |
Returns:
| Name | Type | Description |
|---|---|---|
closest_point |
np.array
|
The furthest point |
max_index |
int
|
The index of the furthest point in the candidates array |
Source code in src/micromorph/bacteria/utilities.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | |
fix_coordinates_order(coordinates: np.array) -> np.array
#
This function fixes the coordinate order in a list of points (usually extracted from a binary image). In shorts, it picks a point and then finds the closest point to it, and so on.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coordinates
|
np.array
|
An np.array with 2 columns and N rows (N being the number of points) |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ordered_coordinates |
np.array
|
An np.array with 2 columns and N rows (N being the number of points) |
Source code in src/micromorph/bacteria/utilities.py
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | |
get_boundary_coords(binary_image: np.array) -> np.array
#
Runs the find boundary function from skimage, and then extracts the coordinates of the boundary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
binary_image
|
np.array
|
A binary image |
required |
Returns:
| Name | Type | Description |
|---|---|---|
boundary_xy |
np.array
|
An np.array with 2 columns and N rows (N being the number of boundary pixels). The points will not be ordered. |
Source code in src/micromorph/bacteria/utilities.py
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | |
get_coords(binary_image: np.array) -> np.array
#
A function to get the coordinates of the non-zero pixels in a binary image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
binary_image
|
np.array
|
A binary image |
required |
Returns:
| Name | Type | Description |
|---|---|---|
xy |
np.array
|
An np.array with 2 columns and N rows (N being the number of non-zero pixels) |
Source code in src/micromorph/bacteria/utilities.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |
get_width_profile_lines(medial_axis: np.array, n_points: int = 3, line_magnitude: int or float = 10)
#
Function to get the lines that will be used to calculate the width of the bacterium through fitting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
medial_axis
|
np.array
|
An np.array with 2 columns and N rows (N being the number of points in the medial axis). |
required |
n_points
|
int
|
The number of points to sample along the medial axis. |
3
|
line_magnitude
|
int or float
|
The length of the lines that will be perpendicular to the medial axis. |
10
|
Returns:
| Type | Description |
|---|---|
x_high, y_high, x_low, y_low : np.arrays
|
np.arrays with 1 column and N rows (N being the number of points in the medial axis). |
Source code in src/micromorph/bacteria/utilities.py
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 | |
prune_short_branches(skeleton: np.array) -> np.array
#
Function to find the longest branch in a skeleton.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
skeleton
|
np.array
|
The binary image, skeletonised |
required |
Returns:
| Name | Type | Description |
|---|---|---|
pruned_skeleton |
np.array
|
A binary image with only the longest branch of the skeleton |
Source code in src/micromorph/bacteria/utilities.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | |
trace_axis(medial_axis_image: np.array) -> np.array
#
Trace the medial axis, returning an ordered array of points from one end to the other. The first endpoint is defined as the most leftmost of the endpoints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
medial_axis_image
|
np.array
|
The binary image with the medial axis |
required |
Returns:
| Name | Type | Description |
|---|---|---|
medial_axis |
np.array
|
An np.array with 2 columns and N rows (N being the number of points in the medial axis) |
Source code in src/micromorph/bacteria/utilities.py
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | |