def mhd2npy(fn): # back the other way: r = vtk.vtkMetaImageReader() r.SetFileName(fn) r.Update() image = r.GetOutput() vtk_data = image.GetPointData().GetScalars() numpy_data = ns.vtk_to_numpy(vtk_data) dims = image.GetDimensions() numpy_data = numpy_data.reshape(dims[2], dims[1], dims[0]) numpy_data = numpy_data.transpose(2, 1, 0) return numpy_data
def loadSingleFile(self): loader = self.fileLoader loader.loadFile() if loader.accepted: loader.setDir(os.path.dirname(str(loader.selectedFile))) self.disableSlider() self.dicomReader = vtkgdcm.vtkGDCMImageReader() self.dicomReader.SetFileName(str(loader.selectedFile)) print(dir(self.dicomReader)) print(self.dicomReader.GetScale()) self.dicomReader.Update() imageData = self.dicomReader.GetOutput() size = imageData.GetDimensions() width = size[0] height = size[1] self.vtkWidget.setMaximumSize(QtCore.QSize(width, height)) self.vtkWidget.setMinimumSize(QtCore.QSize(width, height)) RefDs = dicom.read_file(str(loader.selectedFile)) ConstPixelDims = (int(RefDs.Rows), int(RefDs.Columns), 1) pointData = imageData.GetPointData() arrayData = pointData.GetArray(0) arrayDicom = numpy_support.vtk_to_numpy(arrayData) arrayDicom = arrayDicom.reshape(ConstPixelDims, order='F') shape = arrayDicom.shape wtf = arrayDicom.reshape(shape[0], shape[1]) wtf = numpy.fliplr(wtf).transpose() max = numpy.max(wtf) min = numpy.min(wtf) print(numpy.max(wtf)) print(numpy.min(wtf)) grad = numpy.gradient(wtf) print(wtf) computed = numpy.sqrt(numpy.square(grad[0]) + numpy.square(grad[1])) #self.proc.start() ax = self.figure.add_subplot(111) ax.imshow(wtf, interpolation="nearest", cmap=plt.get_cmap('gray'), vmin=0, vmax=max) self.canvas.draw() #points = vtk.vtkPoints() #points.SetNumberOfPoints(2) #points.Allocate(2) #points.InsertPoint(0, 100, 100, 0.001) #points.InsertPoint(0, 200, 200, 0.001) #cells = vtk.vtkCellArray() #cells.Initialize() #line = vtk.vtkLine() #line.GetPointIds().SetId(0,0) #line.GetPointIds().SetId(1,1) #cells.InsertNextCell(line) #poly = vtk.vtkPolyData() #poly.Initialize() #poly.SetPoints(points) #poly.SetLines(cells) #poly.Modified() #mapper = vtk.vtkPolyDataMapper2D() #print(dir(mapper)) #mapper.SetInput(poly) #mapper.ScalarVisibilityOn() #mapper.SetScalarModeToUsePointData() #mapper.Update() #self.drawLine([(200,200), (300,300)]) #actor = vtk.vtkActor2D() #actor.SetMapper(mapper) blend = vtk.vtkImageBlend() blend.AddInputConnection(self.dicomReader.GetOutputPort()) #blend.AddInputConnection(actor.GetOutputPort()) self.viewer.SetInputConnection(blend.GetOutputPort()) #print(dir(self.viewer.GetRenderer())) #self.viewer.GetRenderer().AddActor2D(actor) #self.viewer.SetInputConnection(self.dicomReader.GetOutputPort()) self.viewer.SetZSlice(0) self.getMedicalData() self.iren.ReInitialize() self.iren.Render() self.iren.Start() #actor = vtk.vtkImageActor() #self.viewer.GetRenderer().AddActor(actor) self.viewer.GetRenderer().Render()