RLEMask#
- class rlemasklib.RLEMask(obj, *, shape=None)[source]#
Run-length encoded mask.
The RLEMask class represents a binary mask using run-length encoding. The mask can be created from a dense array, a bounding box, a polygon, or a circle, or from run-length counts. The mask can be manipulated using set operations like union, intersection, and difference, and can be converted to a dense array. Morphological operations, warping, transpose, flipping, cropping, padding, connected components, and other operations are also supported.
The main constructor can take a dense array, a dictionary, or a list of run-length counts.
It is recommended to use the static factory methods
from_array(),from_dict(),from_counts(),from_bbox(),from_polygon(),zeros(), andones()to create new RLEMask objects, as they are more explicit.- Parameters:
obj – the input object to create the mask from. It can be a dense 2D array, a dictionary, or a list/1D-array of run-length counts.
shape (Optional[Sequence[int]]) – [height, width] of the mask, in case the input is a list of run-length counts.
- Raises:
ValueError – if the input is not a dense 2D array, a dictionary, or a sequence of run-length counts.
ValueError – if the sum of the counts is not equal to height * width when counts are given.
ValueError – if the shape is not provided when counts are given.
Properties#
The shape of the mask (height, width). |
|
The run-length counts of the mask, as a copy of the underlying data. |
|
The run-length counts of the mask, as a direct view of the underlying memory. |
|
The ratio of the number of runlengths vs number of pixels of the entire mask |
|
The transpose of the mask (i.e., columns become rows and vice versa) |
Instance Methods#
|
The area of the mask (number of foreground pixels). |
The number of pixels along the contour of the mask. |
|
Check if the RLE mask is valid (no nonfirst zero runs and runs summing to H*W). |
|
The number of nonzero pixels in the mask, equivalent to |
|
|
The indices of the nonzero elements in the mask as a 2D numpy array. |
|
Crop the RLE mask to get a submask, by slicing, or retrieve a single pixel value. |
|
Set the value of a submask to either a constant or another RLE or dense mask. |
Compute the complement of an RLE mask. |
|
|
Compute the union of two RLE masks. |
|
Compute the union with another RLEMask in place. |
|
Compute the intersection of two RLEMasks. |
|
Compute the intersection with another RLEMask in place. |
|
Compute the symmetric difference of two RLEMasks. |
|
Compute the symmetric difference with another RLEMasks in place. |
|
Compute the difference of two RLEMasks. |
|
Compute the difference with another RLEMask in place. |
|
Check if two RLEMasks are equal (same runlengths and shape). |
|
A string representation of the RLEMask, containing the shape and the runlengths. |
|
Convert the RLEMask to a dense numpy array, used by numpy functions. |
|
Check if any pixel in the mask is foreground. |
|
Check if all pixels in the mask are foreground. |
|
Dilate the mask vertically. |
|
Max-pool the mask by a factor of 2. |
|
Min-pool the mask by a factor of 2. |
Average-pool the mask by a factor of 2. |
|
|
Perform a 2D average pooling with the given kernel size and threshold the result. |
|
Perform a 2D convolution with the given weighted kernel and threshold the result. |
|
Resize the mask to a new shape. |
|
Apply an affine warping transformation to the mask. |
|
Apply a perspective warping (homography) transformation to the mask. |
|
[Experimental] Warp the mask according to changing lens-distorted camera parameters |
|
Pad the mask with constant values. |
|
Compute the complement of an RLE mask. |
|
Shift (translate) the mask by an offset vector. |
|
Dilate a mask with a kernel of a given shape and size. |
|
Erode a mask with a kernel of a given shape and size. |
|
Dilate a mask with a 3x3 kernel. |
|
Erode a mask with a 3x3 kernel. |
|
Dilate a mask with a round 5x5 kernel. |
|
Erode a mask with a round 5x5 kernel. |
|
An RLE consisting of those foreground pixels that have at least one background neighbor. |
|
The largest axis-aligned rectangle that fits entirely inside the foreground. |
|
The largest axis-aligned foreground rectangle with a given center point. |
|
Merge this mask with another using a Boolean function. |
|
Repeat the mask pixels multiple times along the axes. |
|
The centroid of the mask, as a numpy float32 array [x, y]. |
|
Compute image moments, same as cv2.moments. |
Compute the 7 Hu moment invariants [1]. |
|
|
Extract connected components from the mask. |
|
Get statistics for all connected components without extracting them. |
|
Count connected components without extracting them. |
|
Extract connected components and their stats in a single pass. |
|
Extract the largest connected component of the mask. |
|
Remove small connected components from the mask. |
|
Fill small holes (i.e., connected components of the background) in the mask. |
|
The bounding box of the foreground, i.e. the smallest rectangle that contains the mask. |
|
Crop the mask to the bounding box. |
|
Crop the mask to the bounding box of the foreground. |
Transpose the mask, i.e. swap the axes such that columns become rows and vice versa. |
|
|
Rotate the mask by a multiple of 90 degrees. |
|
Flip the mask along an axis. |
|
Flip the mask vertically (upside down). |
|
Flip the mask horizontally (left-right). |
|
Tile the mask multiple times along the axes, analogous to |
|
Clone the mask. |
|
Fill a rectangle in the mask. |
|
Fill a circle in the mask. |
|
Convert the RLE mask to a dictionary. |
|
Convert the RLE mask to a dense numpy array. |
|
Decode the RLE mask into an existing array. |
Support for pickle serialization. |
|
|
Compute the intersection-over-union (IoU) between two masks. |
Static Methods#
|
Create an RLEMask object from run-length counts. |
|
Create an RLEMask object from a dense mask. |
|
Create an RLEMask object from an RLE dictionary. |
|
Create an RLEMask object from a bounding box. |
|
Create an RLEMask object representing a filled circle. |
|
Create an RLEMask from an 8-bit grayscale PNG. |
|
Create an RLEMask object from a polygon. |
|
Create a new RLE mask of zeros. |
|
Create a new RLE mask of ones. |
|
Create a new RLE mask of ones with the same shape as another mask. |
|
Create a new RLE mask of zeros with the same shape as another mask. |
|
Return a mask where each pixel is set if and only if at least threshold of the |
|
Return a mask where each pixel is set if and only if all input masks have the pixel set. |
|
Return a mask where each pixel is set if at least one of the input masks has the pixel set. |
|
Merge many masks using either the same or different Boolean functions. |
|
Merge many masks using a custom n-ary boolean function. |
|
Create a merge function from a custom n-ary boolean function. |
|
Merge a list of RLE masks to a label map indicating which masks contains each pixel. |
|
Convert a label map to a dict of RLEMasks. |
|
Convert a PNG label map directly to a dict of RLEMasks. |
|
Concatenate masks along an axis. |
|
Horizontally concatenate masks. |
|
Vertically concatenate masks. |
|
Compute the intersection-over-union (IoU) between two sets of masks. |
References