Rectangle

class supervisely_lib.geometry.rectangle.Rectangle(top: int, left: int, bottom: int, right: int, sly_id: Optional[int] = None, class_id: Optional[int] = None, labeler_login: Optional[int] = None, updated_at: Optional[str] = None, created_at: Optional[str] = None)[source]

Bases: supervisely_lib.geometry.geometry.Geometry

Rectangle geometry for a single Label. Rectangle class object is immutable.

Parameters
  • top (int or float) – Minimal vertical value of Rectangle object.

  • left (int or float) – Minimal horizontal value of Rectangle object.

  • bottom (int or float) – Maximal vertical value of Rectangle object.

  • right (int or float) – Maximal vertical value of Rectangle object.

  • sly_id (int, optional) – Rectangle ID in Supervisely server.

  • class_id (int, optional) – ID of ObjClass to which Rectangle belongs.

  • labeler_login (str, optional) – Login of the user who created Rectangle.

  • updated_at (str, optional) – Date and Time when Rectangle was modified last. Date Format: Year:Month:Day:Hour:Minute:Seconds. Example: ‘2021-01-22T19:37:50.158Z’.

  • created_at (str, optional) – Date and Time when Rectangle was created. Date Format is the same as in “updated_at” parameter.

Raises

ValueError. Rectangle top argument must have less or equal value then bottom, left argument must have less or equal value then right

Usage example
top = 100
left = 100
bottom = 700
right = 900
figure = sly.Rectangle(top, left, bottom, right)
to_json() → dict[source]

Convert the Rectangle to a json dict. Read more about Supervisely format.

Returns

Json format as a dict

Return type

dict

Usage example
figure_json = figure.to_json()
print(figure_json)
# Output: {
#    "points": {
#        "exterior": [
#            [100, 100],
#            [900, 700]
#        ],
#        "interior": []
#    }
# }
classmethod from_json(data: dict)supervisely_lib.geometry.rectangle.Rectangle[source]

Convert a json dict to Rectangle. Read more about Supervisely format.

Parameters

data (dict) – Rectangle in json format as a dict.

Returns

Rectangle object

Return type

Rectangle

Usage example
figure_json = {
    "points": {
        "exterior": [
            [100, 100],
            [900, 700]
        ],
        "interior": []
    }
}
figure = sly.Rectangle.from_json(figure_json)
crop(other: supervisely_lib.geometry.rectangle.Rectangle) → List[supervisely_lib.geometry.rectangle.Rectangle][source]

Crops current Rectangle.

Parameters

rect (Rectangle) – Rectangle object for crop.

Returns

List of Rectangle objects

Return type

List[Rectangle]

Usage Example
crop_figures = figure.crop(sly.Rectangle(0, 0, 300, 350))
property corners

Get list of Rectangle corners.

Returns

List of PointLocation objects

Return type

List[PointLocation]

Usage Example
corners = figure.corners
for corner in corners:
    print(corner.row, corner.col)
# Output:
# 100 100
# 100 900
# 700 900
# 700 100
rotate(rotator: supervisely_lib.geometry.image_rotator.ImageRotator)supervisely_lib.geometry.rectangle.Rectangle[source]

Rotates current Rectangle.

Parameters

rotator (ImageRotator) – ImageRotator object for rotation.

Returns

Rectangle object

Return type

Rectangle

Usage Example
from supervisely_lib.geometry.image_rotator import ImageRotator
# Remember that Rectangle class object is immutable, and we need to assign new instance of Rectangle to a new variable
height, width = 300, 400
rotator = ImageRotator((height, width), 25)
rotate_figure = figure.rotate(rotator)
resize(in_size: Tuple[int, int], out_size: Tuple[int, int])supervisely_lib.geometry.rectangle.Rectangle[source]

Resizes current Rectangle.

Parameters
  • in_size (Tuple[int, int]) – Input image size (height, width) to which belongs Rectangle.

  • out_size (Tuple[int, int]) – Desired output image size (height, width) to which belongs Rectangle.

Returns

Rectangle object

Return type

Rectangle

Usage Example
# Remember that Rectangle class object is immutable, and we need to assign new instance of Rectangle to a new variable
in_height, in_width = 300, 400
out_height, out_width = 600, 800
resize_figure = figure.resize((in_height, in_width), (out_height, out_width))
scale(factor: float)supervisely_lib.geometry.rectangle.Rectangle[source]

Scales current Rectangle.

Parameters

factor (float) – Scale parameter.

Returns

Rectangle object

Return type

Rectangle

Usage Example
# Remember that Rectangle class object is immutable, and we need to assign new instance of Rectangle to a new variable
scale_figure = figure.scale(0.75)
translate(drow: int, dcol: int)supervisely_lib.geometry.rectangle.Rectangle[source]

Translates current Rectangle.

Parameters
  • drow (int) – Horizontal shift.

  • dcol (int) – Vertical shift.

Returns

Rectangle object

Return type

Rectangle

Usage Example
# Remember that Rectangle class object is immutable, and we need to assign new instance of Rectangle to a new variable
translate_figure = figure.translate(150, 250)
fliplr(img_size: Tuple[int, int])supervisely_lib.geometry.rectangle.Rectangle[source]

Flips current Rectangle in horizontal.

Parameters

img_size (Tuple[int, int]) – Input image size (height, width) to which belongs Rectangle.

Returns

Rectangle object

Return type

Rectangle

Usage Example
# Remember that Rectangle class object is immutable, and we need to assign new instance of Rectangle to a new variable
height, width = 300, 400
fliplr_figure = figure.fliplr((height, width))
flipud(img_size: Tuple[int, int])supervisely_lib.geometry.rectangle.Rectangle[source]

Flips current Rectangle in vertical.

Parameters

img_size (Tuple[int, int]) – Input image size (height, width) to which belongs Rectangle.

Returns

Rectangle object

Return type

Rectangle

Usage Example
# Remember that Rectangle class object is immutable, and we need to assign new instance of Rectangle to a new variable
height, width = 300, 400
flipud_figure = figure.flipud((height, width))
to_bbox()supervisely_lib.geometry.rectangle.Rectangle[source]

Makes a copy of Rectangle.

Returns

Rectangle object

Return type

Rectangle

Usage Example
# Remember that Rectangle class object is immutable, and we need to assign new instance of Rectangle to a new variable
new_figure = figure.to_bbox()
property area

Rectangle area.

Returns

Area of current Rectangle object

Return type

float

Usage Example
print(figure.area)
# Output: 7288.0
classmethod from_array(arr: numpy.ndarray)supervisely_lib.geometry.rectangle.Rectangle[source]

Create Rectangle with given array shape.

Parameters

arr (np.ndarray) – Numpy array.

Returns

Rectangle object

Return type

Rectangle

Usage Example
np_array = np.zeros((300, 400))
figure_from_np = sly.Rectangle.from_array(np_array)
classmethod from_size(size: Tuple[int, int])supervisely_lib.geometry.rectangle.Rectangle[source]

Create Rectangle with given size shape.

Parameters

size (Tuple[int, int]) – Input size.

Returns

Rectangle object

Return type

Rectangle

Usage Example
size = (300, 400)
figure_from_size = sly.Rectangle.from_size(size)
classmethod from_geometries_list(geometries: List[supervisely_lib.geometry.geometry])Rectangle[source]

Create Rectangle from given geometry objects.

Parameters

geometries (List[Geometry]) – List of geometry type objects: Bitmap, Cuboid, Point, Polygon, Polyline, Rectangle, Graph.

Returns

Rectangle object

Return type

Rectangle

Usage Example
geom_objs = [sly.Point(100, 200), sly.Polyline([sly.PointLocation(730, 2104), sly.PointLocation(2479, 402)])]
figure_from_geom_objs = sly.Rectangle.from_geometries_list(geom_objs)
property left

Minimal horizontal value of Rectangle.

Returns

Minimal horizontal value

Return type

int

Usage Example
print(figure.left)
# Output: 100
property right

Maximal horizontal value of Rectangle.

Returns

Maximal horizontal value

Return type

int

Usage Example
print(figure.right)
# Output: 900
property top

Minimal vertical value of Rectangle.

Returns

Minimal vertical value

Return type

int

Usage Example
print(rectangle.top)
# Output: 100
property bottom

Maximal vertical value of Rectangle.

Returns

Maximal vertical value

Return type

int

Usage Example
print(figure.bottom)
# Output: 700
property center

Center of Rectangle.

Returns

PointLocation object

Return type

PointLocation

Usage Example
center = figure.center()
property width

Width of Rectangle.

Returns

Width

Return type

int

Usage Example
print(figure.width)
# Output: 801
property height

Height of Rectangle

Returns

Height

Return type

int

Usage Example
print(figure.height)
# Output: 601
contains(rect: supervisely_lib.geometry.rectangle.Rectangle) → bool[source]

Checks if Rectangle contains a given Rectangle object.

Parameters

rect (Rectangle) – Rectangle object.

Returns

True if Rectangle contains given Rectangle object, otherwise False

Return type

bool

Usage Example
rect = sly.Rectangle(200, 250, 400, 500))
print(figure.contains(rect))
# Output: True
contains_point_location(pt: supervisely_lib.geometry.point_location.PointLocation) → bool[source]

Checks if Rectangle contains a given PointLocation object.

Parameters

pt (PointLocation) – PointLocation object.

Returns

True if Rectangle contains given PointLocation object, otherwise False

Return type

bool

Usage Example
pt = sly.PointLocation(250, 300))
print(figure.contains_point_location(pt))
# Output: True
to_size() → Tuple[int, int][source]

Height and width of Rectangle.

Returns

Height and width of Rectangle object

Return type

Tuple[int, int]

Usage Example
height, width = figure.to_size()
print(height, width)
# Output: 700 900
get_cropped_numpy_slice(data: numpy.ndarray) → numpy.ndarray[source]

Slice of given numpy array with Rectangle.

Parameters

data (np.ndarray) – Numpy array.

Returns

Sliced numpy array

Return type

np.ndarray

Usage Example
np_slice = np.zeros((200, 500))
mask_slice = figure.get_cropped_numpy_slice(np_slice)
print(mask_slice.shape)
# Output: (199, 499)
intersects_with(rect: supervisely_lib.geometry.rectangle.Rectangle) → bool[source]

Checks intersects Rectangle with given Rectangle object or not.

Parameters

rect (Rectangle) – Rectangle object.

Returns

True if given Rectangle object intersects with Rectangle, otherwise False

Return type

bool

Usage Example
rect = sly.Rectangle(90, 90, 400, 500)
print(figure.intersects_with(rect))
# Output: True