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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -7
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 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)
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)