BitmapBase

class BitmapBase(data, origin=None, expected_data_dims=None, sly_id=None, class_id=None, labeler_login=None, updated_at=None, created_at=None)[source]

Bases: Geometry

Base class for bitmap-like geometries (Bitmap, AlphaMask, MultichannelBitmap); mask + origin. Immutable.

BitmapBase is a base class of Bitmap geometry. BitmapBase class object is immutable.

Example of creating and using see in Bitmap.

Parameters:
data : np.ndarray

Bitmap mask data.

origin=None

PointLocation: top, left corner of Bitmap. Position of the Bitmap within image.

expected_data_dims : int, optional

Number of dimensions of data numpy array.

sly_id : int, optional

Bitmap ID in Supervisely server.

class_id : int, optional

ID of ObjClass to which Bitmap belongs.

labeler_login : str, optional

Login of the user who created Bitmap.

updated_at : str, optional

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

Methods

allowed_transforms

Returns the allowed transforms for the Geometry.

base64_2_data

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.

data_2_base64

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

Flip current Bitmap in horizontal.

flipud

Flip current Bitmap in vertical.

from_json

Convert a json dict to BitmapBase.

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

rotate

Rotates around image center -> New Geometry :param rotator: Class for image rotation.

scale

Scale current Bitmap.

to_bbox

Create Rectangle object from current Bitmap.

to_json

Convert the BitmapBase to a json dict.

translate

Translate current Bitmap.

validate

Validate geometry.

Attributes

area

data

Get mask data of Bitmap.

origin

Position of the Bitmap within image.

classmethod allowed_transforms()

Returns the allowed transforms for the Geometry.

classmethod from_json(json_data)[source]

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

Parameters:
json_data : dict

Bitmap in json format as a dict.

Returns:

Bitmap.

Return type:

BitmapBase

Usage Example:
import supervisely as sly

figure_json = {
    "bitmap": {
        "origin": [1, 1],
        "data": "eJzrDPBz5+WS4mJgYOD19HAJAtLMIMwIInOeqf8BUmwBPiGuQPr///9Lb86/C2QxlgT5BTM4PLuRBuTwebo4hlTMSa44sKHhISMDuxpTYrr03F6gDIOnq5/LOqeEJgDM5ht6"
    },
    "shape": "bitmap",
    "geometryType": "bitmap"
}

figure = sly.Bitmap.from_json(figure_json)
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]

Flip current Bitmap in horizontal.

Parameters:
img_size : Tuple[int, int]

img_size which belongs Bitmap.

Returns:

Flipped bitmap in horizontal.

Return type:

BitmapBase

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

Flip current Bitmap in vertical.

Parameters:
img_size : Tuple[int, int]

img_size which belongs Bitmap.

Returns:

Flipped bitmap in vertical.

Return type:

BitmapBase

Usage Example:
# Remember that Bitmap class object is immutable, and we need to assign new instance of Bitmap 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)
Parameters:
in_size : Tuple[int, int]

Input image size (height, width).

out_size : Tuple[int, int]

Desired output image size (height, width) of the Annotation to which Label belongs.

Returns:

Geometry after resize.

Return type:

Geometry

Raises:

NotImplementedError – if method is not implemented in subclass

rotate(rotator)

Rotates around image center -> New Geometry :param rotator: Class for image rotation. :type rotator: ImageRotator :returns: Geometry after rotation. :rtype: Geometry

scale(factor)[source]

Scale current Bitmap.

Parameters:
factor : float

Scale parameter.

Returns:

Scaled bitmap.

Return type:

BitmapBase

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

Create Rectangle object from current Bitmap.

Returns:

Rectangle from bitmap.

Return type:

Rectangle

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

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

Returns:

Json format as a dict

Return type:

dict

Usage Example:
import supervisely as sly

mask = np.array([[0, 0, 0, 0, 0],
                [0, 1, 1, 1, 0],
                [0, 1, 0, 1, 0],
                [0, 1, 1, 1, 0],
                [0, 0, 0, 0, 0]], dtype=np.bool_)

figure = sly.Bitmap(mask)
figure_json = figure.to_json()
print(json.dumps(figure_json, indent=4))
# Output: {
#    "bitmap": {
#        "origin": [1, 1],
#        "data": "eJzrDPBz5+WS4mJgYOD19HAJAtLMIMwIInOeqf8BUmwBPiGuQPr///9Lb86/C2QxlgT5BTM4PLuRBuTwebo4hlTMSa44sKHhISMDuxpTYrr03F6gDIOnq5/LOqeEJgDM5ht6"
#    },
#    "shape": "bitmap",
#    "geometryType": "bitmap"
# }
translate(drow, dcol)[source]

Translate current Bitmap.

Parameters:
drow : int

Horizontal shift.

dcol : int

Vertical shift.

Returns:

Translated bitmap.

Return type:

BitmapBase

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

Get mask data of Bitmap.

Returns:

Data of Bitmap.

Return type:

np.ndarray

property origin : supervisely.geometry.point_location.PointLocation

Position of the Bitmap within image.

Returns:

Top, left corner of Bitmap.

Return type:

PointLocation