Spaces:
Running
on
Zero
Running
on
Zero
FrancescoLR
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -33,7 +33,7 @@ def extract_middle_slices(nifti_path, output_image_path, slice_size=180):
|
|
33 |
Extracts slices centered around the center of mass of non-zero voxels in a 3D NIfTI image.
|
34 |
The slices are taken along axial, coronal, and sagittal planes and saved as a single PNG.
|
35 |
"""
|
36 |
-
|
37 |
img = nib.load(nifti_path)
|
38 |
data = img.get_fdata()
|
39 |
|
@@ -44,18 +44,18 @@ def extract_middle_slices(nifti_path, output_image_path, slice_size=180):
|
|
44 |
# Define half the slice size to extract regions around the center of mass
|
45 |
half_size = slice_size // 2
|
46 |
|
47 |
-
# Safely extract 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 for the
|
54 |
-
|
55 |
-
for
|
56 |
-
start = max(center[
|
57 |
-
end = min(center[
|
58 |
-
extracted_slice =
|
59 |
return extracted_slice
|
60 |
|
61 |
axial_slice = extract_2d_slice(data, center, axis=2) # Axial (z-axis)
|
|
|
33 |
Extracts slices centered around the center of mass of non-zero voxels in a 3D NIfTI image.
|
34 |
The slices are taken along axial, coronal, and sagittal planes and saved as a single PNG.
|
35 |
"""
|
36 |
+
# Load NIfTI image and get the data
|
37 |
img = nib.load(nifti_path)
|
38 |
data = img.get_fdata()
|
39 |
|
|
|
44 |
# Define half the slice size to extract regions around the center of mass
|
45 |
half_size = slice_size // 2
|
46 |
|
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 for the remaining dimensions
|
54 |
+
remaining_axes = [i for i in range(3) if i != axis]
|
55 |
+
for dim in remaining_axes:
|
56 |
+
start = max(center[dim] - half_size, 0)
|
57 |
+
end = min(center[dim] + half_size, extracted_slice.shape[dim])
|
58 |
+
extracted_slice = extracted_slice.take(range(start, end), axis=dim - (dim > axis))
|
59 |
return extracted_slice
|
60 |
|
61 |
axial_slice = extract_2d_slice(data, center, axis=2) # Axial (z-axis)
|