Skip to content

Commit

Permalink
Segment a tube on click event
Browse files Browse the repository at this point in the history
  • Loading branch information
HastingsGreer committed Feb 4, 2019
1 parent 9e8346b commit 2994617
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
1 change: 1 addition & 0 deletions server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__/*
13 changes: 13 additions & 0 deletions server/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ def print_matrix(itkmat, size=(3, 3)):
sys.stdout.write('{} '.format(itkmat(i, j)))
sys.stdout.write('\n')

def itk_pos_to_array(itkpos):
return [itkpos.GetElement(i) for i in range(3)]

def spatial_object_pt_to_radius(pt):
#forgive me
return float(str(pt).split("R: ")[1].split("\n")[0])


def itk_image_dimension(itk_image):
return len(itk_image.GetLargestPossibleRegion().GetSize())
def itk_pixel_type(itkimage):
return getattr(itk, "".join([char for char in repr(itkimage).split('itkImagePython.')[1].split(';')[0][8:] if char.isupper()]))

# modified from: https://github.com/InsightSoftwareConsortium/itk-jupyter-widgets/blob/master/itkwidgets/trait_types.py#L49
def _itk_image_to_type(itkimage):
component_str = repr(itkimage).split('itkImagePython.')[1].split(';')[0][8:]
Expand Down
43 changes: 36 additions & 7 deletions server/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,46 @@ def median_filter(self, image, radius):
# TODO auto-serialize in objdir_wrap?
return helper.itk_to_vtkjs_image(result)

@rpc('otsu_segment_filter')
@helper.objdir_wrap
def otsu_segment_filter(self, image):
print ("otsu_segment")

itk_image = helper.vtkjs_to_itk_image(image)

otsu_filter = itk.TubeTK.SegmentUsingOtsuThreshold[helper.itk_pixel_type(itk_image),
helper.itk_image_dimension(itk_image),
helper.itk_pixel_type(itk_image)].New()
otsu_filter.SetInput(itk_image)
otsu_filter.Update()

result = otsu_filter.GetOutput()

# TODO auto-serialize in objdir_wrap?
return helper.itk_to_vtkjs_image(result)


@rpc('segment')
@helper.objdir_wrap
def segment(self, image, point):

itk_image = helper.vtkjs_to_itk_image(image)

itk_image = itk.CastImageFilter[type(itk_image), itk.Image[itk.F, 3]].New()(itk_image)

print('segment at:', point)

return [
{'point': [0, 10, 10], 'radius': 10},
{'point': [1, 11, 11], 'radius': 20},
{'point': [1, 12, 12], 'radius': 30},
{'point': [0, 13, 13], 'radius': 40},
{'point': [0, 14, 14], 'radius': 50},
]
tuber = itk.TubeTK.SegmentTubes[type(itk_image)].New()

tuber.SetInputImage(itk_image)
atube = tuber.ExtractTube(point, 0, True)

tube_py = []
for j in range(atube.GetNumberOfPoints()):
pt = atube.GetPoint(j)
pos = helper.itk_pos_to_array(pt.GetPosition())
radius = helper.spatial_object_pt_to_radius(pt)
tube_py.append({'point': pos, 'radius': radius},)

return tube_py

0 comments on commit 2994617

Please sign in to comment.