Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 from skimage.draw import polygon
00009 import numpy as np
00010
00011 def segToMask( S, h, w ):
00012 """
00013 Convert polygon segmentation to binary mask.
00014 :param S (float array) : polygon segmentation mask
00015 :param h (int) : target mask height
00016 :param w (int) : target mask width
00017 :return: M (bool 2D array) : binary mask
00018 """
00019 M = np.zeros((h,w), dtype=np.bool)
00020 for s in S:
00021 N = len(s)
00022 rr, cc = polygon(np.array(s[1:N:2]).clip(max=h-1), \
00023 np.array(s[0:N:2]).clip(max=w-1))
00024 M[rr, cc] = 1
00025 return M
00026
00027
00028 def decodeMask(R):
00029 """
00030 Decode binary mask M encoded via run-length encoding.
00031 :param R (object RLE) : run-length encoding of binary mask
00032 :return: M (bool 2D array) : decoded binary mask
00033 """
00034 N = len(R['counts'])
00035 M = np.zeros( (R['size'][0]*R['size'][1], ))
00036 n = 0
00037 val = 1
00038 for pos in range(N):
00039 val = not val
00040 for c in range(R['counts'][pos]):
00041 R['counts'][pos]
00042 M[n] = val
00043 n += 1
00044 return M.reshape((R['size']), order='F')
00045
00046 def mask_coco2voc(coco_masks, im_height, im_width):
00047 voc_masks = np.zeros((len(coco_masks), im_height, im_width))
00048 for i, ann in enumerate(coco_masks):
00049 if type(ann) == list:
00050
00051 m = segToMask(ann, im_height, im_width)
00052 else:
00053
00054 m = decodeMask(ann)
00055 voc_masks[i,:,:]=m;
00056 return voc_masks