Feret Modules
Calculate feret distances for 2-D objects.
This code comes from a gist written by @VolkerH under BSD-3 License
https://gist.github.com/VolkerH/0d07d05d5cb189b56362e8ee41882abf
During testing it was discovered that sorting points prior to derivation of upper and lower convex hulls was problematic so this step was removed.
all_pairs(points: npt.NDArray) -> list[tuple[list, list]]
Given a list of 2-D points, finds all ways of sandwiching the points.
Calculates the upper and lower convex hulls and then finds all pairwise combinations between each set of points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
NDArray
|
Numpy array of coordinates defining the outline of an object. |
required |
Returns:
| Type | Description |
|---|---|
List[tuple[int, int]]
|
List of all pair-wise combinations of points between lower and upper hulls. |
Source code in topostats\measure\feret.py
cross2d(x: npt.NDArray, y: npt.NDArray) -> npt.NDArray
Return the c$ross product of two 2-D arrays of vectors.
See https://numpy.org/doc/2.0/reference/generated/numpy.cross.html
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
NDArray
|
First 2-D array. |
required |
y
|
NDArray
|
Second 2-D array. |
required |
Returns:
| Type | Description |
|---|---|
NDArray
|
Cross product of the two vectors. |
Source code in topostats\measure\feret.py
get_feret_from_labelim(label_image: npt.NDArray, labels: None | list | set = None, axis: int = 0) -> dict
Calculate the minimum and maximum feret and coordinates of each connected component within a labelled image.
If labels is None, all labels > 0 will be analyzed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label_image
|
NDArray
|
Numpy array with labelled connected components (integer). |
required |
labels
|
None | list
|
A list of labelled objects for which to calculate. |
None
|
axis
|
int
|
Which axis to sort coordinates on, 0 for row (default); 1 for columns. |
0
|
Returns:
| Type | Description |
|---|---|
dict
|
Labels as keys and values are a tuple of the minimum and maximum feret distances and coordinates. |
Source code in topostats\measure\feret.py
get_feret_from_mask(mask_im: npt.NDArray, axis: int = 0) -> tuple[float, tuple[int, int], float, tuple[int, int]]
Calculate the minimum and maximum feret diameter of the foreground object of a binary mask.
The outline of the object is calculated and the pixel coordinates transformed to a list for calculation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask_im
|
NDArray
|
Binary Numpy array. |
required |
axis
|
int
|
Which axis to sort coordinates on, 0 for row (default); 1 for columns. |
0
|
Returns:
| Type | Description |
|---|---|
Tuple[float, Tuple[int, int], float, Tuple[int, int]]
|
Returns a tuple of the minimum feret and its coordinates and the maximum feret and its coordinates. |
Source code in topostats\measure\feret.py
hulls(points: npt.NDArray, axis: int = 1) -> tuple[list, list]
Graham scan to find upper and lower convex hulls of a set of 2-D points.
Points should be sorted in asecnding order first.
Graham scan <https://en.wikipedia.org/wiki/Graham_scan>
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
NDArray
|
2-D Array of points for the outline of an object. |
required |
axis
|
int
|
Which axis to sort coordinates on 0 for row; 1 for columns (default). |
1
|
Returns:
| Type | Description |
|---|---|
tuple[list, list]
|
Tuple of two Numpy arrays of the original coordinates split into upper and lower hulls. |
Source code in topostats\measure\feret.py
min_max_feret(points: npt.NDArray, axis: int = 0, precision: int = 13) -> dict[float, tuple[int, int], float, tuple[int, int]]
Given a list of 2-D points, returns the minimum and maximum feret diameters.
Feret diameter <https://en.wikipedia.org/wiki/Feret_diameter>
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
NDArray
|
A 2-D array of points for the outline of an object. |
required |
axis
|
int
|
Which axis to sort coordinates on, 0 for row (default); 1 for columns. |
0
|
precision
|
int
|
Number of decimal places passed on to in_polygon() for rounding. |
13
|
Returns:
| Type | Description |
|---|---|
dictionary
|
Tuple of the minimum feret distance and its coordinates and the maximum feret distance and its coordinates. |
Source code in topostats\measure\feret.py
orientation(p: npt.NDArray, q: npt.NDArray, r: npt.NDArray) -> int
Determine the orientation of three points as either clockwise, counter-clock-wise or colinear.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
NDArray
|
First point (assumed to have a length of 2). |
required |
q
|
NDArray
|
Second point (assumed to have a length of 2). |
required |
r
|
NDArray
|
Third point (assumed to have a length of 2). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Returns a positive value if p-q-r are clockwise, neg if counter-clock-wise, zero if colinear. |
Source code in topostats\measure\feret.py
plot_feret(points: npt.NDArray, axis: int = 0, plot_points: str | None = 'k', plot_hulls: tuple | None = ('g-', 'r-'), plot_calipers: str | None = 'y-', plot_triangle_heights: str | None = 'b:', plot_min_feret: str | None = 'm--', plot_max_feret: str | None = 'm--', filename: str | Path | None = './feret.png', show: bool = False) -> tuple
Plot upper and lower convex hulls with rotating calipers and optionally the feret distances.
Plot varying levels of details in constructing convex hulls and deriving the minimum and maximum feret.
For format strings see the Notes section of matplotlib.pyplot.plot
<https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html>.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
NDArray
|
Points to be plotted which form the shape of interest. |
required |
axis
|
int
|
Which axis to sort coordinates on, 0 for row (default); 1 for columns. (Should give the same results!). |
0
|
plot_points
|
str | None
|
Format string for plotting points. If 'None' points are not plotted. |
'k'
|
plot_hulls
|
tuple | None
|
Tuple of length 2 of format strings for plotting the convex hull, these should differe to allow distinction between hulls. If 'None' hulls are not plotted. |
('g-', 'r-')
|
plot_calipers
|
str | None
|
Format string for plotting calipers. If 'None' calipers are not plotted. |
'y-'
|
plot_triangle_heights
|
str | None
|
Format string for plotting the triangle heights used in calulcating the minimum feret. These should cross the opposite edge perpendicularly. If 'None' triangle heights are not plotted. |
'b:'
|
plot_min_feret
|
str | None
|
Format string for plotting the minimum feret. If 'None' the minimum feret is not plotted. |
'm--'
|
plot_max_feret
|
str | None
|
Format string for plotting the maximum feret. If 'None' the maximum feret is not plotted. |
'm--'
|
filename
|
str | Path | None
|
Location to save the image to. |
'./feret.png'
|
show
|
bool
|
Whether to display the image. |
False
|
Returns:
| Type | Description |
|---|---|
(fig, ax)
|
Returns Matplotlib figure and axis objects. |
Examples:
>>> tiny_quadrilateral = np.asarray(
[
[0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0],
[0, 1, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
],
dtype=np.uint8)
>>> holo_ellipse_angled = np.zeros((8, 10), dtype=np.uint8)
rr, cc = draw.ellipse_perimeter(4, 5, 1, 3, orientation=np.deg2rad(30))
holo_ellipse_angled[rr, cc] = 1
Source code in topostats\measure\feret.py
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 519 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 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 | |
rotating_calipers(points: npt.NDArray, axis: int = 0) -> Generator
Given a list of 2-D points, finds all ways of sandwiching the points between two parallel lines.
This yields the sequence of pairs of points touched by each pair of lines across all points around the hull of a polygon.
Rotating Calipers <https://en.wikipedia.org/wiki/Rotating_calipers>_
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
NDArray
|
Numpy array of coordinates defining the outline of an object. |
required |
axis
|
int
|
Which axis to sort coordinates on, 0 for row (default); 1 for columns. |
0
|
Returns:
| Type | Description |
|---|---|
Generator
|
Numpy array of pairs of points. |
Source code in topostats\measure\feret.py
sort_clockwise(coordinates: npt.NDArray) -> npt.NDArray
Sort an array of coordinates in a clockwise order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coordinates
|
NDArray
|
Unordered array of points. Typically a convex hull. |
required |
Returns:
| Type | Description |
|---|---|
NDArray
|
Points ordered in a clockwise direction. |
Examples:
>>> import numpy as np
>>> from topostats.measure import feret
>>> unordered = np.asarray([[0, 0], [5, 5], [0, 5], [5, 0]])
>>> feret.sort_clockwise(unordered)
array([[0, 0],
[0, 5],
[5, 5],
[5, 0]])
Source code in topostats\measure\feret.py
sort_coords(points: npt.NDArray, axis: int = 1) -> npt.NDArray
Sort the coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
NDArray
|
Array of coordinates. |
required |
axis
|
int
|
Which axis to axis coordinates on 0 for row; 1 for columns (default). |
1
|
Returns:
| Type | Description |
|---|---|
NDArray
|
Array sorted by row then column. |
Source code in topostats\measure\feret.py
triangle_height(base1: npt.NDArray | list, base2: npt.NDArray | list, apex: npt.NDArray | list) -> float
Calculate the height of triangle formed by three points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base1
|
int
|
Coordinate of first base point of triangle. |
required |
base2
|
int
|
Coordinate of second base point of triangle. |
required |
apex
|
int
|
Coordinate of the apex of the triangle. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Height of the triangle. |
Examples: