Spaces:
Sleeping
Sleeping
vincentgao95
commited on
Commit
•
b6beafe
1
Parent(s):
e3b93fc
Update app.py
Browse files
app.py
CHANGED
@@ -22,41 +22,25 @@ def get_num_slices(data, view):
|
|
22 |
|
23 |
def extract_slice(data, view, slice_index):
|
24 |
if view == 'Axial':
|
25 |
-
|
|
|
26 |
elif view == 'Coronal':
|
27 |
-
|
|
|
|
|
|
|
28 |
elif view == 'Sagittal':
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
slice_pil = Image.fromarray(slice_image)
|
34 |
-
|
35 |
-
# Get original dimensions
|
36 |
-
original_width, original_height = slice_pil.size
|
37 |
-
|
38 |
-
# Calculate aspect ratio
|
39 |
-
aspect_ratio = original_width / original_height
|
40 |
-
|
41 |
-
# Determine new dimensions based on the aspect ratio
|
42 |
-
if original_width > original_height:
|
43 |
-
new_width = min(max_size[0], original_width)
|
44 |
-
new_height = int(new_width / aspect_ratio)
|
45 |
-
else:
|
46 |
-
new_height = min(max_size[1], original_height)
|
47 |
-
new_width = int(new_height * aspect_ratio)
|
48 |
|
49 |
-
|
50 |
-
resized_image = slice_pil.resize((new_width, new_height), Image.ANTIALIAS)
|
51 |
-
return np.array(resized_image)
|
52 |
|
53 |
def visualize_slice(file_obj, view, slice_index):
|
54 |
data = load_nrrd(file_obj)
|
55 |
slice_image = extract_slice(data, view, slice_index)
|
56 |
-
|
57 |
-
# Resize the slice image while maintaining aspect ratio
|
58 |
-
slice_image = resize_slice(slice_image)
|
59 |
-
|
60 |
# Plot the slice
|
61 |
fig, ax = plt.subplots()
|
62 |
ax.imshow(slice_image, cmap='gray')
|
|
|
22 |
|
23 |
def extract_slice(data, view, slice_index):
|
24 |
if view == 'Axial':
|
25 |
+
# Axial view: XY plane
|
26 |
+
slice_image = data[:, :, slice_index]
|
27 |
elif view == 'Coronal':
|
28 |
+
# Coronal view: XZ plane
|
29 |
+
slice_image = data[:, slice_index, :]
|
30 |
+
# Flip the image vertically for correct orientation
|
31 |
+
slice_image = np.flipud(slice_image).T
|
32 |
elif view == 'Sagittal':
|
33 |
+
# Sagittal view: YZ plane
|
34 |
+
slice_image = data[slice_index, :, :]
|
35 |
+
# Rotate the image for correct orientation
|
36 |
+
slice_image = np.rot90(slice_image, k=3)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
+
return slice_image
|
|
|
|
|
39 |
|
40 |
def visualize_slice(file_obj, view, slice_index):
|
41 |
data = load_nrrd(file_obj)
|
42 |
slice_image = extract_slice(data, view, slice_index)
|
43 |
+
|
|
|
|
|
|
|
44 |
# Plot the slice
|
45 |
fig, ax = plt.subplots()
|
46 |
ax.imshow(slice_image, cmap='gray')
|