Rectangle

class Rectangle(top, left, bottom, right, sly_id=None, class_id=None, labeler_login=None, updated_at=None, created_at=None)[source]

Bases: Geometry

Axis-aligned 2D bounding box (top, left, bottom, right). Immutable.

Axis-aligned 2D bounding box.

Parameters:
top : int or float

Top edge (min row).

left : int or float

Left edge (min col).

bottom : int or float

Bottom edge (max row).

right : int or float

Right edge (max col).

sly_id : int, optional

Server-side rectangle ID.

class_id : int, optional

ObjClass ID.

labeler_login : str, optional

Login of user who created the rectangle.

updated_at : str, optional

Last modification timestamp (ISO format).

created_at : str, optional

Creation timestamp (ISO format).

Raises:

ValueError – If top > bottom or left > right.

Usage Example:
import supervisely as sly

rect = sly.Rectangle(100, 100, 700, 900)

Methods

allowed_transforms

Returns the allowed transforms for the Rectangle.

clone

Clone from GEOMETRYYY

config_from_json

Convert geometry config from json format

config_to_json

Convert geometry config to json format

contains

Checks if Rectangle contains a given Rectangle object.

contains_point_location

Checks if Rectangle contains a given PointLocation object.

convert

Convert geometry to another geometry shape.

crop

Crops current Rectangle.

draw

draw_contour

Draws the figure contour on a given bitmap canvas :param bitmap: np.ndarray :param color: [R, G, B] :param thickness: (int) :param config: drawing config specific to a concrete subclass, e.g. per edge colors.

fliplr

Flips current Rectangle in horizontal.

flipud

Flips current Rectangle in vertical.

from_array

Create Rectangle with given array shape.

from_geometries_list

Create Rectangle from given geometry objects.

from_json

Convert a json dict to Rectangle.

from_size

Create Rectangle with given size shape.

geometry_name

Returns the name of the geometry.

get_cropped_numpy_slice

Slice of given numpy array with Rectangle.

get_mask

Returns 2D boolean mask of the geometry.

intersects_with

Checks intersects Rectangle with given Rectangle object or not.

name

Get the name of the geometry.

relative_crop

Crops object like "crop" method, but return results with coordinates relative to rect :param rect: Rectangle for crop.

resize

Resizes current Rectangle.

rotate

Rotates current Rectangle.

scale

Scales current Rectangle.

to_bbox

Makes a copy of Rectangle.

to_json

Convert the Rectangle to a json dict.

to_size

Height and width of Rectangle.

translate

Translates current Rectangle.

validate

Validate geometry.

Attributes

area

Rectangle area.

bottom

Maximal vertical value of Rectangle.

center

Center of Rectangle.

corners

Get list of Rectangle corners.

height

Height of Rectangle

left

Minimal horizontal value of Rectangle.

right

Maximal horizontal value of Rectangle.

top

Minimal vertical value of Rectangle.

width

Width of Rectangle.

classmethod allowed_transforms()[source]

Returns the allowed transforms for the Rectangle.

classmethod from_array(arr)[source]

Create Rectangle with given array shape.

Parameters:
arr : np.ndarray

Numpy array.

Returns:

Rectangle from array.

Return type:

Rectangle

Usage Example:
import supervisely as sly

np_array = np.zeros((300, 400))
figure_from_np = sly.Rectangle.from_array(np_array)
classmethod from_geometries_list(geometries)[source]

Create Rectangle from given geometry objects.

Parameters:
geometries

List of geometry type objects: Bitmap>, Cuboid>, Point>, Polygon>, Polyline>, Rectangle>, GraphNodes>.

Returns:

Rectangle from given geometry objects.

Return type:

Rectangle

Usage Example:
import supervisely as sly

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)
classmethod from_json(data)[source]

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

Parameters:
data : dict

Rectangle in json format as a dict.

Returns:

Rectangle from json.

Return type:

Rectangle

Usage Example:
import supervisely as sly

figure_json = {
    "points": {
        "exterior": [
            [100, 100],
            [900, 700]
        ],
        "interior": []
    }
}
figure = sly.Rectangle.from_json(figure_json)
classmethod from_size(size)[source]

Create Rectangle with given size shape.

Parameters:
size : Tuple[int, int]

Input size.

Returns:

Rectangle from size.

Return type:

Rectangle

Usage Example:
import supervisely as sly

size = (300, 400)
figure_from_size = sly.Rectangle.from_size(size)
classmethod name()

Get the name of the geometry.

Same as geometry_name(), but shorter. In order to make the code more concise.

Returns:

string with name of geometry

static config_from_json(config)

Convert geometry config from json format

Parameters:
config : dict

dictionary(geometry config) in json format

Returns:

dictionary(geometry config) in json format

Return type:

dict

static config_to_json(config)

Convert geometry config to json format

Parameters:
config : dict

dictionary(geometry config)

Returns:

dictionary(geometry config) in json format

Return type:

dict

static geometry_name()[source]

Returns the name of the geometry.

Returns:

name of the geometry

Return type:

str

clone()

Clone from GEOMETRYYY

contains(rect)[source]

Checks if Rectangle contains a given Rectangle object.

Parameters:
rect

Rectangle to check if it contains.

Returns:

True if Rectangle contains given Rectangle, otherwise False

Return type:

bool

Usage Example:
import supervisely as sly

rect = sly.Rectangle(200, 250, 400, 500))
print(figure.contains(rect))
# Output: True
contains_point_location(pt)[source]

Checks if Rectangle contains a given PointLocation object.

Parameters:
pt

PointLocation to check if it contains.

Returns:

True if Rectangle contains given PointLocation, otherwise False

Return type:

bool

Usage Example:
import supervisely as sly

pt = sly.PointLocation(250, 300))
print(figure.contains_point_location(pt))
# Output: True
convert(new_geometry, contour_radius=0, approx_epsilon=None)

Convert geometry to another geometry shape.

Parameters:
new_geometry

New geometry shape.

contour_radius : int

Radius of the contour.

approx_epsilon : float

Approximation epsilon.

Returns:

List of geometries.

Return type:

List[Geometry]

crop(other)[source]

Crops current Rectangle.

Parameters:
other

Rectangle to crop from.

Returns:

List of Rectangles from Rectangle.

Return type:

List[Rectangle]

Usage Example:
import supervisely as sly

crop_figures = figure.crop(sly.Rectangle(0, 0, 300, 350))
draw(bitmap, color, thickness=1, config=None)
Parameters:
bitmap

np.ndarray

color

Color [R, G, B]

thickness=1

used only in Polyline and Point

config : dict

Drawing config specific to a concrete subclass, e.g. per edge colors

Raises:

NotImplementedError – if method is not implemented in subclass

draw_contour(bitmap, color, thickness=1, config=None)

Draws the figure contour on a given bitmap canvas :param bitmap: np.ndarray :param color: [R, G, B] :param thickness: (int) :param config: drawing config specific to a concrete subclass, e.g. per edge colors

fliplr(img_size)[source]

Flips current Rectangle in horizontal.

Parameters:
img_size : Tuple[int, int]

Input image size (height, width) to which belongs Rectangle.

Returns:

Flipped Rectangle in horizontal.

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)[source]

Flips current Rectangle in vertical.

Parameters:
img_size : Tuple[int, int]

Input image size (height, width) to which belongs Rectangle.

Returns:

Flipped Rectangle in vertical.

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))
get_cropped_numpy_slice(data)[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)
get_mask(img_size)

Returns 2D boolean mask of the geometry. With shape as img_size (height, width) and filled with True values inside the geometry and False values outside. dtype = np.bool shape = img_size

Parameters:
img_size : Tuple[int, int]

size of the image (height, width)

Returns:

2D boolean mask of the geometry

Return type:

np.ndarray

intersects_with(rect)[source]

Checks intersects Rectangle with given Rectangle object or not.

Parameters:
rect

Rectangle to check if it intersects with.

Returns:

True if given Rectangle intersects with Rectangle, otherwise False

Return type:

bool

Usage Example:
import supervisely as sly

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

Crops object like “crop” method, but return results with coordinates relative to rect :param rect: Rectangle for crop. :type rect: Rectangle :returns: List of Geometry after relative crop. :rtype: List[Geometry]` :raises NotImplementedError: if method is not implemented in subclass

resize(in_size, out_size)[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:

Resized Rectangle.

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))
rotate(rotator)[source]

Rotates current Rectangle.

Parameters:
rotator

Class for object rotation.

Returns:

Rotated Rectangle.

Return type:

Rectangle

Usage Example:
from supervisely.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)
scale(factor)[source]

Scales current Rectangle.

Parameters:
factor : float

Scale parameter.

Returns:

Scaled Rectangle.

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)
to_bbox()[source]

Makes a copy of Rectangle.

Returns:

Copy of Rectangle.

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()
to_json()[source]

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

Returns:

Rectangle in 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": []
#    }
# }
to_size()[source]

Height and width of Rectangle.

Returns:

Height and width of Rectangle.

Return type:

Tuple[int, int]

Usage Example:
height, width = figure.to_size()
print(height, width)
# Output: 700 900
translate(drow, dcol)[source]

Translates current Rectangle.

Parameters:
drow : int

Horizontal shift.

dcol : int

Vertical shift.

Returns:

Translated Rectangle.

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)
validate(obj_class_shape, settings)

Validate geometry.

Parameters:
obj_class_shape : str

Object class shape.

settings : dict

Settings.

Raises:

ValueError – if geometry validation error

property area : float

Rectangle area.

Returns:

Area of current Rectangle.

Return type:

float

Usage Example:
print(figure.area)
# Output: 7288.0
property bottom : int

Maximal vertical value of Rectangle.

Returns:

Maximal vertical value

Return type:

int

Usage Example:
print(figure.bottom)
# Output: 700
property center : supervisely.geometry.point_location.PointLocation

Center of Rectangle.

Returns:

Center of Rectangle.

Return type:

PointLocation

Usage Example:
center = figure.center()
property corners : list[PointLocation, PointLocation, PointLocation, PointLocation]

Get list of Rectangle corners.

Returns:

List of Rectangle corners.

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
property height : int

Height of Rectangle

Returns:

Height

Return type:

int

Usage Example:
print(figure.height)
# Output: 601
property left : int

Minimal horizontal value of Rectangle.

Returns:

Minimal horizontal value

Return type:

int

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

Maximal horizontal value of Rectangle.

Returns:

Maximal horizontal value

Return type:

int

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

Minimal vertical value of Rectangle.

Returns:

Minimal vertical value

Return type:

int

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

Width of Rectangle.

Returns:

Width

Return type:

int

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