FrancescoLR commited on
Commit
a74b6ce
·
verified ·
1 Parent(s): 44da582

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -24
app.py CHANGED
@@ -27,48 +27,47 @@ def download_model():
27
  subprocess.run(["unzip", "-o", zip_path, "-d", MODEL_DIR])
28
  print("Dataset004_WML downloaded and extracted.")
29
 
30
- def extract_middle_slices(nifti_path, output_image_path):
31
  """
32
- Extracts middle slices from a 3D NIfTI image in axial, coronal, and sagittal planes
33
- and saves them as a single PNG file with subplots.
34
  """
35
- import nibabel as nib
36
- import matplotlib.pyplot as plt
37
- import numpy as np
38
-
39
  # Load NIfTI image and get the data
40
  img = nib.load(nifti_path)
41
  data = img.get_fdata()
42
 
43
- # Get middle slices along the three planes
44
- middle_axial = data[:, :, data.shape[2] // 2] # Axial (z-axis)
45
- middle_coronal = data[:, data.shape[1] // 2, :] # Coronal (y-axis)
46
- middle_sagittal = data[data.shape[0] // 2, :, :] # Sagittal (x-axis)
47
 
48
- # Rotate slices for proper orientation
49
- middle_axial = np.rot90(middle_axial, k=-1) # Rotate 90 degrees clockwise
50
- middle_coronal = np.rot90(middle_coronal, k=2) # Rotate 180 degrees
51
- middle_sagittal = np.flipud(np.rot90(middle_sagittal, k=-1)) # Rotate 90 degrees clockwise and flip up-down
52
 
53
- # Pad sagittal slice to match dimensions of other slices
54
- max_height = max(middle_axial.shape[0], middle_sagittal.shape[0])
55
- max_width = max(middle_axial.shape[1], middle_sagittal.shape[1])
 
 
56
 
57
- padded_sagittal = np.zeros((max_height, max_width))
58
- padded_sagittal[:middle_sagittal.shape[0], :middle_sagittal.shape[1]] = middle_sagittal
 
59
 
60
  # Create subplots
61
- fig, axes = plt.subplots(1, 3, figsize=(12, 4)) # 3 subplots in a row
62
 
63
  # Plot each slice
64
- axes[0].imshow(middle_axial, cmap="gray")
65
  axes[0].axis("off")
 
66
 
67
- axes[1].imshow(middle_coronal, cmap="gray")
68
  axes[1].axis("off")
 
69
 
70
- axes[2].imshow(padded_sagittal, cmap="gray")
71
  axes[2].axis("off")
 
72
 
73
  # Save the figure
74
  plt.tight_layout()
@@ -76,6 +75,7 @@ def extract_middle_slices(nifti_path, output_image_path):
76
  plt.close()
77
 
78
 
 
79
  # Function to run nnUNet inference
80
  @spaces.GPU # Decorate the function to allocate GPU for its execution
81
  def run_nnunet_predict(nifti_file):
 
27
  subprocess.run(["unzip", "-o", zip_path, "-d", MODEL_DIR])
28
  print("Dataset004_WML downloaded and extracted.")
29
 
30
+ def extract_middle_slices(nifti_path, output_image_path, slice_size=180):
31
  """
32
+ Extracts slices centered around the center of mass of non-zero voxels in a 3D NIfTI image.
33
+ The slices are taken along axial, coronal, and sagittal planes and saved as a single PNG.
34
  """
 
 
 
 
35
  # Load NIfTI image and get the data
36
  img = nib.load(nifti_path)
37
  data = img.get_fdata()
38
 
39
+ # Compute the center of mass of non-zero voxels
40
+ com = center_of_mass(data > 0)
41
+ center = np.round(com).astype(int)
 
42
 
43
+ # Define half the slice size to extract regions around the center of mass
44
+ half_size = slice_size // 2
 
 
45
 
46
+ # Extract slices around the center of mass
47
+ def extract_slice(data, center, axis):
48
+ slices = [slice(None)] * 3
49
+ slices[axis] = slice(center[axis] - half_size, center[axis] + half_size)
50
+ return np.take(data, range(center[axis] - half_size, center[axis] + half_size), axis=axis, mode='constant', cval=0)
51
 
52
+ axial_slice = extract_slice(data, center, axis=2) # Axial (z-axis)
53
+ coronal_slice = extract_slice(data, center, axis=1) # Coronal (y-axis)
54
+ sagittal_slice = extract_slice(data, center, axis=0) # Sagittal (x-axis)
55
 
56
  # Create subplots
57
+ fig, axes = plt.subplots(1, 3, figsize=(12, 4))
58
 
59
  # Plot each slice
60
+ axes[0].imshow(axial_slice, cmap="gray", origin="lower")
61
  axes[0].axis("off")
62
+ axes[0].set_title("Axial")
63
 
64
+ axes[1].imshow(coronal_slice, cmap="gray", origin="lower")
65
  axes[1].axis("off")
66
+ axes[1].set_title("Coronal")
67
 
68
+ axes[2].imshow(sagittal_slice, cmap="gray", origin="lower")
69
  axes[2].axis("off")
70
+ axes[2].set_title("Sagittal")
71
 
72
  # Save the figure
73
  plt.tight_layout()
 
75
  plt.close()
76
 
77
 
78
+
79
  # Function to run nnUNet inference
80
  @spaces.GPU # Decorate the function to allocate GPU for its execution
81
  def run_nnunet_predict(nifti_file):