Skip to content

convexity

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

Compute the convexity of a polygon.

Note

Convexity is the relative amount that an object differs from a convex object. Convexity is defined by computing the ratio of the perimeter of an object's convex hull to the perimeter of the object itself. This will take the value of 1 for a convex object, and will be less than 1 if the object is not convex, such as one having an irregular boundary. - Wirth

Convexity: $$ \frac{P_{convex}}{P_{poly}} $$

where \(P_{convex}\) is the perimeter of the convex hull 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 convexity value of a polygon between 0-1.

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

    Note:
        Convexity is the relative amount that an object differs from a
        convex object. Convexity is defined by computing the ratio of
        the perimeter of an object's convex hull to the perimeter of
        the object itself. This will take the value of 1 for a convex
        object, and will be less than 1 if the object is not convex, such
        as one having an irregular boundary.
        - [Wirth](http://www.cyto.purdue.edu/cdroms/micro2/content/education/wirth10.pdf)

    **Convexity:**
    $$
    \\frac{P_{convex}}{P_{poly}}
    $$

    where $P_{convex}$ is the perimeter of the convex hull and $P_{poly}$ is the
    perimeter of the polygon.

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

    Returns:
        float:
            The convexity value of a polygon between 0-1.
    """
    convex_perimeter = polygon.convex_hull.length
    perimeter = polygon.length

    convexity = convex_perimeter / perimeter

    return convexity