Spaces:
Sleeping
Sleeping
Upload tumor_features.py
Browse files- utils/tumor_features.py +30 -7
utils/tumor_features.py
CHANGED
@@ -2,13 +2,11 @@ from scipy.ndimage import label, find_objects
|
|
2 |
import numpy as np
|
3 |
import cv2
|
4 |
|
5 |
-
|
6 |
IMAGE_SPACING_X = 0.7031
|
7 |
IMAGE_SPACING_Y = 0.7031
|
8 |
IMAGE_SPACING_Z = 2.5
|
9 |
|
10 |
|
11 |
-
|
12 |
def compute_largest_diameter(binary_mask):
|
13 |
|
14 |
# Label connected components in the binary mask
|
@@ -37,19 +35,44 @@ def compute_largest_diameter(binary_mask):
|
|
37 |
return largest_diameter / 10 # IN CM
|
38 |
|
39 |
|
|
|
40 |
|
41 |
-
|
42 |
-
def generate_features(img, liver, tumor):
|
43 |
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
|
|
|
|
47 |
features = {
|
48 |
"lesion size (cm)": compute_largest_diameter(tumor),
|
49 |
-
"lesion
|
|
|
50 |
"lesion density (HU)": np.mean(img[tumor==1]),
|
51 |
"involvement of adjacent organs:": "Yes" if np.sum(np.multiply(liver==0, tumor)) > 0 else "No"
|
52 |
}
|
53 |
-
|
54 |
|
55 |
return features
|
|
|
2 |
import numpy as np
|
3 |
import cv2
|
4 |
|
|
|
5 |
IMAGE_SPACING_X = 0.7031
|
6 |
IMAGE_SPACING_Y = 0.7031
|
7 |
IMAGE_SPACING_Z = 2.5
|
8 |
|
9 |
|
|
|
10 |
def compute_largest_diameter(binary_mask):
|
11 |
|
12 |
# Label connected components in the binary mask
|
|
|
35 |
return largest_diameter / 10 # IN CM
|
36 |
|
37 |
|
38 |
+
def compute_shape(binary_mask):
|
39 |
|
40 |
+
contours, _ = cv2.findContours(binary_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
|
|
41 |
|
42 |
+
shape, margin = None, None
|
43 |
+
|
44 |
+
if contours:
|
45 |
+
tumor_contour_area = cv2.contourArea(contours[0])
|
46 |
+
|
47 |
+
tumor_contour_perimeter = cv2.arcLength(contours[0], True)
|
48 |
+
epsilon = 0.02 * tumor_contour_perimeter
|
49 |
+
approx = cv2.approxPolyDP(contours[0], epsilon, True)
|
50 |
+
num_sides = len(approx)
|
51 |
|
52 |
+
# determine the shape based on the number of sides
|
53 |
+
if num_sides < 5: shape = "Round"
|
54 |
+
else: shape = "Irregular"
|
55 |
+
|
56 |
+
# determine the margin characteristics based on solidity
|
57 |
+
hull = cv2.convexHull(contours[0])
|
58 |
+
hull_area = cv2.contourArea(hull)
|
59 |
+
tumor_solidity = tumor_contour_area / hull_area
|
60 |
+
if tumor_solidity > 0.95: margin = "Smooth"
|
61 |
+
elif tumor_solidity > 0.80: margin = "Irregular"
|
62 |
+
else: margin = "Spiculated"
|
63 |
+
|
64 |
+
return shape, margin
|
65 |
+
|
66 |
+
def generate_features(img, liver, tumor):
|
67 |
|
68 |
+
# shape, margin = compute_shape(tumor)
|
69 |
+
shape, margin = "irregular", "irregular"
|
70 |
features = {
|
71 |
"lesion size (cm)": compute_largest_diameter(tumor),
|
72 |
+
"lesion boundary": margin,
|
73 |
+
"lesion shape": shape,
|
74 |
"lesion density (HU)": np.mean(img[tumor==1]),
|
75 |
"involvement of adjacent organs:": "Yes" if np.sum(np.multiply(liver==0, tumor)) > 0 else "No"
|
76 |
}
|
|
|
77 |
|
78 |
return features
|