MultichannelBitmap

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

Bases: BitmapBase

Bitmap mask with multiple channels (e.g. multi-class segmentation). Immutable.

MultichannelBitmap is a geometry for a single Label. MultichannelBitmap object is immutable.

Parameters:
data

bool numpy array

origin=None

points (x and y coordinates) of the top left corner of a bitmap, i.e. the position of the

bitmap within the image :type origin: PointLocation :param sly_id: MultichannelBitmap ID in Supervisely server. :type sly_id: int, optional :param class_id: ID of ObjClass to which MultichannelBitmap belongs. :type class_id: int, optional :param labeler_login: Login of the user who created MultichannelBitmap. :type labeler_login: str, optional :param updated_at: Date and Time when MultichannelBitmap was modified last. Date Format: Year:Month:Day:Hour:Minute:Seconds. Example: ‘2021-01-22T19:37:50.158Z’. :type updated_at: str, optional :param created_at: Date and Time when MultichannelBitmap was created. Date Format is the same as in “updated_at” parameter. :type created_at: str, optional :raises TypeError: if origin is not a PointLocation object :raises TypeError: if data is not a bool numpy array :raises ValueError: if data is not a 3-dimensional numpy array

Methods

allowed_transforms

Returns the allowed transforms for the Geometry.

base64_2_data

The function base64_2_data convert base64 encoded string to numpy

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

Crops the current MultichannelBitmap object with a given rectangle

data_2_base64

he function data_2_base64 convert numpy array to base64 encoded string :param data: numpy array :type data: np.ndarray :returns: string

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_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

Resize the current MultichannelBitmap to match a certain size

rotate

Rotates the MultichannelBitmap within the full image canvas and rotate the whole canvas with a given rotator (ImageRotator class object contain size of image and angle to rotate)

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

Attributes

area

Returns the area of the current MultichannelBitmap.

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)

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

The function base64_2_data convert base64 encoded string to numpy

Parameters:
s : str

string

Returns:

numpy array

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

he function data_2_base64 convert numpy array to base64 encoded string :param data: numpy array :type data: np.ndarray :returns: string

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

Crops the current MultichannelBitmap object with a given rectangle

Parameters:
rect

Rectangle for crop.

Returns:

Cropped MultichannelBitmap.

Return type:

MultichannelBitmap

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)

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)

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

Resize the current MultichannelBitmap to match a certain size

Parameters:
in_size : Tuple[int, int]

input image size

out_size : Tuple[int, int]

output image size

Returns:

MultichannelBitmap after resize.

Return type:

MultichannelBitmap

rotate(rotator)[source]

Rotates the MultichannelBitmap within the full image canvas and rotate the whole canvas with a given rotator (ImageRotator class object contain size of image and angle to rotate)

Parameters:
rotator

Class for image rotation.

Returns:

Rotated MultichannelBitmap.

Return type:

MultichannelBitmap

scale(factor)

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

Create Rectangle object from current Bitmap.

Returns:

Rectangle from bitmap.

Return type:

Rectangle

Usage Example:
rectangle = figure.to_bbox()
to_json()

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)

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(name, settings)[source]
property area

Returns the area of the current MultichannelBitmap.

Returns:

Area of the MultichannelBitmap.

Return type:

int

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