Note
Go to the end to download the full example code
Direct Bundle Registration#
This example explains how you can register two bundles from two different subjects directly in the space of streamlines [1], [2].
To show the concept we will use two pre-saved cingulum bundles. The algorithm used here is called Streamline-based Linear Registration (SLR) [2].
from time import sleep
from dipy.align.streamlinear import StreamlineLinearRegistration
from dipy.data import two_cingulum_bundles
from dipy.tracking.streamline import set_number_of_points
from dipy.viz import actor, window
Let’s download and load the two bundles.
An important step before running the registration is to resample the streamlines so that they both have the same number of points per streamline. Here we will use 20 points. This step is not optional. Inputting streamlines with a different number of points will break the theoretical advantages of using the SLR as explained in [2].
Let’s say now that we want to move the cb_subj2
(moving) so that it can
be aligned with cb_subj1
(static). Here is how this is done.
srr = StreamlineLinearRegistration()
srm = srr.optimize(static=cb_subj1, moving=cb_subj2)
After the optimization is finished we can apply the transformation to
cb_subj2
.
cb_subj2_aligned = srm.transform(cb_subj2)
def show_both_bundles(bundles, colors=None, show=True, fname=None):
scene = window.Scene()
scene.SetBackground(1.0, 1, 1)
for i, bundle in enumerate(bundles):
color = colors[i]
lines_actor = actor.streamtube(bundle, colors=color, linewidth=0.3)
lines_actor.RotateX(-90)
lines_actor.RotateZ(90)
scene.add(lines_actor)
if show:
window.show(scene)
if fname is not None:
sleep(1)
window.record(scene=scene, n_frames=1, out_path=fname, size=(900, 900))
show_both_bundles(
[cb_subj1, cb_subj2],
colors=[window.colors.orange, window.colors.red],
show=False,
fname="before_registration.png",
)

Before bundle registration.
show_both_bundles(
[cb_subj1, cb_subj2_aligned],
colors=[window.colors.orange, window.colors.red],
show=False,
fname="after_registration.png",
)

After bundle registration.
As you can see the two cingulum bundles are well aligned although they contain many streamlines of different lengths and shapes.
Streamline-based Linear Registration (SLR) is a method which given two sets of streamlines (fixed and moving) and a streamline-based cost function, will minimize the cost function and transform the moving set of streamlines (target) to the fixed (reference), so that they maximally overlap under the condition that the transform stays linear.
We denote a single streamline with s and a set of streamlines with S.
A streamline s is an ordered sequence of line segments connecting 3D vector
points
where D is the rectangular matrix given by all pairwise Minimum average
Direct-Flip (MDF) streamline distances (Garyfallidis et al., 2012).
Therefore, every element of matrix D is equal to
Notice, how in Eq. (1), the most similar streamlines from one streamline set
to the other are weighted more by averaging the minimum values of the rows
and columns of matrix D. This makes our method robust to fanning streamlines
near endpoints of bundles and spurious streamlines if any in the bundle. The
MDF is a symmetric distance between two individual streamlines. It was
primarily used for clustering (Garyfallidis et al., 2010; Visser et al.,
2011) and tractography simplification (see Garyfallidis et al., 2012). This
distance can be applied only when both streamlines have the same number of
points. Therefore we assume from now on that an initial interpolation of
streamlines has been applied, so that all streamlines have the same number
of points K, and all segments of each streamline have equal length. The
length of each segment is equal to the length of the streamline divided by
the number of segments
where
where
and K is the total number of points in
Here,
References#
Total running time of the script: (0 minutes 3.251 seconds)