import numpy as np
import matplotlib.pyplot as plt
from medmask import SegmentationMask, MaskArchive
from spacetransformer import Space
from pathlib import Path
import time
# 创建模拟的2D肺部图像 (1, 64, 64) - 单层CT切片
shape = (1, 64, 64)
space = Space(shape=shape, spacing=(1.0, 1.0, 1.0), origin=(0.0, 0.0, 0.0))
# 构建肺叶掩膜 (5个肺叶,互不重叠)
lobe_mask = np.zeros(shape, dtype=np.uint8)
lobe_mask[0, 10:30, 10:25] = 1 # 左上叶
lobe_mask[0, 35:55, 10:25] = 2 # 左下叶
lobe_mask[0, 10:25, 40:55] = 3 # 右上叶
lobe_mask[0, 30:45, 40:55] = 4 # 右中叶
lobe_mask[0, 50:60, 40:55] = 5 # 右下叶
lobe_mapping = {
"left_upper_lobe": 1,
"left_lower_lobe": 2,
"right_upper_lobe": 3,
"right_middle_lobe": 4,
"right_lower_lobe": 5
}
# 构建肺段掩膜 (10个肺段,与肺叶重叠)
segment_mask = np.zeros(shape, dtype=np.uint8)
# 左上叶的段
segment_mask[0, 10:18, 10:18] = 1
segment_mask[0, 18:25, 12:20] = 2
segment_mask[0, 22:30, 17:25] = 3
# 左下叶的段
segment_mask[0, 35:42, 10:18] = 4
segment_mask[0, 42:50, 12:20] = 5
segment_mask[0, 48:55, 17:25] = 6
# 右上叶的段
segment_mask[0, 10:18, 40:48] = 7
segment_mask[0, 18:25, 42:50] = 8
# 右中叶的段
segment_mask[0, 30:38, 40:48] = 9
segment_mask[0, 38:45, 42:50] = 10
segment_mapping = {
"LUL_S1": 1, "LUL_S2": 2, "LUL_S3": 3,
"LLL_S4": 4, "LLL_S5": 5, "LLL_S6": 6,
"RUL_S1": 7, "RUL_S2": 8,
"RML_S4": 9, "RML_S5": 10
}
# 构建病灶掩膜 (3个病灶,可与肺叶重叠)
lesion_mask = np.zeros(shape, dtype=np.uint8)
lesion_mask[0, 15:20, 15:20] = 1 # 病灶1:位于左上叶
lesion_mask[0, 40:45, 15:20] = 2 # 病灶2:位于左下叶
lesion_mask[0, 25:30, 45:50] = 3 # 病灶3:位于右中叶
lesion_mapping = {
"nodule_1": 1,
"nodule_2": 2,
"mass_1": 3
}
# 构建全肺掩膜 (包含所有肺叶区域)
whole_lung_mask = np.zeros(shape, dtype=np.uint8)
whole_lung_mask[0, 8:62, 8:57] = 1 # 整个肺部区域,稍微扩大范围
whole_lung_mapping = {"whole_lung": 1}
print("模拟数据构建完成:")
print(f"空间信息: {shape}")
print(f"肺叶标签数: {len(lobe_mapping)}")
print(f"肺段标签数: {len(segment_mapping)}")
print(f"病灶标签数: {len(lesion_mapping)}")
print(f"全肺标签数: {len(whole_lung_mapping)}")