本文整理汇总了Python中pycocotools.mask.merge方法的典型用法代码示例。如果您正苦于以下问题:Python mask.merge方法的具体用法?Python mask.merge怎么用?Python mask.merge使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pycocotools.mask
的用法示例。
在下文中一共展示了mask.merge方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __get_annotation__
# 需要导入模块: from pycocotools import mask [as 别名]
# 或者: from pycocotools.mask import merge [as 别名]
def __get_annotation__(self, mask, image=None):
_, contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
segmentation = []
for contour in contours:
# Valid polygons have >= 6 coordinates (3 points)
if contour.size >= 6:
segmentation.append(contour.flatten().tolist())
RLEs = cocomask.frPyObjects(segmentation, mask.shape[0], mask.shape[1])
RLE = cocomask.merge(RLEs)
# RLE = cocomask.encode(np.asfortranarray(mask))
area = cocomask.area(RLE)
[x, y, w, h] = cv2.boundingRect(mask)
if image is not None:
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
cv2.drawContours(image, contours, -1, (0,255,0), 1)
cv2.rectangle(image,(x,y),(x+w,y+h), (255,0,0), 2)
cv2.imshow("", image)
cv2.waitKey(1)
return segmentation, [x, y, w, h], area
示例2: annToRLE
# 需要导入模块: from pycocotools import mask [as 别名]
# 或者: from pycocotools.mask import merge [as 别名]
def annToRLE(self, ann, height, width):
"""
Convert annotation which can be polygons, uncompressed RLE to RLE.
:return: binary mask (numpy 2D array)
"""
segm = ann['segmentation']
if isinstance(segm, list):
# polygon -- a single object might consist of multiple parts
# we merge all parts into one mask rle code
rles = maskUtils.frPyObjects(segm, height, width)
rle = maskUtils.merge(rles)
elif isinstance(segm['counts'], list):
# uncompressed RLE
rle = maskUtils.frPyObjects(segm, height, width)
else:
# rle
rle = ann['segmentation']
return rle
示例3: to_mask
# 需要导入模块: from pycocotools import mask [as 别名]
# 或者: from pycocotools.mask import merge [as 别名]
def to_mask(polys, size):
"""Convert list of polygons to full size binary mask
Parameters
----------
polys : list of numpy.ndarray
Numpy.ndarray with shape (N, 2) where N is the number of bounding boxes.
The second axis represents points of the polygons.
Specifically, these are :math:`(x, y)`.
size : tuple
Tuple of length 2: (width, height).
Returns
-------
numpy.ndarray
Full size binary mask of shape (height, width)
"""
try_import_pycocotools()
import pycocotools.mask as cocomask
width, height = size
polys = [p.flatten().tolist() for p in polys]
rles = cocomask.frPyObjects(polys, height, width)
rle = cocomask.merge(rles)
return cocomask.decode(rle)
示例4: annToRLE
# 需要导入模块: from pycocotools import mask [as 别名]
# 或者: from pycocotools.mask import merge [as 别名]
def annToRLE(ann, h, w):
"""
Convert annotation which can be polygons, uncompressed RLE to RLE.
:return: binary mask (numpy 2D array)
"""
segm = ann['segmentation']
if type(segm) == list:
# polygon -- a single object might consist of multiple parts
# we merge all parts into one mask rle code
rles = maskUtils.frPyObjects(segm, h, w)
rle = maskUtils.merge(rles)
elif type(segm['counts']) == list:
# uncompressed RLE
rle = maskUtils.frPyObjects(segm, h, w)
else:
# rle
rle = ann['segmentation']
return rle
示例5: annToRLE
# 需要导入模块: from pycocotools import mask [as 别名]
# 或者: from pycocotools.mask import merge [as 别名]
def annToRLE(self, ann):
"""
Convert annotation which can be polygons, uncompressed RLE to RLE.
:return: binary mask (numpy 2D array)
"""
t = self.imgs[ann['image_id']]
h, w = t['height'], t['width']
segm = ann['segmentation']
if type(segm) == list:
# polygon -- a single object might consist of multiple parts
# we merge all parts into one mask rle code
rles = maskUtils.frPyObjects(segm, h, w)
rle = maskUtils.merge(rles)
elif type(segm['counts']) == list:
# uncompressed RLE
rle = maskUtils.frPyObjects(segm, h, w)
else:
# rle
rle = ann['segmentation']
return rle
示例6: annToRLE
# 需要导入模块: from pycocotools import mask [as 别名]
# 或者: from pycocotools.mask import merge [as 别名]
def annToRLE(self, ann, height, width):
"""
Convert annotation which can be polygons, uncompressed RLE to RLE.
:return: binary mask (numpy 2D array)
"""
segm = ann['segmentation']
if isinstance(segm, list):
# polygon -- a single object might consist of multiple parts
# we merge all parts into one mask rle code
rles = mask.frPyObjects(segm, height, width)
rle = mask.merge(rles)
elif isinstance(segm['counts'], list):
# uncompressed RLE
rle = mask.frPyObjects(segm, height, width)
else:
# rle
rle = ann['segmentation']
return rle
示例7: polygons_to_mask
# 需要导入模块: from pycocotools import mask [as 别名]
# 或者: from pycocotools.mask import merge [as 别名]
def polygons_to_mask(polys, height, width):
"""
Convert polygons to binary masks.
Args:
polys: a list of nx2 float array. Each array contains many (x, y) coordinates.
Returns:
a binary matrix of (height, width)
"""
polys = [p.flatten().tolist() for p in polys]
assert len(polys) > 0, "Polygons are empty!"
import pycocotools.mask as cocomask
rles = cocomask.frPyObjects(polys, height, width)
rle = cocomask.merge(rles)
return cocomask.decode(rle)
示例8: _poly2mask
# 需要导入模块: from pycocotools import mask [as 别名]
# 或者: from pycocotools.mask import merge [as 别名]
def _poly2mask(self, mask_ann, img_h, img_w):
"""Private function to convert masks represented with polygon to
bitmaps.
Args:
mask_ann (list | dict): Polygon mask annotation input.
img_h (int): The height of output mask.
img_w (int): The width of output mask.
Returns:
numpy.ndarray: The decode bitmap mask of shape (img_h, img_w).
"""
if isinstance(mask_ann, list):
# polygon -- a single object might consist of multiple parts
# we merge all parts into one mask rle code
rles = maskUtils.frPyObjects(mask_ann, img_h, img_w)
rle = maskUtils.merge(rles)
elif isinstance(mask_ann['counts'], list):
# uncompressed RLE
rle = maskUtils.frPyObjects(mask_ann, img_h, img_w)
else:
# rle
rle = mask_ann
mask = maskUtils.decode(rle)
return mask
示例9: polygon_to_bitmap
# 需要导入模块: from pycocotools import mask [as 别名]
# 或者: from pycocotools.mask import merge [as 别名]
def polygon_to_bitmap(polygons, height, width):
"""Convert masks from the form of polygons to bitmaps.
Args:
polygons (list[ndarray]): masks in polygon representation
height (int): mask height
width (int): mask width
Return:
ndarray: the converted masks in bitmap representation
"""
rles = maskUtils.frPyObjects(polygons, height, width)
rle = maskUtils.merge(rles)
bitmap_mask = maskUtils.decode(rle).astype(np.bool)
return bitmap_mask
示例10: convert
# 需要导入模块: from pycocotools import mask [as 别名]
# 或者: from pycocotools.mask import merge [as 别名]
def convert(self, mode):
width, height = self.size
if mode == "mask":
rles = mask_utils.frPyObjects(
[p.numpy() for p in self.polygons], height, width
)
rle = mask_utils.merge(rles)
mask = mask_utils.decode(rle)
mask = torch.from_numpy(mask)
# TODO add squeeze?
return mask
示例11: convert_to_binarymask
# 需要导入模块: from pycocotools import mask [as 别名]
# 或者: from pycocotools.mask import merge [as 别名]
def convert_to_binarymask(self):
width, height = self.size
# formatting for COCO PythonAPI
polygons = [p.numpy() for p in self.polygons]
rles = mask_utils.frPyObjects(polygons, height, width)
rle = mask_utils.merge(rles)
mask = mask_utils.decode(rle)
mask = torch.from_numpy(mask)
return mask