FrancescoLR commited on
Commit
70fbe25
·
verified ·
1 Parent(s): 0496106

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -8
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
- # Load NIfTI image and get the data
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 other two dimensions
54
- other_axes = [i for i in range(3) if i != axis]
55
- for i in other_axes:
56
- start = max(center[i] - half_size, 0)
57
- end = min(center[i] + half_size, data.shape[i])
58
- extracted_slice = np.take(extracted_slice, range(start, end), axis=i)
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)