otx.api.entities.shapes.polygon#

This module implements the Polygon Shape entity.

Classes

Point(x, y)

This class defines a Point with an X and Y coordinate.

Polygon(points[, modification_date])

Represents a polygon formed by a list of coordinates.

class otx.api.entities.shapes.polygon.Point(x: float, y: float)[source]#

Bases: object

This class defines a Point with an X and Y coordinate.

Multiple points can be used to represent a Polygon

denormalize_wrt_roi_shape(roi_shape: Rectangle) Point[source]#

The inverse of normalize_wrt_roi_shape.

Transforming Polygon from the normalized coordinate system to the roi coordinate system. This is used to pull ground truth during training process of the tasks. Examples given in the Shape implementations.

Parameters:

roi_shape (Rectangle) – the shape of the roi

normalize_wrt_roi(roi_shape: Rectangle) Point[source]#

The inverse of denormalize_wrt_roi_shape.

Transforming Polygon from the roi coordinate system to the normalized coordinate system. This is used when the tasks want to save the analysis results.

For example in Detection -> Segmentation pipeline, the analysis results of segmentation needs to be normalized to the roi (bounding boxes) coming from the detection.

Parameters:

roi_shape (Point) – the shape of the roi

class otx.api.entities.shapes.polygon.Polygon(points: List[Point], modification_date: datetime | None = None)[source]#

Bases: Shape

Represents a polygon formed by a list of coordinates.

NB Freehand drawings are also stored as polygons.

Parameters:
  • points – list of Point’s forming the polygon

  • modification_date – last modified date

denormalize_wrt_roi_shape(roi_shape: Rectangle) Polygon[source]#

Transforming shape from the normalized coordinate system to the roi coordinate system.

This function is the inverse of normalize_wrt_roi_shape

Example

Assume we have Polygon p1 which lives in the top-right quarter of the normalized coordinate space. The roi is a rectangle living in the half right of the normalized coordinate space. This function returns Polygon p1 expressed in the coordinate space of roi. (should return top-half)

Polygon denormalized to a rectangle as ROI

>>> from otx.api.entities.shapes.rectangle import Rectangle
>>> from otx.api.entities.annotation import Annotation
>>> p1 = Polygon(points=[Point(x=0.5, y=0.0), Point(x=0.75, y=0.2), Point(x=0.6, y=0.1)])
>>> roi = Rectangle(x1=0.5, x2=1.0, y1=0.0, y2=1.0)  # the half-right
>>> normalized = p1.denormalize_wrt_roi_shape(roi_shape)
>>> normalized
Polygon(, len(points)=3)
Parameters:

roi_shape – Region of Interest

Returns:

New polygon in the ROI coordinate system

get_area() float[source]#

Returns the approximate area of the shape.

Area is a value between 0 and 1, computed by converting the Polygon to a shapely polygon and reading the .area property.

NOTE: This method should not be relied on for exact area computation. The area is approximate, because shapes are continuous, but pixels are discrete.

Example

>>> Polygon(points=[Point(x=0.0, y=0.5), Point(x=0.5, y=0.5), Point(x=0.75, y=0.75)]).get_area()
0.0625
Returns:

area of the shape

normalize_wrt_roi_shape(roi_shape: Rectangle) Polygon[source]#

Transforms from the roi coordinate system to the normalized coordinate system.

This function is the inverse of denormalize_wrt_roi_shape.

Example

Assume we have Polygon p1 which lives in the top-right quarter of a 2D space. The 2D space where p1 lives in is an roi living in the top-left quarter of the normalized coordinate space. This function returns Polygon p1 expressed in the normalized coordinate space.

>>> from otx.api.entities.annotation import Annotation
>>> from otx.api.entities.shapes.rectangle import Rectangle
>>> p1 = Polygon(points=[Point(x=0.5, y=0.0), Point(x=0.75, y=0.2), Point(x=0.6, y=0.1)])
>>> roi = Rectangle(x1=0.0, x2=0.5, y1=0.0, y2=0.5)
>>> normalized = p1.normalize_wrt_roi_shape(roi_shape)
>>> normalized
Polygon(, len(points)=3)
Parameters:

roi_shape – Region of Interest

Returns:

New polygon in the image coordinate system