Skip to content

Commit

Permalink
checking if streamline points are outside image
Browse files Browse the repository at this point in the history
  • Loading branch information
peterneher committed Oct 24, 2023
1 parent 2074709 commit bb8ab34
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 23 deletions.
27 changes: 9 additions & 18 deletions radtract/parcellation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import multiprocessing
import sys
from radtract.tractdensity import tract_envelope
from radtract.utils import load_trk_streamlines, save_as_vtk_fib, save_trk_streamlines
from radtract.utils import load_trk_streamlines, save_as_vtk_fib, save_trk_streamlines, is_inside
from fury.colormap import distinguishable_colormap
import joblib

Expand Down Expand Up @@ -124,19 +124,6 @@ def split_parcellation(parcellation: nib.Nifti1Image):
return parcels


def is_inside(index, image):
"""
Checks if a given index is inside the image.
:param index:
:param image:
:return:
"""
for i in range(3):
if index[i] < 0 or index[i] > image.shape[i] - 1:
return False
return True


def resample_streamlines(streamlines: nib.streamlines.array_sequence.ArraySequence,
nb_points: int):
"""
Expand Down Expand Up @@ -550,10 +537,14 @@ def parcellate_tract(streamlines: nib.streamlines.array_sequence.ArraySequence,
for j in range(num_points):
p_cont = s[j]
p = np.round(p_cont).astype('int64')
label = envelope_data[p[0], p[1], p[2]]
color = lut_cmap[label-1]*255.0
color = np.append(color, 255.0)
colors.append(color)

if is_inside(p, envelope_data):
label = envelope_data[p[0], p[1], p[2]]
color = lut_cmap[label-1]*255.0
color = np.append(color, 255.0)
colors.append(color)
else:
colors.append(np.array([0, 0, 0, 0]))

elif streamline_point_parcels is not None:
parcellation = streamline_point_parcels
Expand Down
11 changes: 6 additions & 5 deletions radtract/tractdensity.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np
import vtk
from dipy.tracking.streamline import transform_streamlines
from radtract.utils import load_trk_streamlines
from radtract.utils import load_trk_streamlines, is_inside
from skimage.morphology import binary_closing
import argparse
import sys
Expand Down Expand Up @@ -145,10 +145,11 @@ def tract_density(streamlines: nib.streamlines.array_sequence.ArraySequence, # i

segments = intersect_image(spacing, start_index, end_index, start_index_cont, end_index_cont)
for seg in segments:
if binary:
image_data[seg[0][0], seg[0][1], seg[0][2]] = 1
else:
image_data[seg[0][0], seg[0][1], seg[0][2]] += seg[1]
if is_inside(seg[0], image_data):
if binary:
image_data[seg[0][0], seg[0][1], seg[0][2]] = 1
else:
image_data[seg[0][0], seg[0][1], seg[0][2]] += seg[1]

# Perform morphological closing if binary and do_closing is True
if binary and do_closing:
Expand Down
34 changes: 34 additions & 0 deletions radtract/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
from fury.utils import lines_to_vtk_polydata, numpy_to_vtk_colors
from dipy.io.stateful_tractogram import Space, StatefulTractogram
import nibabel as nib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from fury.colormap import distinguishable_colormap


def save_trk_streamlines(streamlines: nib.streamlines.array_sequence.ArraySequence, filename: str, reference_image: nib.Nifti1Image):
Expand Down Expand Up @@ -38,6 +42,36 @@ def save_as_vtk_fib(streamlines, out_filename, colors=None):
save_polydata(polydata=polydata, file_name=out_filename, binary=True)


def plot_parcellation(nifti_file, mip_axis):
"""
"""

image = nib.load(nifti_file)
data = image.get_fdata()
mip = np.max(data, axis=mip_axis)
nb_labels = len(np.unique(mip)) - 1
fury_cmap = distinguishable_colormap(nb_colors=nb_labels)
fury_cmap = [np.array([0, 0, 0, 1])] + fury_cmap
mpl_cmap = ListedColormap(fury_cmap)
plt.imshow(mip.T, cmap=mpl_cmap, origin='lower')
plt.show()


def is_inside(index, image):
"""
Checks if a given index is inside the image.
:param index:
:param image:
:return:
"""
for i in range(3):
if index[i] < 0 or index[i] > image.shape[i] - 1:
return False
return True



def main():
pass

Expand Down

0 comments on commit bb8ab34

Please sign in to comment.