Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -47,17 +47,18 @@ def extract_middle_slices(nifti_path, output_image_path, slice_size=180):
|
|
47 |
# Safely extract and crop 2D slices
|
48 |
def extract_2d_slice(data, center, axis):
|
49 |
slices = [slice(None)] * 3
|
50 |
-
slices[axis] = center[axis]
|
51 |
extracted_slice = data[tuple(slices)]
|
52 |
|
53 |
-
# Crop around the center
|
54 |
remaining_axes = [i for i in range(3) if i != axis]
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
return
|
60 |
|
|
|
61 |
axial_slice = extract_2d_slice(data, center, axis=2) # Axial (z-axis)
|
62 |
coronal_slice = extract_2d_slice(data, center, axis=1) # Coronal (y-axis)
|
63 |
sagittal_slice = extract_2d_slice(data, center, axis=0) # Sagittal (x-axis)
|
|
|
47 |
# Safely extract and crop 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
|
51 |
extracted_slice = data[tuple(slices)]
|
52 |
|
53 |
+
# Crop the 2D slice around the center in the remaining dimensions
|
54 |
remaining_axes = [i for i in range(3) if i != axis]
|
55 |
+
cropped_slice = extracted_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 |
+
return cropped_slice
|
60 |
|
61 |
+
# Extract slices in axial, coronal, and sagittal planes
|
62 |
axial_slice = extract_2d_slice(data, center, axis=2) # Axial (z-axis)
|
63 |
coronal_slice = extract_2d_slice(data, center, axis=1) # Coronal (y-axis)
|
64 |
sagittal_slice = extract_2d_slice(data, center, axis=0) # Sagittal (x-axis)
|