1 Star 0 Fork 0

hfc/MobileSAM-in-the-Browser-encode-RLE

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
rle.py 6.63 KB
一键复制 编辑 原始数据 按行查看 历史
hfc 提交于 2024-05-08 21:52 . init
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
import numpy as np
import matplotlib.pyplot as plt
def rle_to_mask(rle):
"""Compute a binary mask from an uncompressed RLE."""
h, w = rle["size"]
mask = np.empty(h * w, dtype=bool)
idx = 0
parity = False
for count in rle["counts"]:
mask[idx : idx + count] = parity
idx += count
parity ^= True
mask = mask.reshape(w, h)
return mask.transpose()
# 示例 RLE 数据
rle = {
"size": (684, 1024),
# "size": (1024, 684),
"counts": [
430566,
7,
674,
11,
670,
14,
667,
17,
666,
18,
665,
19,
665,
19,
664,
19,
655,
28,
655,
28,
654,
29,
653,
30,
653,
31,
649,
35,
647,
36,
645,
39,
643,
41,
640,
44,
637,
47,
634,
49,
633,
51,
631,
53,
629,
55,
627,
56,
626,
58,
624,
60,
622,
61,
619,
65,
616,
67,
615,
69,
613,
71,
610,
74,
608,
76,
607,
76,
606,
78,
603,
81,
599,
84,
599,
85,
597,
87,
596,
88,
595,
88,
595,
89,
594,
90,
593,
92,
591,
93,
590,
94,
590,
94,
590,
94,
589,
95,
588,
96,
588,
96,
587,
97,
586,
98,
586,
98,
586,
97,
586,
97,
587,
97,
587,
97,
586,
97,
587,
97,
586,
98,
585,
99,
585,
99,
584,
100,
582,
101,
582,
102,
581,
100,
583,
99,
584,
99,
585,
99,
584,
99,
585,
99,
584,
99,
585,
99,
585,
99,
584,
101,
583,
101,
583,
101,
582,
101,
583,
101,
582,
101,
582,
102,
582,
102,
581,
102,
582,
102,
581,
103,
581,
103,
581,
102,
581,
103,
581,
103,
581,
102,
582,
102,
582,
17,
6,
78,
583,
17,
6,
78,
583,
17,
4,
80,
583,
18,
1,
82,
582,
102,
582,
101,
583,
101,
582,
46,
2,
53,
583,
27,
22,
52,
582,
28,
22,
52,
582,
28,
4,
2,
16,
52,
582,
28,
3,
6,
1,
7,
4,
52,
582,
29,
2,
71,
582,
29,
1,
71,
582,
100,
584,
100,
584,
65,
3,
31,
584,
56,
2,
8,
3,
31,
584,
56,
1,
42,
585,
56,
1,
42,
584,
57,
1,
25,
2,
14,
585,
57,
1,
25,
2,
14,
585,
57,
1,
40,
585,
58,
1,
40,
585,
58,
1,
40,
584,
59,
1,
39,
585,
99,
584,
100,
584,
66,
9,
24,
584,
69,
6,
25,
584,
99,
585,
99,
584,
100,
584,
100,
583,
101,
583,
101,
582,
102,
582,
101,
582,
102,
582,
102,
581,
103,
581,
61,
2,
39,
582,
60,
3,
39,
581,
61,
3,
39,
581,
60,
4,
38,
582,
60,
4,
38,
582,
62,
1,
38,
582,
102,
582,
102,
582,
102,
581,
103,
580,
103,
580,
104,
580,
104,
580,
103,
581,
103,
580,
61,
6,
35,
582,
48,
1,
11,
8,
33,
582,
64,
5,
32,
583,
101,
582,
101,
583,
101,
582,
101,
583,
101,
583,
101,
583,
101,
582,
102,
582,
101,
582,
102,
582,
101,
582,
102,
582,
102,
581,
102,
582,
102,
581,
102,
582,
102,
581,
103,
581,
102,
582,
102,
581,
103,
581,
103,
581,
103,
581,
103,
580,
103,
581,
103,
580,
104,
580,
103,
581,
102,
582,
102,
581,
102,
582,
102,
581,
103,
581,
102,
582,
102,
581,
102,
582,
101,
582,
101,
583,
101,
582,
101,
583,
101,
583,
100,
583,
100,
584,
99,
585,
99,
584,
99,
585,
99,
584,
100,
584,
99,
584,
100,
584,
100,
584,
99,
585,
99,
585,
99,
584,
100,
584,
99,
585,
99,
584,
100,
584,
99,
584,
100,
584,
99,
584,
99,
585,
98,
585,
98,
586,
94,
590,
94,
589,
94,
590,
94,
590,
94,
590,
94,
589,
95,
589,
94,
589,
95,
589,
94,
589,
95,
589,
94,
590,
93,
590,
94,
590,
93,
590,
94,
590,
94,
589,
94,
590,
94,
589,
94,
590,
94,
590,
93,
591,
93,
590,
93,
591,
93,
591,
92,
591,
93,
591,
93,
590,
93,
590,
94,
590,
93,
591,
93,
590,
94,
589,
94,
590,
94,
590,
93,
591,
92,
591,
93,
591,
92,
592,
92,
591,
93,
591,
93,
591,
92,
591,
93,
591,
93,
591,
92,
591,
93,
591,
93,
591,
92,
591,
93,
591,
93,
591,
92,
591,
93,
591,
92,
592,
91,
592,
92,
592,
91,
593,
91,
592,
91,
593,
91,
593,
91,
592,
91,
593,
91,
593,
91,
592,
91,
593,
91,
593,
90,
593,
91,
593,
90,
594,
89,
594,
89,
595,
87,
597,
86,
597,
86,
598,
83,
600,
81,
603,
78,
605,
75,
608,
73,
611,
70,
614,
68,
615,
65,
619,
62,
622,
59,
625,
57,
626,
54,
630,
51,
633,
48,
635,
45,
639,
42,
641,
41,
643,
38,
645,
36,
648,
33,
651,
30,
653,
29,
655,
27,
657,
24,
660,
21,
663,
17,
668,
14,
670,
11,
674,
8,
678,
3,
52537
]
}
# 将 RLE 转换为掩码
mask = rle_to_mask(rle)
# 可视化掩码
plt.figure(figsize=(8, 8))
plt.imshow(mask, cmap="gray")
plt.title("Mask from RLE")
plt.axis("off")
plt.show()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hfcfdf/mobile-sam-in-the-browser-encode-rle.git
git@gitee.com:hfcfdf/mobile-sam-in-the-browser-encode-rle.git
hfcfdf
mobile-sam-in-the-browser-encode-rle
MobileSAM-in-the-Browser-encode-RLE
master

搜索帮助