Skip to content

squareness

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

Compute the squareness of a polygon.

Note

Squareness is a measure of how close an object is to a square.

Squareness: $$ \left(\frac{4*\sqrt{A_{poly}}}{P_{poly}}\right)^2 $$

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

Note

For irregular shapes, squareness is close to zero and for circular shapes close to 1.3. For squares, equals 1

Parameters:

Name Type Description Default
polygon Polygon

Input shapely polygon object.

required

Returns:

Name Type Description
float float

The squareness value of a polygon.

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

    Note:
        Squareness is a measure of how close an object is to a square.

    **Squareness:**
    $$
    \\left(\\frac{4*\\sqrt{A_{poly}}}{P_{poly}}\\right)^2
    $$

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

    Note:
        For irregular shapes, squareness is close to zero and for circular shapes close
        to 1.3. For squares, equals 1

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

    Returns:
        float:
            The squareness value of a polygon.
    """
    area = polygon.area
    perimeter = polygon.length

    return ((np.sqrt(area) * 4) / perimeter) ** 2