|
from scipy.ndimage import label, find_objects |
|
import numpy as np |
|
import cv2 |
|
|
|
|
|
IMAGE_SPACING_X = 0.7031 |
|
IMAGE_SPACING_Y = 0.7031 |
|
IMAGE_SPACING_Z = 2.5 |
|
|
|
|
|
|
|
def compute_largest_diameter(binary_mask): |
|
|
|
|
|
labeled_array, num_features = label(binary_mask) |
|
|
|
|
|
tumor_objects = find_objects(labeled_array) |
|
|
|
|
|
largest_diameter = 0 |
|
|
|
|
|
for obj in tumor_objects: |
|
|
|
z_dim = obj[2].stop - obj[2].start |
|
y_dim = obj[1].stop - obj[1].start |
|
x_dim = obj[0].stop - obj[0].start |
|
|
|
|
|
diameter = max(z_dim * IMAGE_SPACING_Z, y_dim * IMAGE_SPACING_Y, x_dim * IMAGE_SPACING_X) |
|
|
|
|
|
if diameter > largest_diameter: |
|
largest_diameter = diameter |
|
|
|
return largest_diameter / 10 |
|
|
|
|
|
|
|
|
|
def generate_features(img, liver, tumor): |
|
|
|
contours, _ = cv2.findContours(mask_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) |
|
|
|
|
|
features = { |
|
"lesion size (cm)": compute_largest_diameter(tumor), |
|
"lesion shape": "irregular", |
|
"lesion density (HU)": np.mean(img[tumor==1]), |
|
"involvement of adjacent organs:": "Yes" if np.sum(np.multiply(liver==0, tumor)) > 0 else "No" |
|
} |
|
|
|
|
|
return features |
|
|