def GetSlices(self, orientation, slice_number): if self.buffer_slices[orientation].index == slice_number: if self.buffer_slices[orientation].vtk_image: image = self.buffer_slices[orientation].vtk_image else: n_image = self.get_image_slice(orientation, slice_number) image = converters.to_vtk(n_image, self.spacing, slice_number, orientation) ww_wl_image = self.do_ww_wl(image) image = self.do_colour_image(ww_wl_image) if self.current_mask and self.current_mask.is_shown: if self.buffer_slices[orientation].vtk_mask: print "Getting from buffer" mask = self.buffer_slices[orientation].vtk_mask else: print "Do not getting from buffer" n_mask = self.get_mask_slice(orientation, slice_number) mask = converters.to_vtk(n_mask, self.spacing, slice_number, orientation) mask = self.do_colour_mask(mask) self.buffer_slices[orientation].mask = n_mask final_image = self.do_blend(image, mask) self.buffer_slices[orientation].vtk_mask = mask else: final_image = image self.buffer_slices[orientation].vtk_image = image else: n_image = self.get_image_slice(orientation, slice_number) image = converters.to_vtk(n_image, self.spacing, slice_number, orientation) ww_wl_image = self.do_ww_wl(image) image = self.do_colour_image(ww_wl_image) if self.current_mask and self.current_mask.is_shown: n_mask = self.get_mask_slice(orientation, slice_number) mask = converters.to_vtk(n_mask, self.spacing, slice_number, orientation) mask = self.do_colour_mask(mask) final_image = self.do_blend(image, mask) else: n_mask = None final_image = image mask = None self.buffer_slices[orientation].index = slice_number self.buffer_slices[orientation].image = n_image self.buffer_slices[orientation].mask = n_mask self.buffer_slices[orientation].vtk_image = image self.buffer_slices[orientation].vtk_mask = mask return final_image
def LoadImage(self): slice_data = slice_.Slice() n_array = slice_data.matrix spacing = slice_data.spacing slice_number = 0 orientation = 'AXIAL' image = converters.to_vtk(n_array, spacing, slice_number, orientation) self.image = image
def CreateSurface(self, roi): if self.from_binary: a_mask = numpy.array(self.mask[roi.start + 1: roi.stop + 1, 1:, 1:]) image = converters.to_vtk(a_mask, self.spacing, roi.start, "AXIAL") del a_mask else: a_image = numpy.array(self.image[roi]) if self.algorithm == u'InVesalius 3.b2': a_mask = numpy.array(self.mask[roi.start + 1: roi.stop + 1, 1:, 1:]) a_image[a_mask == 1] = a_image.min() - 1 a_image[a_mask == 254] = (self.min_value + self.max_value) / 2.0 image = converters.to_vtk(a_image, self.spacing, roi.start, "AXIAL") gauss = vtk.vtkImageGaussianSmooth() gauss.SetInput(image) gauss.SetRadiusFactor(0.3) gauss.ReleaseDataFlagOn() gauss.Update() del image image = gauss.GetOutput() del gauss del a_mask else: image = converters.to_vtk(a_image, self.spacing, roi.start, "AXIAL") del a_image if self.imagedata_resolution: # image = iu.ResampleImage3D(image, self.imagedata_resolution) image = ResampleImage3D(image, self.imagedata_resolution) flip = vtk.vtkImageFlip() flip.SetInput(image) flip.SetFilteredAxis(1) flip.FlipAboutOriginOn() flip.ReleaseDataFlagOn() flip.Update() del image image = flip.GetOutput() del flip #filename = tempfile.mktemp(suffix='_%s.vti' % (self.pid)) #writer = vtk.vtkXMLImageDataWriter() #writer.SetInput(mask_vtk) #writer.SetFileName(filename) #writer.Write() #print "Writing piece", roi, "to", filename # Create vtkPolyData from vtkImageData #print "Generating Polydata" #if self.mode == "CONTOUR": #print "Contour" contour = vtk.vtkContourFilter() contour.SetInput(image) #contour.SetInput(flip.GetOutput()) if self.from_binary: contour.SetValue(0, 127) # initial threshold else: contour.SetValue(0, self.min_value) # initial threshold contour.SetValue(1, self.max_value) # final threshold contour.ComputeScalarsOn() contour.ComputeGradientsOn() contour.ComputeNormalsOn() contour.ReleaseDataFlagOn() contour.Update() #contour.AddObserver("ProgressEvent", lambda obj,evt: # self.SendProgress(obj, _("Generating 3D surface..."))) polydata = contour.GetOutput() del image del contour #else: #mode == "GRAYSCALE": #mcubes = vtk.vtkMarchingCubes() #mcubes.SetInput(flip.GetOutput()) #mcubes.SetValue(0, self.min_value) #mcubes.SetValue(1, self.max_value) #mcubes.ComputeScalarsOff() #mcubes.ComputeGradientsOff() #mcubes.ComputeNormalsOff() #mcubes.AddObserver("ProgressEvent", lambda obj,evt: #self.SendProgress(obj, _("Generating 3D surface..."))) #polydata = mcubes.GetOutput() #triangle = vtk.vtkTriangleFilter() #triangle.SetInput(polydata) #triangle.AddObserver("ProgressEvent", lambda obj,evt: #self.SendProgress(obj, _("Generating 3D surface..."))) #triangle.Update() #polydata = triangle.GetOutput() #if self.decimate_reduction: ##print "Decimating" #decimation = vtk.vtkDecimatePro() #decimation.SetInput(polydata) #decimation.SetTargetReduction(0.3) #decimation.AddObserver("ProgressEvent", lambda obj,evt: #self.SendProgress(obj, _("Generating 3D surface..."))) ##decimation.PreserveTopologyOn() #decimation.SplittingOff() #decimation.BoundaryVertexDeletionOff() #polydata = decimation.GetOutput() self.pipe.send(None) filename = tempfile.mktemp(suffix='_%s.vtp' % (self.pid)) writer = vtk.vtkXMLPolyDataWriter() writer.SetInput(polydata) writer.SetFileName(filename) writer.Write() print "Writing piece", roi, "to", filename del polydata del writer self.q_out.put(filename)
def bitmap2memmap(files, slice_size, orientation, spacing, resolution_percentage): """ From a list of dicom files it creates memmap file in the temp folder and returns it and its related filename. """ message = _("Generating multiplanar visualization...") update_progress= vtk_utils.ShowProgress(len(files) - 1, dialog_type = "ProgressDialog") temp_file = tempfile.mktemp() if orientation == 'SAGITTAL': if resolution_percentage == 1.0: shape = slice_size[1], slice_size[0], len(files) else: shape = math.ceil(slice_size[1]*resolution_percentage),\ math.ceil(slice_size[0]*resolution_percentage), len(files) elif orientation == 'CORONAL': if resolution_percentage == 1.0: shape = slice_size[1], len(files), slice_size[0] else: shape = math.ceil(slice_size[1]*resolution_percentage), len(files),\ math.ceil(slice_size[0]*resolution_percentage) else: if resolution_percentage == 1.0: shape = len(files), slice_size[1], slice_size[0] else: shape = len(files), math.ceil(slice_size[1]*resolution_percentage),\ math.ceil(slice_size[0]*resolution_percentage) if resolution_percentage == 1.0: matrix = numpy.memmap(temp_file, mode='w+', dtype='int16', shape=shape) cont = 0 max_scalar = None min_scalar = None xy_shape = None first_resample_entry = False for n, f in enumerate(files): image_as_array = bitmap_reader.ReadBitmap(f) image = converters.to_vtk(image_as_array, spacing=spacing,\ slice_number=1, orientation=orientation.upper()) if resolution_percentage != 1.0: image_resized = ResampleImage2D(image, px=None, py=None,\ resolution_percentage = resolution_percentage, update_progress = None) yx_shape = image_resized.GetDimensions()[1], image_resized.GetDimensions()[0] if not(first_resample_entry): shape = shape[0], yx_shape[0], yx_shape[1] matrix = numpy.memmap(temp_file, mode='w+', dtype='int16', shape=shape) first_resample_entry = True image = image_resized min_aux, max_aux = image.GetScalarRange() if min_scalar is None or min_aux < min_scalar: min_scalar = min_aux if max_scalar is None or max_aux > max_scalar: max_scalar = max_aux array = numpy_support.vtk_to_numpy(image.GetPointData().GetScalars()) if array.dtype == 'uint16': array = array - 32768/2 array = array.astype("int16") if orientation == 'CORONAL': array.shape = matrix.shape[0], matrix.shape[2] matrix[:, n, :] = array[:,::-1] elif orientation == 'SAGITTAL': array.shape = matrix.shape[0], matrix.shape[1] # TODO: Verify if it's necessary to add the slices swapped only in # sagittal rmi or only in # Rasiane's case or is necessary in all # sagittal cases. matrix[:, :, n] = array[:,::-1] else: array.shape = matrix.shape[1], matrix.shape[2] matrix[n] = array update_progress(cont,message) cont += 1 matrix.flush() scalar_range = min_scalar, max_scalar return matrix, scalar_range, temp_file
def CreateSurface(self, roi): if self.from_binary: a_mask = numpy.array(self.mask[roi.start + 1:roi.stop + 1, 1:, 1:]) image = converters.to_vtk(a_mask, self.spacing, roi.start, "AXIAL") del a_mask else: a_image = numpy.array(self.image[roi]) if self.algorithm == u'InVesalius 3.b2': a_mask = numpy.array(self.mask[roi.start + 1:roi.stop + 1, 1:, 1:]) a_image[a_mask == 1] = a_image.min() - 1 a_image[a_mask == 254] = (self.min_value + self.max_value) / 2.0 image = converters.to_vtk(a_image, self.spacing, roi.start, "AXIAL") gauss = vtk.vtkImageGaussianSmooth() gauss.SetInput(image) gauss.SetRadiusFactor(0.3) gauss.ReleaseDataFlagOn() gauss.Update() del image image = gauss.GetOutput() del gauss del a_mask else: image = converters.to_vtk(a_image, self.spacing, roi.start, "AXIAL") del a_image if self.imagedata_resolution: # image = iu.ResampleImage3D(image, self.imagedata_resolution) image = ResampleImage3D(image, self.imagedata_resolution) flip = vtk.vtkImageFlip() flip.SetInput(image) flip.SetFilteredAxis(1) flip.FlipAboutOriginOn() flip.ReleaseDataFlagOn() flip.Update() del image image = flip.GetOutput() del flip #filename = tempfile.mktemp(suffix='_%s.vti' % (self.pid)) #writer = vtk.vtkXMLImageDataWriter() #writer.SetInput(mask_vtk) #writer.SetFileName(filename) #writer.Write() #print "Writing piece", roi, "to", filename # Create vtkPolyData from vtkImageData #print "Generating Polydata" #if self.mode == "CONTOUR": #print "Contour" contour = vtk.vtkContourFilter() contour.SetInput(image) #contour.SetInput(flip.GetOutput()) if self.from_binary: contour.SetValue(0, 127) # initial threshold else: contour.SetValue(0, self.min_value) # initial threshold contour.SetValue(1, self.max_value) # final threshold contour.ComputeScalarsOn() contour.ComputeGradientsOn() contour.ComputeNormalsOn() contour.ReleaseDataFlagOn() contour.Update() #contour.AddObserver("ProgressEvent", lambda obj,evt: # self.SendProgress(obj, _("Generating 3D surface..."))) polydata = contour.GetOutput() del image del contour #else: #mode == "GRAYSCALE": #mcubes = vtk.vtkMarchingCubes() #mcubes.SetInput(flip.GetOutput()) #mcubes.SetValue(0, self.min_value) #mcubes.SetValue(1, self.max_value) #mcubes.ComputeScalarsOff() #mcubes.ComputeGradientsOff() #mcubes.ComputeNormalsOff() #mcubes.AddObserver("ProgressEvent", lambda obj,evt: #self.SendProgress(obj, _("Generating 3D surface..."))) #polydata = mcubes.GetOutput() #triangle = vtk.vtkTriangleFilter() #triangle.SetInput(polydata) #triangle.AddObserver("ProgressEvent", lambda obj,evt: #self.SendProgress(obj, _("Generating 3D surface..."))) #triangle.Update() #polydata = triangle.GetOutput() #if self.decimate_reduction: ##print "Decimating" #decimation = vtk.vtkDecimatePro() #decimation.SetInput(polydata) #decimation.SetTargetReduction(0.3) #decimation.AddObserver("ProgressEvent", lambda obj,evt: #self.SendProgress(obj, _("Generating 3D surface..."))) ##decimation.PreserveTopologyOn() #decimation.SplittingOff() #decimation.BoundaryVertexDeletionOff() #polydata = decimation.GetOutput() self.pipe.send(None) filename = tempfile.mktemp(suffix='_%s.vtp' % (self.pid)) writer = vtk.vtkXMLPolyDataWriter() writer.SetInput(polydata) writer.SetFileName(filename) writer.Write() print "Writing piece", roi, "to", filename del polydata del writer self.q_out.put(filename)