otx.api.entities.shapes.rectangle#

This module implements the Rectangle shape entity.

Classes

Rectangle(x1, y1, x2, y2[, modification_date])

Rectangle represents a rectangular shape.

class otx.api.entities.shapes.rectangle.Rectangle(x1: float, y1: float, x2: float, y2: float, modification_date: datetime | None = None)[source]#

Bases: Shape

Rectangle represents a rectangular shape.

Rectangle are used to annotate detection and classification tasks. In the classification case, the rectangle is a full rectangle spanning the whole related item (could be an image, video frame, a region of interest).

  • x1 and y1 represent the top-left coordinate of the rectangle

  • x2 and y2 representing the bottom-right coordinate of the rectangle

Parameters:
  • x1 (float) – x-coordinate of the top-left corner of the rectangle

  • y1 (float) – y-coordinate of the top-left corner of the rectangle

  • x2 (float) – x-coordinate of the bottom-right corner of the rectangle

  • y2 (float) – y-coordinate of the bottom-right corner of the rectangle

  • modification_date (datetime.datetime) – Date of the last modification of the rectangle

clip_to_visible_region() Rectangle[source]#

Clip the rectangle to the [0, 1] visible region of an image.

Returns:

Clipped rectangle.

Return type:

Rectangle

crop_numpy_array(data: ndarray) ndarray[source]#

Crop the given Numpy array to the region of interest represented by this rectangle.

Parameters:

data (np.ndarray) – Image to crop.

Returns:

Cropped image.

Return type:

np.ndarray

denormalize_wrt_roi_shape(roi_shape: ShapeEntity) Rectangle[source]#

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

Example

Assume we have rectangle b1 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 rectangle b1 expressed in the coordinate space of roi. (should return top-half) Box denormalized to a rectangle as ROI

>>> from otx.api.entities.annotation import Annotation
>>> b1 = Rectangle(x1=0.5, x2=1.0, y1=0.0, y2=0.5)
# the top-right
>>> roi = Annotation(Rectangle(x1=0.5, x2=1.0, y1=0.0, y2=1.0))
# the half-right
>>> normalized = b1.denormalize_wrt_roi_shape(roi_shape)
# should return top half
>>> normalized
Box(, x=0.0, y=0.0, width=1.0, height=0.5)
Parameters:

roi_shape (ShapeEntity) – Region of Interest

Raises:

ValueError – If the roi_shape is not a Rectangle.

Returns:

New polygon in the ROI coordinate system

Return type:

Rectangle

classmethod generate_full_box() Rectangle[source]#

Returns a rectangle that fully encapsulates the normalized coordinate space.

Example

>>> Rectangle.generate_full_box()
Box(, x=0.0, y=0.0, width=1.0, height=1.0)
Returns:

A rectangle that fully encapsulates the normalized coordinate space.

Return type:

Rectangle

get_area() float[source]#

Computes the approximate area of the shape.

Area is a value between 0 and 1, calculated as (x2-x1) * (y2-y1)

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

>>> Rectangle(0, 0, 1, 1).get_area()
1.0
>>> Rectangle(0.5, 0.5, 1.0, 1.0).get_area()
0.25
Returns:

Approximate area of the shape.

Return type:

float

static is_full_box(rectangle: ShapeEntity) bool[source]#

Returns true if rectangle is a full box (occupying the full normalized coordinate space).

Example

>>> b1 = Rectangle(x1=0.5, x2=1.0, y1=0.0, y2=1.0)
>>> Rectangle.is_full_box(b1)
False
>>> b2 = Rectangle(x1=0.0, x2=1.0, y1=0.0, y2=1.0)
>>> Rectangle.is_full_box(b2)
True
Parameters:

rectangle (ShapeEntity) – rectangle to evaluate

Returns:

true if it fully encapsulate normalized coordinate space.

Return type:

bool

normalize_wrt_roi_shape(roi_shape: ShapeEntity) Rectangle[source]#

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

Example

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

>>> from otx.api.entities.annotation import Annotation
>>> b1 = Rectangle(x1=0.5, x2=1.0, y1=0.0, y2=0.5)
>>> roi = Rectangle(x1=0.0, x2=0.5, y1=0.0, y2=0.5)
>>> normalized = b1.normalize_wrt_roi_shape(roi_shape)
>>> normalized
Box(, x=0.25, y=0.0, width=0.25, height=0.25)
Parameters:

roi_shape (ShapeEntity) – Region of Interest.

Raises:

ValueError – If the roi_shape is not a Rectangle.

Returns:

New polygon in the image coordinate system

property diagonal: float#

Returns the diagonal size/hypotenuse of the rectangle (x-axis).

Example

>>> b1 = Rectangle(x1=0.0, x2=0.3, y1=0.0, y2=0.4)
>>> b1.diagonal
0.5
Returns:

the width of the rectangle. (x-axis)

Return type:

float

property height: float#

Returns the height of the rectangle (y-axis).

Example

>>> b1 = Rectangle(x1=0.5, x2=1.0, y1=0.0, y2=0.5)
>>> b1.height
0.5
Returns:

the height of the rectangle. (y-axis)

Return type:

float

property width: float#

Returns the width of the rectangle (x-axis).

Example

>>> b1 = Rectangle(x1=0.5, x2=1.0, y1=0.0, y2=0.5)
>>> b1.width
0.5
Returns:

the width of the rectangle. (x-axis)

Return type:

float