Guest User

Untitled

a guest
Dec 14th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. import numpy
  2. import cv2
  3.  
  4.  
  5. def decodeSeg(mask, segmentations):
  6. """
  7. Draw segmentation
  8. """
  9. pts = [
  10. numpy
  11. .array(anno)
  12. .reshape(-1, 2)
  13. .round()
  14. .astype(int)
  15. for anno in segmentations
  16. ]
  17. mask = cv2.fillPoly(mask, pts, 1)
  18.  
  19. return mask
  20.  
  21. def decodeRl(mask, rle):
  22. """
  23. Run-length encoded object decode
  24. """
  25. mask = mask.reshape(-1, order='F')
  26.  
  27. last = 0
  28. val = True
  29. for count in rle['counts']:
  30. val = not val
  31. mask[last:(last+count)] |= val
  32. last += count
  33.  
  34. mask = mask.reshape(rle['size'], order='F')
  35. return mask
  36.  
  37. def annotation2binarymask(annotations):
  38. mask = numpy.zeros((h, w), numpy.uint8)
  39. for annotation in annotations:
  40. segmentations = annotation['segmentation']
  41. if isinstance(segmentations, list): # segmentation
  42. mask = decodeSeg(mask, segmentations)
  43. else: # run-length
  44. mask = decodeRl(mask, segmentations)
  45. return mask
Add Comment
Please, Sign In to add comment