File size: 1,205 Bytes
9dc7a8d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | import os
import numpy as np
import pandas as pd
import SimpleITK as sitk
from skimage.measure import label, regionprops
from glob import glob
from tqdm import tqdm
masks = sorted(glob('luna25_ts_seg/*.nii.gz'))
record = []
for mask_file in tqdm(masks):
mask_itk = sitk.ReadImage(mask_file)
mask_arr = sitk.GetArrayFromImage(mask_itk)
unit_volume = np.prod(mask_itk.GetSpacing())
binary_mask = (mask_arr == 2).astype(int) # Extract nodule label
labeled_mask = label(binary_mask, connectivity=3) # 3D connectivity
properties = regionprops(labeled_mask)
for prop in properties:
# print(f"Region {prop.label}: Centroid = {prop.centroid}, Volume = {prop.area} voxels")
record.append({
'mask_file': os.path.basename(mask_file),
'centroid': np.round(prop.centroid, 3),
'volume_px': int(prop.area),
'volume_mm3': np.round(prop.area * unit_volume, 3),
'bbox': np.array(prop.bbox),
# 'axis_major_length': prop.axis_major_length,
# 'axis_minor_length': prop.axis_minor_length
})
record_df = pd.DataFrame(record)
record_df.to_csv('metadata.csv', index=False)
|