def __init__(self, module_manager): SimpleVTKClassModuleBase.__init__( self, module_manager, vtk.vtkImageWriter(), 'Writing vtkImage.', ('vtkImage',), (), replaceDoc=True, inputFunctions=None, outputFunctions=None)
def MeshToVolume(Filename): reader = vtk.vtkPolyDataReader() pol2stenc = vtk.vtkPolyDataToImageStencil() imgstenc = vtk.vtkImageStencil() reader.SetFileName(os.path.join(subjects_dir,subject_dir,Filename)) reader.Update() ref_mesh = reader.GetOutput() ref_volume = vtk.vtkImageData() # define output volume dimension spacing = (0.5,0.5,0.5) ref_volume.SetSpacing(spacing) bounds = ref_mesh.GetBounds() dim = [math.ceil(bounds[ii*2+1] - bounds[ii*2] / spacing[ii]) for ii in range(0,3)] origin = [bounds[ii*2] + spacing[ii] / 2 for ii in range(0,3)] extent = (0,dim[0] - 1,0,dim[1] -1 ,0,dim[2]-1) ref_volume.SetOrigin(origin) ref_volume.SetDimensions(dim) ref_volume.SetExtent(extent) ref_volume.SetScalarTypeToUnsignedChar() ref_volume.AllocateScalars() # Fill the image with white voxels for i in range(0,ref_volume.GetNumberOfPoints()): ref_volume.GetPointData().GetScalars().SetTuple1(i,255) print ref_volume.GetNumberOfPoints() pol2stenc.SetInput(ref_mesh) pol2stenc.SetOutputOrigin(origin) pol2stenc.SetOutputSpacing(spacing) pol2stenc.SetOutputWholeExtent(ref_volume.GetExtent()) pol2stenc.Update() imgstenc.SetInput(ref_volume) imgstenc.SetStencil(pol2stenc.GetOutput()) imgstenc.ReverseStencilOff() imgstenc.SetBackgroundValue(0) imgstenc.Update() tmp = imgstenc.GetOutput() writer = vtk.vtkImageWriter() writer.SetFileName('prova.nii.gz') writer.SetInput(ref_volume) writer.Update() out = v2n(tmp.GetPointData().GetScalars()) return np.reshape(out, (dim[0],dim[1],dim[2]))
def saveRawImage(name, image): """ save a Raw Image (.img,.raw) from a vtkImageData """ writer = vtk.vtkImageWriter() writer.SetFileName(name) writer.SetInput(image) writer.SetFileDimensionality(3) writer.Write()
def __init__(self, module_manager): SimpleVTKClassModuleBase.__init__(self, module_manager, vtk.vtkImageWriter(), 'Writing vtkImage.', ('vtkImage', ), (), replaceDoc=True, inputFunctions=None, outputFunctions=None)
def saveRawImage(name, image, coding='uchar'): """ save a Raw Image (.img,.raw) from a vtkImageData """ imgCast = castImage(image, coding) writer = vtk.vtkImageWriter() writer.SetFileName(name) writer.SetInput(imgCast) writer.SetFileDimensionality(3) writer.Write()
def average_all_images(image_datas, fname='average_image.raw'): # how cool is this, this class was from my masters' thesis. # though the new vtk coding is not recognizable anymore... sum = vtk.vtkImageWeightedSum() weights = vtk.vtkDoubleArray() for image in image_datas: weights.InsertNextTuple1(1.0) sum.AddInput(image) sum.SetWeights(weights) sum.NormalizeByWeightOn() sum.Update() writer = vtk.vtkImageWriter() writer.SetInput(sum.GetOutput()) writer.SetFileName(fname) writer.SetFileDimensionality(3) writer.Write() return(sum.GetOutput())
def writeImage( image, filename, verbose=1): myVTK.myPrint(verbose, "*** writeImage: " + filename + " ***") if ('vtk' in filename): image_writer = vtk.vtkImageWriter() elif ('vti' in filename): image_writer = vtk.vtkXMLImageDataWriter() else: assert 0, "File must be .vtk or .vti. Aborting." image_writer.SetFileName(filename) image_writer.SetInputData(image) image_writer.Update() image_writer.Write()
def writeImage(image, filename, verbose=0): myVTK.myPrint(verbose, "*** writeImage: " + filename + " ***") if ('vtk' in filename): image_writer = vtk.vtkImageWriter() elif ('vti' in filename): image_writer = vtk.vtkXMLImageDataWriter() else: assert 0, "File must be .vtk or .vti. Aborting." myVTK.myPrint(verbose - 1, "n_points = " + str(image.GetNumberOfPoints())) image_writer.SetFileName(filename) if (vtk.vtkVersion.GetVTKMajorVersion() >= 6): image_writer.SetInputData(image) else: image_writer.SetInput(image) image_writer.Update() image_writer.Write()
def writeImage( image, filename, verbose=0): mypy.my_print(verbose, "*** writeImage: "+filename+" ***") if ('vtk' in filename): image_writer = vtk.vtkImageWriter() elif ('vti' in filename): image_writer = vtk.vtkXMLImageDataWriter() else: assert 0, "File must be .vtk or .vti. Aborting." mypy.my_print(verbose, "n_points = "+str(image.GetNumberOfPoints())) image_writer.SetFileName(filename) if (vtk.vtkVersion.GetVTKMajorVersion() >= 6): image_writer.SetInputData(image) else: image_writer.SetInput(image) image_writer.Update() image_writer.Write()
idx = numpy.nonzero(data_fiber_counts[sidx]) LI = numpy.zeros(data_fiber_counts[0].shape) LI[idx] = numpy.divide(data_fiber_LI[sidx][idx], data_fiber_counts[sidx][idx]) sum_LI += LI sum_LIR[LI > 0] += LI[LI > 0] sum_LIL[LI < 0] += LI[LI < 0] flip_sum_LIL = sum_LIL[::-1,:,:] symm_LIL = sum_LIL + flip_sum_LIL flip_sum_LIR = sum_LIR[::-1,:,:] symm_LIR = sum_LIR + flip_sum_LIR right_img = wma.filter.array_to_vtk(symm_LIR) writer = vtk.vtkImageWriter() writer.SetFilePrefix('right') writer.SetInput(right_img) writer.SetFileDimensionality(2) writer.SetFilePattern('%s.%03d') writer.Write() left_img = wma.filter.array_to_vtk(symm_LIL) writer.SetFilePrefix('left') writer.SetInput(left_img) writer.Write() # Read groups file, in theory handedness = numpy.array([75, 85, 55, -35, -90, -85, -95, 70, -75, -25, 60, -70, 75, 100, 75, -35, -45, 90, 85, -75, 5, -80, 50, -40, -95, 100])
def writeImageData(self, fname): writer = vtk.vtkImageWriter() writer.SetInput(self.reader.GetOutput()) writer.SetFileName(fname) writer.SetFileDimensionality(3) writer.Write()
def MeshToVolume(Filename): reader = vtk.vtkPolyDataReader() pol2stenc = vtk.vtkPolyDataToImageStencil() imgstenc = vtk.vtkImageStencil() reader.SetFileName(os.path.join(subjects_dir, subject_dir, Filename)) reader.Update() ref_mesh = reader.GetOutput() ref_volume = vtk.vtkImageData() # define output volume dimension spacing = (0.5, 0.5, 0.5) ref_volume.SetSpacing(spacing) bounds = ref_mesh.GetBounds() dim = [ math.ceil(bounds[ii * 2 + 1] - bounds[ii * 2] / spacing[ii]) for ii in range(0, 3) ] origin = [bounds[ii * 2] + spacing[ii] / 2 for ii in range(0, 3)] extent = (0, dim[0] - 1, 0, dim[1] - 1, 0, dim[2] - 1) ref_volume.SetOrigin(origin) ref_volume.SetDimensions(dim) ref_volume.SetExtent(extent) ref_volume.SetScalarTypeToUnsignedChar() ref_volume.AllocateScalars() # Fill the image with white voxels for i in range(0, ref_volume.GetNumberOfPoints()): ref_volume.GetPointData().GetScalars().SetTuple1(i, 255) print ref_volume.GetNumberOfPoints() pol2stenc.SetInput(ref_mesh) pol2stenc.SetOutputOrigin(origin) pol2stenc.SetOutputSpacing(spacing) pol2stenc.SetOutputWholeExtent(ref_volume.GetExtent()) pol2stenc.Update() imgstenc.SetInput(ref_volume) imgstenc.SetStencil(pol2stenc.GetOutput()) imgstenc.ReverseStencilOff() imgstenc.SetBackgroundValue(0) imgstenc.Update() tmp = imgstenc.GetOutput() writer = vtk.vtkImageWriter() writer.SetFileName('prova.nii.gz') writer.SetInput(ref_volume) writer.Update() out = v2n(tmp.GetPointData().GetScalars()) return np.reshape(out, (dim[0], dim[1], dim[2]))