Skip to content

compactness

cellseg_gsontools.geometry.compactness(polygon, **kwargs)

Compute the compactness of a polygon.

Note

Compactness is defined as the ratio of the area of an object to the area of a circle with the same perimeter. A circle is the most compact shape. Objects that are elliptical or have complicated, irregular (not smooth) boundaries have larger compactness. - Wirth

Compactness: $$ \frac{4\pi A_{poly}}{P_{poly}^2} $$

where \(A_{poly}\) is the area of the polygon and \(P_{poly}\) is the perimeter of the polygon.

Parameters:

Name Type Description Default
polygon Polygon

Input shapely polygon object.

required

Returns:

Name Type Description
float float

The compactness value of a polygon between 0-1.

Source code in cellseg_gsontools/geometry/shape_metrics.py
def compactness(polygon: Polygon, **kwargs) -> float:
    """Compute the compactness of a polygon.

    Note:
        Compactness is defined as the ratio of the area of an object
        to the area of a circle with the same perimeter. A circle is the
        most compact shape. Objects that are elliptical or have complicated,
        irregular (not smooth) boundaries have larger compactness.
        - [Wirth](http://www.cyto.purdue.edu/cdroms/micro2/content/education/wirth10.pdf)

    **Compactness:**
    $$
    \\frac{4\\pi A_{poly}}{P_{poly}^2}
    $$

    where $A_{poly}$ is the area of the polygon and $P_{poly}$ is the perimeter of
    the polygon.

    Parameters:
        polygon (Polygon):
            Input shapely polygon object.

    Returns:
        float:
            The compactness value of a polygon between 0-1.
    """
    perimeter = polygon.length
    area = polygon.area

    compactness = (4 * np.pi * area) / perimeter**2

    return compactness