Spaces:
Running
on
Zero
Running
on
Zero
FrancescoLR
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -41,10 +41,10 @@ def extract_middle_slices(nifti_path, output_image_path, slice_size=180):
|
|
41 |
com = center_of_mass(data > 0)
|
42 |
center = np.round(com).astype(int)
|
43 |
|
44 |
-
# Define half the slice size
|
45 |
half_size = slice_size // 2
|
46 |
|
47 |
-
# Safely extract and
|
48 |
def extract_2d_slice(data, center, axis):
|
49 |
slices = [slice(None)] * 3
|
50 |
slices[axis] = center[axis] # Fix the axis to extract a single slice
|
@@ -56,7 +56,15 @@ def extract_middle_slices(nifti_path, output_image_path, slice_size=180):
|
|
56 |
max(center[remaining_axes[0]] - half_size, 0):min(center[remaining_axes[0]] + half_size, extracted_slice.shape[0]),
|
57 |
max(center[remaining_axes[1]] - half_size, 0):min(center[remaining_axes[1]] + half_size, extracted_slice.shape[1]),
|
58 |
]
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
# Extract slices in axial, coronal, and sagittal planes
|
62 |
axial_slice = extract_2d_slice(data, center, axis=2) # Axial (z-axis)
|
@@ -73,14 +81,14 @@ def extract_middle_slices(nifti_path, output_image_path, slice_size=180):
|
|
73 |
# Create subplots
|
74 |
fig, axes = plt.subplots(1, 3, figsize=(12, 4))
|
75 |
|
76 |
-
# Plot each rotated slice
|
77 |
-
axes[0].imshow(axial_slice, cmap="gray", origin="lower"
|
78 |
axes[0].axis("off")
|
79 |
|
80 |
-
axes[1].imshow(coronal_slice, cmap="gray", origin="lower"
|
81 |
axes[1].axis("off")
|
82 |
|
83 |
-
axes[2].imshow(sagittal_slice, cmap="gray", origin="lower"
|
84 |
axes[2].axis("off")
|
85 |
|
86 |
# Save the figure
|
|
|
41 |
com = center_of_mass(data > 0)
|
42 |
center = np.round(com).astype(int)
|
43 |
|
44 |
+
# Define half the slice size
|
45 |
half_size = slice_size // 2
|
46 |
|
47 |
+
# Safely extract and pad 2D slices
|
48 |
def extract_2d_slice(data, center, axis):
|
49 |
slices = [slice(None)] * 3
|
50 |
slices[axis] = center[axis] # Fix the axis to extract a single slice
|
|
|
56 |
max(center[remaining_axes[0]] - half_size, 0):min(center[remaining_axes[0]] + half_size, extracted_slice.shape[0]),
|
57 |
max(center[remaining_axes[1]] - half_size, 0):min(center[remaining_axes[1]] + half_size, extracted_slice.shape[1]),
|
58 |
]
|
59 |
+
|
60 |
+
# Pad the slice to ensure 180x180 dimensions
|
61 |
+
pad_height = slice_size - cropped_slice.shape[0]
|
62 |
+
pad_width = slice_size - cropped_slice.shape[1]
|
63 |
+
padded_slice = np.pad(cropped_slice,
|
64 |
+
((pad_height // 2, pad_height - pad_height // 2),
|
65 |
+
(pad_width // 2, pad_width - pad_width // 2)),
|
66 |
+
mode='constant', constant_values=0)
|
67 |
+
return padded_slice
|
68 |
|
69 |
# Extract slices in axial, coronal, and sagittal planes
|
70 |
axial_slice = extract_2d_slice(data, center, axis=2) # Axial (z-axis)
|
|
|
81 |
# Create subplots
|
82 |
fig, axes = plt.subplots(1, 3, figsize=(12, 4))
|
83 |
|
84 |
+
# Plot each padded and rotated slice
|
85 |
+
axes[0].imshow(axial_slice, cmap="gray", origin="lower")
|
86 |
axes[0].axis("off")
|
87 |
|
88 |
+
axes[1].imshow(coronal_slice, cmap="gray", origin="lower")
|
89 |
axes[1].axis("off")
|
90 |
|
91 |
+
axes[2].imshow(sagittal_slice, cmap="gray", origin="lower")
|
92 |
axes[2].axis("off")
|
93 |
|
94 |
# Save the figure
|