def AddShotNoise(inputImage, outputImage, noiseAmplitude, noiseFraction, extent): shotNoiseSource = vtk.vtkImageNoiseSource() shotNoiseSource.SetWholeExtent(extent) shotNoiseSource.SetMinimum(0.0) shotNoiseSource.SetMaximum(1.0) shotNoiseThresh1 = vtk.vtkImageThreshold() shotNoiseThresh1.SetInputConnection(shotNoiseSource.GetOutputPort()) shotNoiseThresh1.ThresholdByLower(1.0 - noiseFraction) shotNoiseThresh1.SetInValue(0) shotNoiseThresh1.SetOutValue(noiseAmplitude) shotNoiseThresh2 = vtk.vtkImageThreshold() shotNoiseThresh2.SetInputConnection(shotNoiseSource.GetOutputPort()) shotNoiseThresh2.ThresholdByLower(noiseFraction) shotNoiseThresh2.SetInValue(1.0 - noiseAmplitude) shotNoiseThresh2.SetOutValue(0.0) shotNoise = vtk.vtkImageMathematics() shotNoise.SetInputConnection(0, shotNoiseThresh1.GetOutputPort()) shotNoise.SetInputConnection(1, shotNoiseThresh2.GetOutputPort()) shotNoise.SetOperationToAdd() add = vtk.vtkImageMathematics() add.SetInputData(0, inputImage) add.SetInputConnection(1, shotNoise.GetOutputPort()) add.SetOperationToAdd() add.Update() outputImage.DeepCopy(add.GetOutput())
def __init__(self, module_manager): SimpleVTKClassModuleBase.__init__( self, module_manager, vtk.vtkImageNoiseSource(), 'Processing.', (), ('vtkImageData',), replaceDoc=True, inputFunctions=None, outputFunctions=None)
imageCanvas.DrawSegment(45, 45, 45, 210) imageCanvas.DrawSegment(45, 210, 210, 210) imageCanvas.DrawSegment(210, 210, 210, 45) imageCanvas.DrawSegment(210, 45, 45, 45) imageCanvas.DrawSegment(100, 150, 150, 100) imageCanvas.DrawSegment(110, 160, 160, 110) imageCanvas.DrawSegment(90, 140, 140, 90) imageCanvas.DrawSegment(120, 170, 170, 120) imageCanvas.DrawSegment(80, 130, 130, 80) imageCanvas.Update() shotNoiseAmplitude = 255.0 shotNoiseFraction = 0.1 # set shotNoiseExtent "1 256 1 256 0 0" shotNoiseSource = vtk.vtkImageNoiseSource() shotNoiseSource.SetWholeExtent(1, 256, 1, 256, 0, 0) # $shotNoiseExtent shotNoiseSource.SetMinimum(0.0) shotNoiseSource.SetMaximum(1.0) shotNoiseSource.ReleaseDataFlagOff() shotNoiseThresh1 = vtk.vtkImageThreshold() shotNoiseThresh1.SetInputConnection(shotNoiseSource.GetOutputPort()) shotNoiseThresh1.ThresholdByLower(1.0 - shotNoiseFraction) shotNoiseThresh1.SetInValue(0) shotNoiseThresh1.SetOutValue(shotNoiseAmplitude) shotNoiseThresh1.Update() shotNoiseThresh2 = vtk.vtkImageThreshold() shotNoiseThresh2.SetInputConnection(shotNoiseSource.GetOutputPort())
def main(argv): imageViewer = vtk.vtkImageViewer2() if len(argv) < 2: noiseSource = vtk.vtkImageNoiseSource() noiseSource.SetWholeExtent(0, 512, 0, 512, 0, 0) noiseSource.SetMinimum(0.0) noiseSource.SetMaximum(65535.0) # cast noise image to unsigned short imageCast = vtk.vtkImageCast() imageCast.SetInputConnection(noiseSource.GetOutputPort()) imageCast.SetOutputScalarTypeToUnsignedShort() imageCast.Update() # connect to image viewer pipeline imageViewer.SetInputConnection(imageCast.GetOutputPort()) else: # Parse input argument inputFilename = str(argv[1]) # Read the image tiffReader = vtk.vtkTIFFReader() if not tiffReader.CanReadFile(inputFilename): return tiffReader.SetFileName(inputFilename) # connect to image viewer pipeline imageViewer.SetInputConnection(tiffReader.GetOutputPort()) # Picker to pick pixels propPicker = vtk.vtkPropPicker() propPicker.PickFromListOn() # Give the picker a prop to pick imageActor = imageViewer.GetImageActor() propPicker.AddPickList(imageActor) # disable interpolation, so we can see each pixel imageActor.InterpolateOff() # Visualize renderWindowInteractor = vtk.vtkRenderWindowInteractor() imageViewer.SetupInteractor(renderWindowInteractor) imageViewer.SetSize(600, 600) renderer = imageViewer.GetRenderer() renderer.ResetCamera() renderer.GradientBackgroundOn() renderer.SetBackground(0.6, 0.6, 0.5) renderer.SetBackground2(0.3, 0.3, 0.2) # Annotate the image with window/level and mouse over pixel # information cornerAnnotation = vtk.vtkCornerAnnotation() cornerAnnotation.SetLinearFontScaleFactor(2) cornerAnnotation.SetNonlinearFontScaleFactor(1) cornerAnnotation.SetMaximumFontSize(20) cornerAnnotation.SetText(0, "Off Image") cornerAnnotation.SetText(3, "<window>\n<level>") cornerAnnotation.GetTextProperty().SetColor(1, 0, 0) imageViewer.GetRenderer().AddViewProp(cornerAnnotation) # Callback listens to MouseMoveEvents invoked by the interactor's style callback = vtkImageInteractionCallback() callback.SetViewer(imageViewer) callback.SetAnnotation(cornerAnnotation) callback.SetPicker(propPicker) # InteractorStyleImage allows for the following controls: # 1) middle mouse + move = camera pan # 2) left mouse + move = window/level # 3) right mouse + move = camera zoom # 4) middle mouse wheel scroll = zoom # 5) 'r' = reset window/level # 6) shift + 'r' = reset camera imageStyle = imageViewer.GetInteractorStyle() imageStyle.AddObserver('MouseMoveEvent', callback.Execute) if len(argv) > 1: imageViewer.GetImageActor().GetMapper().SetInputConnection( tiffReader.GetOutputPort()) renderWindowInteractor.Initialize() renderWindowInteractor.Start()
def createData(self, currentTimepoint): """ Create a test dataset within the parameters defined """ x,y,z = self.parameters["X"], self.parameters["Y"], self.parameters["Z"] if self.modified: print "Creating the time series data" self.createTimeSeries() if self.parameters["CreateAll"]: n = min(self.parameters["CacheAmount"], self.parameters["Time"]) for i in range(0, n): if i == currentTimepoint: print "Won't create timepoint %d, it'll be last"%i continue self.createData(i) print "\n\nGenerating timepoint %d"%currentTimepoint if currentTimepoint in self.imageCache: print "Returning cached image" return self.imageCache[currentTimepoint] print "Allocating image" if self.parameters["CreateNoise"]: print "Creating background noise" noiseSource = vtk.vtkImageNoiseSource() noiseSource.SetWholeExtent(0,x-1,0,y-1,0,z-1) noiseSource.SetMinimum(self.parameters["BackgroundNoiseMin"]) noiseSource.SetMaximum(self.parameters["BackgroundNoiseMax"]) castFilter = vtk.vtkImageCast() castFilter.SetOutputScalarTypeToUnsignedChar() castFilter.SetInputConnection(noiseSource.GetOutputPort()) information = vtk.vtkImageChangeInformation() information.SetInputConnection(castFilter.GetOutputPort()) information.SetOutputSpacing(self.spacing) image = information.GetOutput() image.Update() else: image = vtk.vtkImageData() image.SetScalarTypeToUnsignedChar() x,y,z = self.parameters["X"], self.parameters["Y"], self.parameters["Z"] image.SetDimensions((x,y,z)) image.AllocateScalars() image.SetSpacing(self.spacing) print "Initializing image" for iz in range(0,z): for iy in range(0,y): for ix in range(0,x): image.SetScalarComponentFromDouble(ix,iy,iz,0,0) if self.parameters["CreateNoise"]: noisePercentage = self.parameters["ShotNoiseAmount"] noiseAmount = (noisePercentage/100.0) * (x*y*z) print "Creating shot noise" else: noiseAmount = 0 shotNoiseMin = self.parameters["ShotNoiseMin"] shotNoiseMax = self.parameters["ShotNoiseMax"] shotNoiseDistr = self.parameters["ShotNoiseDistribution"] while noiseAmount > 0: rx,ry,rz = random.randint(0,x-1), random.randint(0,y-1), random.randint(0,z-1) shotInt = self.generateDistributionValue(shotNoiseDistr, shotNoiseMin, shotNoiseMax) image.SetScalarComponentFromDouble(rx,ry,rz,0,shotInt) noiseAmount -= 1 #shiftx, shifty, shiftz = self.shifts[currentTimepoint] print "Creating objects",currentTimepoint for oIter, (objN, (rx,ry,rz), size, objInt) in enumerate(self.objects[currentTimepoint]): #rx += shiftx #ry += shifty #rz += shiftz (rx,ry,rz), realSize, intList, voxelList = self.createObjectAt(image, rx,ry,rz, size, objInt) objMean, objStd, objStdErr = lib.Math.meanstdeverr(intList) # Change possible new size and com to object self.objects[currentTimepoint][oIter] = (objN, (rx,ry,rz), realSize, (objMean, objStdErr), voxelList) if self.parameters["ObjectsCreateSource"]: locator = vtk.vtkOBBTree() locator.SetDataSet(self.polydata) locator.BuildLocator() pointLocator = vtk.vtkPointLocator() pointLocator.SetDataSet(self.polydata) pointLocator.BuildLocator() objPolyTP = [] for objN, (cx, cy, cz), size, meanInt, voxelList in self.objects[currentTimepoint]: cxs = cx * self.spacing[0] cys = cy * self.spacing[1] czs = cz * self.spacing[2] locatorInside = locator.InsideOrOutside((cxs,cys,czs)) if locatorInside == -1: inside = 1 else: inside = 0 percVoxelsInside = 0.0 numIn = 0 for (vx,vy,vz) in voxelList: vxs = vx * self.spacing[0] vys = vy * self.spacing[1] vzs = vz * self.spacing[2] locatorInside = locator.InsideOrOutside((vxs,vys,vzs)) if locatorInside == -1: numIn += 1 percVoxelsInside = float(numIn) / len(voxelList) objid = pointLocator.FindClosestPoint((cxs, cys, czs)) x2,y2,z2 = self.polydata.GetPoint(objid) x2 /= self.spacing[0] y2 /= self.spacing[1] z2 /= self.spacing[2] distToSurf = self.distance((cx,cy,cz), (x2,y2,z2), self.voxelSize) distToCom = self.distance((cx,cy,cz), self.cellCOM, self.voxelSize) objPolyTP.append((objN, (cx,cy,cz), distToSurf, distToCom, inside, percVoxelsInside)) self.objPolydata[currentTimepoint] = objPolyTP n = len(self.imageCache.items()) if n > self.parameters["CacheAmount"]: items = self.imageCache.keys() items.sort() print "Removing ", items[0], "from cache" self.imageCache[items[0]].ReleaseData() del self.imageCache[items[0]] self.imageCache[currentTimepoint] = image self.progressObj.setProgress(currentTimepoint/self.parameters["Time"]) self.updateProgress(None, "ProgressEvent") return image