Skip to content

Instantly share code, notes, and snippets.

@habi
Created September 5, 2024 14:05
Show Gist options
  • Save habi/89ba254da2af95c5dc019ee4a7ecf9ed to your computer and use it in GitHub Desktop.
Save habi/89ba254da2af95c5dc019ee4a7ecf9ed to your computer and use it in GitHub Desktop.
Orthoslicer in Python
def orthoslicer(stack, direction, coordinates, verbose=False):
'''Works like "orthogonal view" (CTRL+Shift+H in ImageJ'''
if verbose:
print('The input stack has a size of %s x %s x %s px' % (stack.shape[0],
stack.shape[1],
stack.shape[2]))
print('Extracting a slice centered %s, %s, %s px in direction %s' % (coordinates[0],
coordinates[1],
coordinates[2],
direction))
if direction == 0:
if verbose:
print('Extracting YZ slice %s of %s' % (coordinates[0], stack.shape[2]))
extractedslice = dask.array.rot90(stack[:, :, coordinates[0]])
elif direction == 1:
if verbose:
print('Extracting XZ slice %s of %s' % (coordinates[1], stack.shape[1]))
extractedslice = stack[:,coordinates[1],:]
elif direction == 2:
if verbose:
print('Extracting slice %s of %s' % (coordinates[2], stack.shape[0]))
extractedslice = stack[coordinates[2],:,:]
if verbose:
plt.figure(999)
for anatomical_direction in range(3):
if anatomical_direction == 0:
plt.subplot(2,2,2)
# Flip and rotate the extracted YZ slice, to be exactly consistent with the OrthoViewer of ImageJ
plt.imshow(skimage.exposure.equalize_adapthist(dask.array.flipud(dask.array.rot90(stack[:, :, coordinates[0]]))))
plt.axhline(coordinates[1], c=seaborn.color_palette(n_colors=3)[1])
plt.axvline(coordinates[2], c=seaborn.color_palette(n_colors=3)[2])
plt.title('YZ slice %s (dir=%s)\nCoords: %s' % (coordinates[0], anatomical_direction, coordinates), color=seaborn.color_palette(n_colors=3)[anatomical_direction])
plt.gca().tick_params(axis='both', color=seaborn.color_palette(n_colors=3)[anatomical_direction])
elif anatomical_direction == 1:
plt.subplot(2,2,3)
plt.imshow(skimage.exposure.equalize_adapthist(stack[:,coordinates[1],:]))
plt.axhline(coordinates[2], c=seaborn.color_palette(n_colors=3)[2])
plt.axvline(coordinates[0], c=seaborn.color_palette(n_colors=3)[0])
plt.title('XZ slice %s (dir=%s)\nCoords: %s' % (coordinates[1], anatomical_direction, coordinates), color=seaborn.color_palette(n_colors=3)[anatomical_direction])
plt.gca().tick_params(axis='both', color=seaborn.color_palette(n_colors=3)[anatomical_direction])
elif anatomical_direction == 2:
plt.subplot(2,2,1)
plt.imshow(skimage.exposure.equalize_adapthist(stack[coordinates[2],:,:]))
plt.axhline(coordinates[1], c=seaborn.color_palette(n_colors=3)[1])
plt.axvline(coordinates[0], c=seaborn.color_palette(n_colors=3)[0])
plt.title('Slice %s (dir=%s)\nCoords: %s' % (coordinates[2], anatomical_direction, coordinates), color=seaborn.color_palette(n_colors=3)[anatomical_direction])
plt.gca().tick_params(axis='both', color=seaborn.color_palette(n_colors=3)[anatomical_direction])
plt.tight_layout(pad=0.1)
plt.show()
return(extractedslice)
# Test extraction script
# Coordinates are set at the border of left big blood vessel on slice 2434 on 11um stack of d152, as shown in OrthoViewer of ImageJ
extracted_slice = orthoslicer(Reconstructions[15],
2,
(1296,1784,2600),
verbose=True)
plt.figure()
plt.imshow(extracted_slice)
plt.show()
@habi
Copy link
Author

habi commented Sep 5, 2024

My code on the left, screenshot of ImageJ on the right.
Screenshot from 2024-09-05 16-01-12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment