Rectangle

class Rectangle[source]

Bases: supervisely.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
import supervisely as sly

top = 100
left = 100
bottom = 700
right = 900
figure = sly.Rectangle(top, left, bottom, right)

Methods

allowed_transforms

clone

Clone from GEOMETRYYY

config_from_json

config_to_json

contains

Checks if Rectangle contains a given Rectangle object.

contains_point_location

Checks if Rectangle contains a given PointLocation object.

convert

crop

Crops current Rectangle.

draw

param bitmap

np.ndarray

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.

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

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

Same as geometry_name(), but shorter.

relative_crop

Crops object like "crop" method, but return results with coordinates relative to rect :param rect: :return: list of Geometry

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

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]
clone()

Clone from GEOMETRYYY

contains(rect)[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
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

PointLocation object.

Returns

True if Rectangle contains given PointLocation object, otherwise False

Return type

bool

Usage Example
import supervisely as sly

pt = sly.PointLocation(250, 300))
print(figure.contains_point_location(pt))
# Output: True
crop(other)[source]

Crops current Rectangle.

Parameters
rect : Rectangle

Rectangle object for crop.

Returns

List of Rectangle objects

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

[R, G, B]

thickness

used only in Polyline and Point

config

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

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

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)[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))
classmethod from_array(arr)[source]

Create Rectangle with given array shape.

Parameters
arr : np.ndarray

Numpy array.

Returns

Rectangle object

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[Geometry]

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

Returns

Rectangle object

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 object

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 object

Return type

Rectangle

Usage Example
import supervisely as sly

size = (300, 400)
figure_from_size = sly.Rectangle.from_size(size)
static geometry_name()[source]
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

Rectangle object.

Returns

True if given Rectangle object 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
classmethod name()

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

Returns

string with name of geometry

relative_crop(rect)

Crops object like “crop” method, but return results with coordinates relative to rect :param rect: :return: list of Geometry

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

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

Rotates current Rectangle.

Parameters
rotator : ImageRotator

ImageRotator object for rotation.

Returns

Rectangle object

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

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)
to_bbox()[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()
to_json()[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": []
#    }
# }
to_size()[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
translate(drow, dcol)[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)
property area

Rectangle area.

Returns

Area of current Rectangle object

Return type

float

Usage Example
print(figure.area)
# Output: 7288.0
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 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
property height

Height of Rectangle

Returns

Height

Return type

int

Usage Example
print(figure.height)
# Output: 601
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 width

Width of Rectangle.

Returns

Width

Return type

int

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