我正在尝试转换在一个程序中创建的组织“掩模”(基本上是组织载玻片上注释的组织区域),并且我想将它们导入到一个名为 QuPath 的程序中。
“掩模”文件本质上是存储为 np.unint8 数组的一系列形状,每个值都是一个像素,其中正值对应于组织的存在,0 对应于不存在的组织。举个例子,这个面具:
[[ 0, 0, 0, 0 ],
[ 0, 1, 1, 0 ],
[ 0, 1, 1, 0 ],
[ 0, 0, 0, 0 ]]
对应于 4x4 的正方形。
数组均位于更大的 H5 文件中以及相关元数据。
我想要将它们导入的程序(QuPath)仅接受 GeoJSON 格式的注释。是否有现有的方法可以将逐像素数组转换为 GeoJSON 格式,还是这必须是一种新颖的方法?
这是我必须从 H5 文件中提取数组的代码。
# open and load file
asset_directory = 'D:\pathai_masks'
file_name = os.path.join(asset_directory, "path5_mask.h5")
h5_file = h5py.File(file_name, 'r')
# access attributes like a regular dictionary
# ex. access'wsi_masks'
wsi_masks = h5_file['wsi_masks']
# Load the JSON from the H5 file:
necrosis_dataset = h5_file["wsi_masks"]["predicted_region_mask_l1_5"]
# Encodes as a numpy.ndarray of uint8
necrosis_numpy_array = necrosis_dataset[:, :]
这就是我必须将数组直接转换为 GeoJSON 文件的方法。
coordinates = []
for row in necrosis_numpy_array:
coordinates.append(row.tolist())
geojson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": coordinates
}
}
]
}
json_data = json.dumps(geojson)
这就是 QuPath 接受的格式。
{
"type": "Feature",
"id": "PathDetectionObject",
"geometry": {
"type": "Polygon",
"coordinates": [[[17738.72, 42238], [17737.94, 42240.13], [17738.39, 42242.34], [17737.21, 42244.28], [17737.19, 42246.54], [17739.74, 42250.23], [17743.86, 42248.63], [17743.7, 42246.37], [17745.05, 42242.08], [17748.38, 42239.13], [17747.76, 42238.25], [17738.72, 42238]]]
},
"properties": {
"isLocked": false,
"measurements": [],
"classification": {
"name": "Other",
"colorRGB": -377282
}
}
}