cocodataset / cocoapi

COCO API - Dataset @ http://cocodataset.org/
Other
6.03k stars 3.75k forks source link

Compressed RLE to uncompressed RLE #386

Open petoor opened 4 years ago

petoor commented 4 years ago

Hi,

As the title says, is there a way to go from compressed RLE to uncompressed RLE?

Or is there a way to decode the byte string returned by the "count" to return the array of integer counts?

Best regards.

LiDaiY commented 4 years ago

hi, have you solved it? I have the same problem

Bibi56 commented 4 years ago

Given the algorithm and some data you should be able to find the details.

stephengmatthews commented 2 years ago

I also needed this so I created the following solution. It's not the cleanest of solutions but gets the job done.

  1. Clone the repo and add the function below to PythonAPI/pycocotools/_mask.pyx.

    def decompress(rleObjs):
    cdef RLEs Rs = _frString(rleObjs)
    rles, hs, ws = [], [], []
    for i in range(Rs.n):
        rles.append([Rs._R[i].cnts[j] for j in range(Rs._R[i].m)])
        hs.append(Rs._R[i].h)
        ws.append(Rs._R[i].w)
    return rles, hs, ws
  2. Uninstall current pycocotools package:

    pip uninstall pycocotools
  3. Compile and install:

    cd PythonAPI
    make install
  4. See the example below for its use. I used the binary mask from https://github.com/facebookresearch/Detectron/issues/100#issuecomment-373458213

    
    import pycocotools.mask as mask_util
    import pycocotools._mask as _mask_util
    import numpy as np

binary_mask = np.array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 1, 1, 1, 0, 0], [ 0, 0, 0, 0, 0, 1, 1, 1, 0, 0], [ 0, 0, 0, 0, 0, 1, 1, 1, 0, 0], [ 0, 0, 0, 0, 0, 1, 1, 1, 0, 0], [ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=np.uint8)

fortran_binary_mask = np.asfortranarray(binary_mask) compressed_rle = mask_util.encode(fortran_binary_mask) decompressed_rles, heights, widths = _mask_util.decompress([compressed_rle])

yevvonlim commented 1 year ago

I had the same issue, but it was quite simple. You can just use
pycocotools.mask.decode(ANNON) to decode your model output in compressed RLE to binary mask. Then you can easily encode it to uncompressed RLE.

armengot commented 1 year ago

https://github.com/armengot/COCO_decode_mask/tree/main/python