def SetInput(self, Input): """Set the current input image """ import itk img = itk.image( Input ) if img : # Update to try to avoid to exit if a c++ exception is throwed # sadely, it will not prevent the program to exit later... # a real fix would be to wrap c++ exception in vtk img.UpdateOutputInformation() img.Update() # release the previous filters self.clear() itk.pipeline.SetInput( self, img ) # flip the image to get the same representation than the vtk one self.connect( itk.FlipImageFilter[img].New() ) axes = self[0].GetFlipAxes() axes.SetElement(1, True) self[0].SetFlipAxes(axes) # change the spacing while still keeping the ratio to workaround vtk bug # when spacing is very small spacing_ = itk.spacing(img) normSpacing = [] for i in range(0, spacing_.Size()): normSpacing.append( spacing_.GetElement(i) / spacing_.GetElement(0) ) self.connect( itk.ChangeInformationImageFilter[img].New( OutputSpacing=normSpacing, ChangeSpacing=True ) ) # now really convert the data self.connect( itk.ImageToVTKImageFilter[img].New() ) self.widget.SetInput( self[-1].GetImporter() )
def hist(input, bins=256, log=True, xrange=None, yrange=None, title="WrapITK histogram"): """Draw the histogram of the input image using http://gnuplot-py.sourceforge.net/ (http://gnuplot-py.sourceforge.net/). """ import Gnuplot, itk, numpy, math img = itk.image(input) a = itk.PyBuffer[img].GetArrayFromImage(img) hist = numpy.histogram(a, bins=bins) x = hist[1] y = hist[0] if log: y = map(math.log10, y) if xrange == None: xrange = ( min(x)-1, max(x)+1 ) g = Gnuplot.Gnuplot() g.title( title ) g.xlabel('Pixel values') if log: g.ylabel('log10( freq )') else: g.ylabel('freq') g('set data style boxes') g('set style fill solid border -1') g.set_range( "xrange", xrange ) if yrange != None: g.set_range( "yrange", yrange ) g.plot( zip( x, y ) ) return g
def hist(input, bins=256, log=True, xrange=None, yrange=None, title="WrapITK histogram"): """Draw the histogram of the input image using http://gnuplot-py.sourceforge.net/ (http://gnuplot-py.sourceforge.net/). """ import Gnuplot, itk, numpy, math img = itk.image(input) a = itk.PyBuffer[img].GetArrayFromImage(img) hist = numpy.histogram(a, bins=bins) x = hist[1] y = hist[0] if log: y = map(math.log10, y) if xrange == None: xrange = (min(x) - 1, max(x) + 1) g = Gnuplot.Gnuplot() g.title(title) g.xlabel('Pixel values') if log: g.ylabel('log10( freq )') else: g.ylabel('freq') g('set data style boxes') g('set style fill solid border -1') g.set_range("xrange", xrange) if yrange != None: g.set_range("yrange", yrange) g.plot(zip(x, y)) return g
def number_of_objects( i ): """Returns the number of objets in the image. i: the input LabelImage """ i.UpdateLargestPossibleRegion() i = itk.image(i) return i.GetNumberOfLabelObjects()
def SetInput(self, input): import itk img = itk.image(input) self.__input__ = img if img: # Update to try to avoid to exit if a c++ exception is throwed # sadely, it will not prevent the program to exit later... # a real fix would be to wrap c++ exception in vtk img.UpdateOutputInformation() img.Update() # flip the image to get the same representation than the vtk one self.__flipper__ = itk.FlipImageFilter[img].New(Input=img) axes = self.__flipper__.GetFlipAxes() axes.SetElement(1, True) self.__flipper__.SetFlipAxes(axes) # change the spacing while still keeping the ratio to workaround vtk bug # when spacing is very small spacing_ = itk.spacing(img) normSpacing = [] for i in range(0, spacing_.Size()): normSpacing.append( spacing_.GetElement(i) / spacing_.GetElement(0)) self.__changeInfo__ = itk.ChangeInformationImageFilter[img].New( self.__flipper__, OutputSpacing=normSpacing, ChangeSpacing=True) # now really convert the data self.__itkvtkConverter__ = itk.ImageToVTKImageFilter[img].New( self.__changeInfo__) self.__volumeMapper__.SetInput( self.__itkvtkConverter__.GetOutput()) # needed to avoid warnings # self.__itkvtkConverter__.GetOutput() must be callable import vtk if not self.__outline__: self.__outline__ = vtk.vtkOutlineFilter() self.__outline__.SetInput(self.__itkvtkConverter__.GetOutput()) self.__outlineMapper__ = vtk.vtkPolyDataMapper() self.__outlineMapper__.SetInput(self.__outline__.GetOutput()) self.__outlineActor__ = vtk.vtkActor() self.__outlineActor__.SetMapper(self.__outlineMapper__) self.__ren__.AddActor(self.__outlineActor__) else: self.__outline__.SetInput(self.__itkvtkConverter__.GetOutput()) self.Render()
def attribute_list( i, name ): """Returns a list of the specified attributes for the objects in the image. i: the input LabelImage name: the attribute name """ i = itk.image(i) relabel = itk.StatisticsRelabelLabelMapFilter[i].New(i, Attribute=name, ReverseOrdering=True, InPlace=False) relabel.UpdateLargestPossibleRegion() r = relabel.GetOutput() l = [] for i in range(1, r.GetNumberOfLabelObjects()+1): l.append( r.GetLabelObject(i).__getattribute__("Get"+name)() ) return l
def attribute_dict( i, name ): """Returns a dict with the attribute values in keys and a list of the corresponding objects in value i: the input LabelImage name: the name of the attribute """ i = itk.image(i) relabel = itk.StatisticsRelabelLabelMapFilter[i].New(i, Attribute=name, ReverseOrdering=True, InPlace=False) relabel.UpdateLargestPossibleRegion() r = relabel.GetOutput() d = {} for i in range(1, r.GetNumberOfLabelObjects()+1): lo = r.GetLabelObject(i) v = lo.__getattribute__("Get"+name)() l = d.get( v, [] ) l.append( lo ) d[v] = l return d
def SetInput(self, input) : import itk img = itk.image(input) self.__input__ = img if img : # Update to try to avoid to exit if a c++ exception is throwed # sadely, it will not prevent the program to exit later... # a real fix would be to wrap c++ exception in vtk img.UpdateOutputInformation() img.Update() # flip the image to get the same representation than the vtk one self.__flipper__ = itk.FlipImageFilter[img].New(Input=img) axes = self.__flipper__.GetFlipAxes() axes.SetElement(1, True) self.__flipper__.SetFlipAxes(axes) # change the spacing while still keeping the ratio to workaround vtk bug # when spacing is very small spacing_ = itk.spacing(img) normSpacing = [] for i in range(0, spacing_.Size()): normSpacing.append( spacing_.GetElement(i) / spacing_.GetElement(0) ) self.__changeInfo__ = itk.ChangeInformationImageFilter[img].New(self.__flipper__, OutputSpacing=normSpacing, ChangeSpacing=True) # now really convert the data self.__itkvtkConverter__ = itk.ImageToVTKImageFilter[img].New(self.__changeInfo__) self.__volumeMapper__.SetInput(self.__itkvtkConverter__.GetOutput()) # needed to avoid warnings # self.__itkvtkConverter__.GetOutput() must be callable import vtk if not self.__outline__ : self.__outline__ = vtk.vtkOutlineFilter() self.__outline__.SetInput(self.__itkvtkConverter__.GetOutput()) self.__outlineMapper__ = vtk.vtkPolyDataMapper() self.__outlineMapper__.SetInput(self.__outline__.GetOutput()) self.__outlineActor__ = vtk.vtkActor() self.__outlineActor__.SetMapper(self.__outlineMapper__) self.__ren__.AddActor(self.__outlineActor__) else : self.__outline__.SetInput(self.__itkvtkConverter__.GetOutput()) self.Render()
def __init__(self, InputImage, sigmaArray, ammount): """ Simple workflow implementing unsharp masking. """ im = itk.image(InputImage) InType = itk.class_(im) self.gaussianSmooth = itk.SmoothingRecursiveGaussianImageFilter[InType,InType].New(\ InputImage, SigmaArray = sigmaArray) self.substract = itk.SubtractImageFilter[InType,InType,InType].New(\ Input1 = InputImage, Input2 = self.gaussianSmooth.GetOutput()) self.shiftScale = itk.ShiftScaleImageFilter[InType,InType].New(\ Input = self.substract.GetOutput(), Scale = ammount, Shift = 0) self.addFilter = itk.AddImageFilter[InType,InType,InType].New(\ Input1 = self.shiftScale.GetOutput(), Input2 = InputImage)
# test ctype assert itk.ctype("unsigned short") == itk.US assert itk.ctype(" unsigned \n short \t ") == itk.US assert itk.ctype("signed short") == itk.SS assert itk.ctype("short") == itk.SS try: itk.ctype("dummy") raise Exception("unknown C type should send an exception") except KeyError: pass # test output assert itk.output(reader) == reader.GetOutput() assert itk.output(1) == 1 # test the deprecated image assert itk.image(reader) == reader.GetOutput() assert itk.image(1) == 1 # test size s = itk.size(reader) assert s[0] == s[1] == 256 s = itk.size(reader.GetOutput()) assert s[0] == s[1] == 256 # test physical size s = itk.physical_size(reader) assert s[0] == s[1] == 256.0 s = itk.physical_size(reader.GetOutput()) assert s[0] == s[1] == 256.0 # test spacing
assert itk.ctype("unsigned short") == itk.US assert itk.ctype(" unsigned \n short \t ") == itk.US assert itk.ctype("signed short") == itk.SS assert itk.ctype("short") == itk.SS try: itk.ctype("dummy") raise Exception("unknown C type should send an exception") except KeyError: pass # test output assert itk.output(reader) == reader.GetOutput() assert itk.output(1) == 1 # test the deprecated image assert itk.image(reader) == reader.GetOutput() assert itk.image(1) == 1 # test size s = itk.size(reader) assert s[0] == s[1] == 256 s = itk.size(reader.GetOutput()) assert s[0] == s[1] == 256 # test physical size s = itk.physical_size(reader) assert s[0] == s[1] == 256.0 s = itk.physical_size(reader.GetOutput()) assert s[0] == s[1] == 256.0 # test spacing
raise Exception("unknown class should send an exception") except KeyError: pass # test ctype assert itk.ctype("unsigned short") == itk.US assert itk.ctype(" unsigned \n short \t ") == itk.US try: itk.ctype("dummy") raise Exception("unknown C type should send an exception") except KeyError: pass # test image assert repr(itk.image(reader)) == repr(reader.GetOutput().GetPointer()) assert repr(itk.image(reader.GetOutput())) == repr(reader.GetOutput().GetPointer()) assert repr(itk.image(reader.GetOutput().GetPointer())) == repr(reader.GetOutput().GetPointer()) assert itk.image(1) == 1 # test strel # should work with the image type, an image instance or a filter # and should work with a list, a tuple, an int or an itk.Size for s in [2, (2, 2), [2, 2], itk.Size[2](2)] : st = itk.strel(dim, s) (tpl, param) = itk.template(st) assert tpl == itk.FlatStructuringElement assert param[0] == dim assert st.GetRadius().GetElement(0) == st.GetRadius().GetElement(1) == 2