Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compressed RLE to uncompressed RLE #386

Open
petoor opened this issue Mar 10, 2020 · 5 comments
Open

Compressed RLE to uncompressed RLE #386

petoor opened this issue Mar 10, 2020 · 5 comments

Comments

@petoor
Copy link

petoor commented Mar 10, 2020

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.

@petoor petoor changed the title Encoded RLE mask to uncompressed RLE Compressed RLE to uncompressed RLE Mar 11, 2020
@LiDaiY
Copy link

LiDaiY commented May 15, 2020

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

@Bibi56
Copy link

Bibi56 commented Jun 30, 2020

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

@stephengmatthews
Copy link

stephengmatthews commented Jul 28, 2021

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
  1. Uninstall current pycocotools package:
pip uninstall pycocotools
  1. Compile and install:
cd PythonAPI
make install
  1. See the example below for its use. I used the binary mask from The RLE or Polygon format of "segmentation“ for extending to coco dataset facebookresearch/Detectron#100 (comment)
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
Copy link

yevvonlim commented Jul 30, 2022

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
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants