VectorGeometry

class VectorGeometry(exterior, interior=[], sly_id=None, class_id=None, labeler_login=None, updated_at=None, created_at=None)[source]

Bases: Geometry

Base class for vector geometries (polygon, polyline) defined by exterior/interior contours. Immutable.

VectorGeometry is a base class of geometry for a single Label. VectorGeometry object is immutable.

Parameters:
exterior

Exterior coordinates, object contour is defined with these points (used for Polygon).

interior=[]

Interior coordinates, object holes is defined with these points (used for Polygon).

sly_id : int, optional

VectorGeometry ID in Supervisely server.

class_id : int, optional

ID of ObjClass to which VectorGeometry belongs.

labeler_login : str, optional

Login of the user who created VectorGeometry.

updated_at : str, optional

Date and Time when VectorGeometry 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 VectorGeometry was created. Date Format is the same as in “updated_at” parameter.

Raises:

TypeError – if exterior or interior parameters are not a list of PointLocation objects

Usage Example:
import supervisely as sly

exterior = [sly.PointLocation(730, 2104), sly.PointLocation(2479, 402), sly.PointLocation(3746, 1646)]
# or exterior = [[730, 2104], [2479, 402], [3746, 1646]]
# or exterior = [(730, 2104), (2479, 402), (3746, 1646)]
interior = [[sly.PointLocation(1907, 1255), sly.PointLocation(2468, 875), sly.PointLocation(2679, 1577)]]
# or interior = [[[730, 2104], [2479, 402], [3746, 1646]]]
# or interior = [[(730, 2104), (2479, 402), (3746, 1646)]]

figure = sly.Polygon(exterior, interior)

Methods

allowed_transforms

Returns the allowed transforms for the Geometry.

approx_dp

clone

Clone from GEOMETRYYY

config_from_json

Convert geometry config from json format

config_to_json

Convert geometry config to json format

convert

Convert geometry to another geometry shape.

crop

Crop the geometry with given 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 VectorGeometry in horizontal.

flipud

Flips current VectorGeometry in vertical.

from_json

Deserialize from a JSON-compatible dict :param data: JSON-compatible dict :returns: Parsed object

geometry_name

Get the name of the geometry.

get_mask

Returns 2D boolean mask of the geometry.

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 VectorGeometry.

rotate

Rotates current VectorGeometry.

scale

Scales current VectorGeometry.

to_bbox

Creates Rectangle object from current VectorGeometry.

to_json

Convert the VectorGeometry to a json dict.

translate

Translates current VectorGeometry.

validate

Validate geometry.

Attributes

area

exterior

VectorGeometry exterior points.

exterior_np

Converts exterior attribute of VectorGeometry to numpy array.

interior

VectorGeometry interior points.

interior_np

Converts interior attribute of VectorGeometry to numpy array.

classmethod allowed_transforms()

Returns the allowed transforms for the Geometry.

classmethod from_json(data)

Deserialize from a JSON-compatible dict :param data: JSON-compatible dict :returns: Parsed object

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

Get the name of the geometry.

Returns:

name of the geometry

Return type:

str

Returns:

string with name of geometry

clone()

Clone from GEOMETRYYY

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

Crop the geometry with given rectangle.

Parameters:
rect

Rectangle for crop.

Returns:

List of Geometry after crop.

Return type:

List[Geometry]`

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 VectorGeometry in horizontal.

Parameters:
img_size : Tuple[int, int]

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

Returns:

Flipped VectorGeometry in horizontal.

Return type:

VectorGeometry

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

Flips current VectorGeometry in vertical.

Parameters:
img_size : Tuple[int, int]

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

Returns:

Flipped VectorGeometry in vertical.

Return type:

VectorGeometry

Usage Example:
# Remember that VectorGeometry class object is immutable, and we need to assign new instance of VectorGeometry to a new variable
height, width = 300, 400
flipud_figure = figure.flipud((height, width))
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

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 VectorGeometry.

Parameters:
in_size : Tuple[int, int]

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

out_size : Tuple[int, int]

Desired output image size (height, width) to which belongs VectorGeometry.

Returns:

Resized VectorGeometry.

Return type:

VectorGeometry

Usage Example:
# Remember that VectorGeometry class object is immutable, and we need to assign new instance of VectorGeometry 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 VectorGeometry.

Parameters:
rotator

ImageRotator for rotate.

Returns:

Rotated VectorGeometry.

Return type:

VectorGeometry

Usage Example:
from supervisely.geometry.image_rotator import ImageRotator

# Remember that VectorGeometry class object is immutable, and we need to assign new instance of VectorGeometry to a new variable
height, width = 300, 400
rotator = ImageRotator((height, width), 25)
rotate_figure = figure.rotate(rotator)
scale(factor)[source]

Scales current VectorGeometry.

Parameters:
factor : float

Scale parameter.

Returns:

Scaled VectorGeometry.

Return type:

VectorGeometry

Usage Example:
# Remember that VectorGeometry class object is immutable, and we need to assign new instance of VectorGeometry to a new variable
scale_figure = figure.scale(0.75)
to_bbox()[source]

Creates Rectangle object from current VectorGeometry.

Returns:

Rectangle from VectorGeometry.

Return type:

Rectangle

Usage Example:
rectangle = figure.to_bbox()
to_json()[source]

Convert the VectorGeometry 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": [
#            [2104, 730],
#            [402, 2479],
#            [1646, 3746]
#        ],
#        "interior": [
#            [
#                [1255, 1907],
#                [875, 2468],
#                [1577, 2679]
#            ]
#        ]
#    }
# }
translate(drow, dcol)[source]

Translates current VectorGeometry.

Parameters:
drow : int

Horizontal shift.

dcol : int

Vertical shift.

Returns:

Translated VectorGeometry.

Return type:

VectorGeometry

Usage Example:
# Remember that VectorGeometry class object is immutable, and we need to assign new instance of VectorGeometry 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
Returns:

float

property exterior : list[supervisely.geometry.point_location.PointLocation]

VectorGeometry exterior points.

Returns:

VectorGeometry exterior points

Return type:

List[PointLocation]

Usage Example:
exterior = figure.exterior
property exterior_np : numpy.ndarray

Converts exterior attribute of VectorGeometry to numpy array.

Returns:

Numpy array

Return type:

np.ndarray

Usage Example:
print(figure.exterior_np)
# Output:
# [[ 730 2104]
#  [2479  402]
#  [3746 1646]]
property interior : list[list[supervisely.geometry.point_location.PointLocation]]

VectorGeometry interior points.

Returns:

VectorGeometry interior points

Return type:

List[List[PointLocation]]

Usage Example:
interior = figure.interior
property interior_np

Converts interior attribute of VectorGeometry to numpy array.

Returns:

Numpy array

Return type:

List[np.ndarray]

Usage Example:
print(figure.interior_np)
# Output:
# [array([[1907, 1255],
#        [2468,  875],
#        [2679, 1577]])]