binary mask to RL encoding

This commit is contained in:
magda skoczen
2024-05-03 09:16:13 +02:00
parent 6913ecf54d
commit 4a068ff8d7
+10 -1
View File
@@ -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