binary mask to RL encoding
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from itertools import chain
|
||||
from itertools import chain, groupby
|
||||
from typing import Dict, List, Optional, Tuple, Union
|
||||
|
||||
import cv2
|
||||
@@ -776,3 +776,12 @@ def rle_to_mask(rle: np.ndarray, resolution_wh: Tuple[int, int]) -> np.ndarray:
|
||||
|
||||
decoded_rle = np.repeat(zero_one_values, rle)
|
||||
return decoded_rle.reshape((height,width), order='F')
|
||||
|
||||
def mask_to_rle(binary_mask: np.ndarray) -> list:
|
||||
rle = []
|
||||
for _, group in groupby(binary_mask.ravel(order='F')):
|
||||
rle.append(len(list(group)))
|
||||
|
||||
if binary_mask[0][0] == 1:
|
||||
rle = [0]+rle
|
||||
return rle
|
||||
Reference in New Issue
Block a user