Skip to content

Instantly share code, notes, and snippets.

@mingrisch
Last active August 29, 2015 13:56
Show Gist options
  • Save mingrisch/9132638 to your computer and use it in GitHub Desktop.
Save mingrisch/9132638 to your computer and use it in GitHub Desktop.
import numpy as np
def run(self,mvNode,labelNode, filename):
"""
Extract pixel curves from a multivolume node
prerequisite: labelNode and mvNode need to have the same dimensions, this is not checked.
As long as the label node is created from the mvNode, that's no problem at all.
"""
# get a numpy reference to the moving volume buffer
mvimg=mvNode.GetImageData()
npmvimg=vtk.util.numpy_support.vtk_to_numpy(mvimg.GetPointData().GetScalars())
nFrames=mvNode.GetNumberOfFrames()
# get a numpy reference to the label image buffer
labelimg=labelNode.GetImageData()
nplabelimg=vtk.util.numpy_support.vtk_to_numpy(labelimg.GetPointData().GetScalars())
# get the times as a list:
time=np.float(mvNode.GetAttribute('MultiVolume.FrameLabels',','))
if len(time) != nFrames:
return
# we follow the multivolume explorer logic, but use numpy, instead of loops
# this dictionary holds all indices labeld with a given label value
labelPixelCurves={}
# get the labels that are defined in the map:
labels=np.unique(nplabelimg)
# 0 means no label, we remove this from the labels:
labels=labels[labels!=0]
for labelValue in labels:
# get the indices of all pixels with the given label value
labelindex=np.where(nplabelimg == labelValue)[0] # where returns a tuple, hence the [0]
# and use these indices to get the pixel curves from the mvNode image
pixelcurves=npmvimg[labelindex,:]
# store the pixel curves for each label in the dictionary:
labelPixelCurves[labelValue]=pixelcurves
# you could save the pixelcurves simply as a textfile with np.savetxt:
#try:
# np.savetxt(filename, pixelcurves
#except IOError:
# print "Failed to write %s" % filename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment