import json
import numpy as np
import matplotlib.pyplot as plt

# 读取你的json
with open('2D_Semantic_Map_839920.json') as f:
    data = json.load(f)

# 自动推断画布高宽
h = max([max((coords[0] for coords in inst['mask_coords']), default=0) for inst in data]) + 1
w = max([max((coords[1] for coords in inst['mask_coords']), default=0) for inst in data]) + 1
img = np.zeros((h, w), dtype=int)

# 遍历所有mask，赋值category_id
for inst in data:
    cid = inst['category_id']
    for y, x in inst['mask_coords']:
        img[y, w - 1 - x] = cid  # 在这里把x左右反转回来！

plt.figure(figsize=(12,12))
im = plt.imshow(img, cmap='tab20')  # 你用的就是这个色表
# plt.colorbar(im, label='Category ID')
plt.title('2D Semantic Map (Restored from 2D Map JSON)')
plt.savefig('2D_Semantic_Map_839920.png', bbox_inches='tight', dpi=300)
plt.show()