Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -28,10 +28,28 @@ def extract_slice(data, view, slice_index):
|
|
28 |
elif view == 'Sagittal':
|
29 |
return data[slice_index, :, :]
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
def visualize_slice(file_obj, view, slice_index):
|
32 |
data = load_nrrd(file_obj)
|
|
|
33 |
slice_image = extract_slice(data, view, slice_index)
|
34 |
-
|
|
|
|
|
|
|
35 |
# Plot the slice
|
36 |
fig, ax = plt.subplots()
|
37 |
ax.imshow(slice_image, cmap='gray')
|
|
|
28 |
elif view == 'Sagittal':
|
29 |
return data[slice_index, :, :]
|
30 |
|
31 |
+
def resize_slice(slice_image, view, data_shape):
|
32 |
+
if view == 'Axial':
|
33 |
+
return slice_image # No resizing needed for Axial view
|
34 |
+
else:
|
35 |
+
# For Coronal and Sagittal views, find the two largest dimensions
|
36 |
+
if view == 'Coronal':
|
37 |
+
resize_dims = (data_shape[0], data_shape[2]) # (x, z)
|
38 |
+
elif view == 'Sagittal':
|
39 |
+
resize_dims = (data_shape[1], data_shape[2]) # (y, z)
|
40 |
+
|
41 |
+
# Resize the slice image while maintaining the aspect ratio
|
42 |
+
resized_image = Image.fromarray(slice_image).resize(resize_dims, Image.ANTIALIAS)
|
43 |
+
return np.array(resized_image)
|
44 |
+
|
45 |
def visualize_slice(file_obj, view, slice_index):
|
46 |
data = load_nrrd(file_obj)
|
47 |
+
data_shape = data.shape # Get the original x, y, z dimensions
|
48 |
slice_image = extract_slice(data, view, slice_index)
|
49 |
+
|
50 |
+
# Resize the slice image if necessary
|
51 |
+
slice_image = resize_slice(slice_image, view, data_shape)
|
52 |
+
|
53 |
# Plot the slice
|
54 |
fig, ax = plt.subplots()
|
55 |
ax.imshow(slice_image, cmap='gray')
|