Rectangle¶
-
class Rectangle(top, left, bottom, right, sly_id=
None, class_id=None, labeler_login=None, updated_at=None, created_at=None)[source]¶ Bases:
GeometryAxis-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
Returns the allowed transforms for the Rectangle.
Clone from GEOMETRYYY
Convert geometry config from json format
Convert geometry config to json format
Checks if Rectangle contains a given Rectangle object.
Checks if Rectangle contains a given PointLocation object.
Convert geometry to another geometry shape.
Crops current Rectangle.
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.
Flips current Rectangle in horizontal.
Flips current Rectangle in vertical.
Create Rectangle with given array shape.
Create Rectangle from given geometry objects.
Convert a json dict to Rectangle.
Create Rectangle with given size shape.
Returns the name of the geometry.
Slice of given numpy array with Rectangle.
Returns 2D boolean mask of the geometry.
Checks intersects Rectangle with given Rectangle object or not.
Get the name of the geometry.
Crops object like "crop" method, but return results with coordinates relative to rect :param rect: Rectangle for crop.
Resizes current Rectangle.
Rotates current Rectangle.
Scales current Rectangle.
Makes a copy of Rectangle.
Convert the Rectangle to a json dict.
Height and width of Rectangle.
Translates current Rectangle.
Validate geometry.
Attributes
Rectangle area.
Maximal vertical value of Rectangle.
Center of Rectangle.
Get list of Rectangle corners.
Height of Rectangle
Minimal horizontal value of Rectangle.
Maximal horizontal value of Rectangle.
Minimal vertical value of Rectangle.
Width of Rectangle.
- 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:
- 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.
- 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 geometry_name()[source]¶
Returns the name of the geometry.
- Returns:
name of the geometry
- Return type:
- clone()¶
Clone from GEOMETRYYY
-
convert(new_geometry, contour_radius=
0, approx_epsilon=None)¶ Convert geometry to another geometry shape.
-
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:
- Returns:
Flipped Rectangle in horizontal.
- Return type:
- 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:
- Returns:
Flipped Rectangle in vertical.
- Return type:
- 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
- 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:
- Returns:
Resized Rectangle.
- Return type:
- 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:
- 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)
- to_bbox()[source]¶
Makes a copy of Rectangle.
- Returns:
Copy of Rectangle.
- Return type:
- 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:
- 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:
- Returns:
Translated Rectangle.
- Return type:
- 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:
- Raises:
ValueError – if geometry validation error
- property area : float¶
Rectangle area.
- Returns:
Area of current Rectangle.
- Return type:
- Usage Example:
print(figure.area) # Output: 7288.0
- property bottom : int¶
Maximal vertical value of Rectangle.
- Returns:
Maximal vertical value
- Return type:
- Usage Example:
print(figure.bottom) # Output: 700
- property center : supervisely.geometry.point_location.PointLocation¶
Center of Rectangle.
- Returns:
Center of Rectangle.
- Return type:
- 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:
- Usage Example:
print(figure.height) # Output: 601
- property left : int¶
Minimal horizontal value of Rectangle.
- Returns:
Minimal horizontal value
- Return type:
- Usage Example:
print(figure.left) # Output: 100
- property right : int¶
Maximal horizontal value of Rectangle.
- Returns:
Maximal horizontal value
- Return type:
- Usage Example:
print(figure.right) # Output: 900