Slice extraction through a specific coordinate

Is there a way that I can extract a coronal slice through posterior commisure, from a AC-PC aligned image automaticaly using open source tools or codes?
Thanks for your help in advance.

There might be a tool, but it’s simple enough to do yourself. If you know the RAS coordinate (x, y, z) you want, you can find its indices (i, j, k) by inverting the image affine:

\left[\array{x\\y\\z\\1}\right] = A\left[\array{i\\j\\k\\1}\right]
\left[\array{i\\j\\k\\1}\right] = A^{-1}\left[\array{x\\y\\z\\1}\right]

So if you want to know where the (0, 0, 0) coordinate is, you could use the following Python:

import numpy as np
import nibabel as nb
img = nb.load(fname)

centroid_1 = np.linalg.inv(img.affine) @ [0, 0, 0, 1]
centroid = centroid_1[:3].astype(int)  # int to round down

If you want a coronal slice, you’ll select the coordinate along the axis lying closest to anterior-posterior. You can find that using aff2axcodes:

axcodes = nb.aff2axcodes(img.affine)
axis_index = next(i for i, code in enumerate(axcodes) if code in "AP")

What you do next depends on how you want your slice. For the sake of keeping the image as a neuroimaging image until you’re ready to do convert to something else, you can use the img.slicer object. Suppose your axis_index == 1 and your centroid == [81, 99, 96]:

coronal_slice = img.slicer[:, 99:100, :]
You could write something a little more programmatic, but it would be much less clear.
slice_idx = centroid[axis_index]
indices = (slice(None),) * axis_idx + (slice(slice_idx, slice_idx + 1),)
single_slice = img.slicer[indices]
2 Likes

3dZcutup in AFNI lets you select which slices to extract into a new dataset. That assumes the third direction of the orientation is the z-direction you want. Otherwise resample your dataset with 3dresample so that it is. Here’s an example with the orientation for the z-direction going from anterior to posterior, so the z-direction will be coronal slices.

3dresample -orient RIA -prefix temp_cor_slices.nii.gz mydset.nii.gz
3dZcutup -prefix zcut${sl} -keep $sl $sl temp_cor_slices.nii.gz

(EDIT for correct orientation)

1 Like

Thanks Chris,

I am just a MD, trying things out. I was just wondering if the concept of getting the coordinates on my image by registering my image to a standard template, then inverse transformation from a coordinate in a standard template.
Sorry if I wasn’t clear enough,.

Thanks.

Thanks Daniel, for your kind reply.

What I want to do, is to align my image to AC-PC line to a specific template, and then get the coordinate of the posterior commisure by transforming my image to the template space, get a coordinate of the PC in the template space, and then inverse transform the coordinates into my native space then extract a coronal slice that passes through this coordinate. I am ready to use any tool possible.

Probably a simple task…but I probably lack the background knowledge.

Thanks.

We did something similar here for transforming coordinates associated with various regions to and from template spaces. There it was all for macaque, but the method is the same. The program takes a string that contains the affine warp and the nonlinear warp volumes to apply to a list of coordinates.

https://github.com/afids/afids-macaca/blob/master/registration/nl_coords_transform.py

For human brain data, I would use @SSwarper in AFNI to compute the alignment. With the transformed coordinates out of nl_coords_transform.py, probably using the -invwarp option to invert the warps. Then use @xyz_to_ijk to compute the ijk indices. Then extract with 3dZcutup as in my previous post.

1 Like