def takeScreenshot(self,name,description,type=-1): # show the message even if not taking a screen shot slicer.util.delayDisplay('Take screenshot: '+description+'.\nResult is available in the Annotations module.', 3000) lm = slicer.app.layoutManager() # switch on the type to get the requested window widget = 0 if type == slicer.qMRMLScreenShotDialog.FullLayout: # full layout widget = lm.viewport() elif type == slicer.qMRMLScreenShotDialog.ThreeD: # just the 3D window widget = lm.threeDWidget(0).threeDView() elif type == slicer.qMRMLScreenShotDialog.Red: # red slice window widget = lm.sliceWidget("Red") elif type == slicer.qMRMLScreenShotDialog.Yellow: # yellow slice window widget = lm.sliceWidget("Yellow") elif type == slicer.qMRMLScreenShotDialog.Green: # green slice window widget = lm.sliceWidget("Green") else: # default to using the full window widget = slicer.util.mainWindow() # reset the type so that the node is set correctly type = slicer.qMRMLScreenShotDialog.FullLayout # grab and convert to vtk image data qimage = ctk.ctkWidgetsUtils.grabWidget(widget) imageData = vtk.vtkImageData() slicer.qMRMLUtils().qImageToVtkImageData(qimage,imageData) annotationLogic = slicer.modules.annotations.logic() annotationLogic.CreateSnapShot(name, description, type, 1, imageData)
def takeScreenShot(name, description, widget=None, screenShotType=-1): lm = slicer.app.layoutManager() if not widget: if screenShotType == slicer.qMRMLScreenShotDialog.FullLayout: widget = lm.viewport() elif screenShotType == slicer.qMRMLScreenShotDialog.ThreeD: widget = lm.threeDWidget(0).threeDView() elif screenShotType == slicer.qMRMLScreenShotDialog.Red: widget = lm.sliceWidget("Red") elif screenShotType == slicer.qMRMLScreenShotDialog.Yellow: widget = lm.sliceWidget("Yellow") elif screenShotType == slicer.qMRMLScreenShotDialog.Green: widget = lm.sliceWidget("Green") else: widget = slicer.util.mainWindow() screenShotType = slicer.qMRMLScreenShotDialog.FullLayout qImage = ctk.ctkWidgetsUtils.grabWidget(widget) imageData = vtk.vtkImageData() slicer.qMRMLUtils().qImageToVtkImageData(qImage, imageData) annotationLogic = slicer.modules.annotations.logic() annotationLogic.CreateSnapShot(name, description, screenShotType, 1.0, imageData) return slicer.util.getNodesByClass('vtkMRMLAnnotationSnapshotNode')[-1]
def _createMagnifiedPixmap(self, xyz, inputImageDataConnection, outputSize, crosshairColor, imageZoom=10): # Use existing instance of objects to avoid instantiating one at each event. imageCrop = self.imageCrop painter = self.painter pen = self.pen def _roundInt(value): try: return int(round(value)) except ValueError: return 0 imageCrop.SetInputConnection(inputImageDataConnection) xyzInt = [0, 0, 0] xyzInt = [_roundInt(value) for value in xyz] producer = inputImageDataConnection.GetProducer() dims = producer.GetOutput().GetDimensions() minDim = min(dims[0], dims[1]) imageSize = _roundInt(minDim / imageZoom / 2.0) imin = max(0, xyzInt[0] - imageSize) imax = min(dims[0] - 1, xyzInt[0] + imageSize) jmin = max(0, xyzInt[1] - imageSize) jmax = min(dims[1] - 1, xyzInt[1] + imageSize) if (imin <= imax) and (jmin <= jmax): imageCrop.SetVOI(imin, imax, jmin, jmax, 0, 0) imageCrop.Update() vtkImage = imageCrop.GetOutput() if vtkImage: qImage = qt.QImage() slicer.qMRMLUtils().vtkImageDataToQImage(vtkImage, qImage) imagePixmap = qt.QPixmap.fromImage(qImage) imagePixmap = imagePixmap.scaled(outputSize, qt.Qt.KeepAspectRatio, qt.Qt.FastTransformation) # draw crosshair painter.begin(imagePixmap) pen = qt.QPen() pen.setColor(crosshairColor) painter.setPen(pen) painter.drawLine(0, int(imagePixmap.height() / 2), imagePixmap.width(), int(imagePixmap.height() / 2)) painter.drawLine(int(imagePixmap.width() / 2), 0, int(imagePixmap.width() / 2), imagePixmap.height()) painter.end() return imagePixmap return None
def takeScreenshot(self, name, description, type=-1): """ Take a screenshot of the selected viewport and store as and annotation snapshot node. Convenience method for automated testing. If self.enableScreenshots is False then only a message is displayed but screenshot is not stored. Screenshots are scaled by self.screenshotScaleFactor. :param name: snapshot node name :param description: description of the node :param type: which viewport to capture. If not specified then captures the entire window. Valid values: slicer.qMRMLScreenShotDialog.FullLayout, slicer.qMRMLScreenShotDialog.ThreeD, slicer.qMRMLScreenShotDialog.Red, slicer.qMRMLScreenShotDialog.Yellow, slicer.qMRMLScreenShotDialog.Green. """ # show the message even if not taking a screen shot slicer.util.delayDisplay(description) if not self.enableScreenshots: return lm = slicer.app.layoutManager() # switch on the type to get the requested window widget = 0 if type == slicer.qMRMLScreenShotDialog.FullLayout: # full layout widget = lm.viewport() elif type == slicer.qMRMLScreenShotDialog.ThreeD: # just the 3D window widget = lm.threeDWidget(0).threeDView() elif type == slicer.qMRMLScreenShotDialog.Red: # red slice window widget = lm.sliceWidget("Red") elif type == slicer.qMRMLScreenShotDialog.Yellow: # yellow slice window widget = lm.sliceWidget("Yellow") elif type == slicer.qMRMLScreenShotDialog.Green: # green slice window widget = lm.sliceWidget("Green") else: # default to using the full window widget = slicer.util.mainWindow() # reset the type so that the node is set correctly type = slicer.qMRMLScreenShotDialog.FullLayout # grab and convert to vtk image data qimage = ctk.ctkWidgetsUtils.grabWidget(widget) imageData = vtk.vtkImageData() slicer.qMRMLUtils().qImageToVtkImageData(qimage, imageData) annotationLogic = slicer.modules.annotations.logic() annotationLogic.CreateSnapShot(name, description, type, self.screenshotScaleFactor, imageData)
def takeScreenshot(self,name,description,type=-1): """ Take a screenshot of the selected viewport and store as and annotation snapshot node. Convenience method for automated testing. If self.enableScreenshots is False then only a message is displayed but screenshot is not stored. Screenshots are scaled by self.screenshotScaleFactor. :param name: snapshot node name :param description: description of the node :param type: which viewport to capture. If not specified then captures the entire window. Valid values: slicer.qMRMLScreenShotDialog.FullLayout, slicer.qMRMLScreenShotDialog.ThreeD, slicer.qMRMLScreenShotDialog.Red, slicer.qMRMLScreenShotDialog.Yellow, slicer.qMRMLScreenShotDialog.Green. """ # show the message even if not taking a screen shot slicer.util.delayDisplay(description) if not self.enableScreenshots: return lm = slicer.app.layoutManager() # switch on the type to get the requested window widget = 0 if type == slicer.qMRMLScreenShotDialog.FullLayout: # full layout widget = lm.viewport() elif type == slicer.qMRMLScreenShotDialog.ThreeD: # just the 3D window widget = lm.threeDWidget(0).threeDView() elif type == slicer.qMRMLScreenShotDialog.Red: # red slice window widget = lm.sliceWidget("Red") elif type == slicer.qMRMLScreenShotDialog.Yellow: # yellow slice window widget = lm.sliceWidget("Yellow") elif type == slicer.qMRMLScreenShotDialog.Green: # green slice window widget = lm.sliceWidget("Green") else: # default to using the full window widget = slicer.util.mainWindow() # reset the type so that the node is set correctly type = slicer.qMRMLScreenShotDialog.FullLayout # grab and convert to vtk image data qimage = ctk.ctkWidgetsUtils.grabWidget(widget) imageData = vtk.vtkImageData() slicer.qMRMLUtils().qImageToVtkImageData(qimage,imageData) annotationLogic = slicer.modules.annotations.logic() annotationLogic.CreateSnapShot(name, description, type, self.screenshotScaleFactor, imageData)
def timeimage(self, request=''): """ For timing and debugging - return an image with the current time rendered as text down to the hundredth of a second :param color: hex encoded RGB of dashed border (default 333 for dark gray) :return: png image """ # check arguments p = urllib.parse.urlparse(request.decode()) q = urllib.parse.parse_qs(p.query) try: color = "#" + q['color'][0].strip().lower() except KeyError: color = "#330" # # make a generally transparent image, # imageWidth = 128 imageHeight = 32 timeImage = qt.QImage(imageWidth, imageHeight, qt.QImage().Format_ARGB32) timeImage.fill(0) # a painter to use for various jobs painter = qt.QPainter() # draw a border around the pixmap painter.begin(timeImage) pen = qt.QPen() color = qt.QColor(color) color.setAlphaF(0.8) pen.setColor(color) pen.setWidth(5) pen.setStyle(3) # dotted line (Qt::DotLine) painter.setPen(pen) rect = qt.QRect(1, 1, imageWidth - 2, imageHeight - 2) painter.drawRect(rect) color = qt.QColor("#333") pen.setColor(color) painter.setPen(pen) position = qt.QPoint(10, 20) text = str(time.time()) # text to draw painter.drawText(position, text) painter.end() # convert the image to vtk, then to png from there vtkTimeImage = vtk.vtkImageData() slicer.qMRMLUtils().qImageToVtkImageData(timeImage, vtkTimeImage) pngData = self.vtkImageDataToPNG(vtkTimeImage) return pngData, b'image/png'
def _createMagnifiedPixmap(self, xyz, inputImageDataConnection, outputSize, crosshairColor, imageZoom=10): # Use existing instance of objects to avoid instantiating one at each event. imageCrop = self.imageCrop painter = self.painter pen = self.pen def _roundInt(value): try: return int(round(value)) except ValueError: return 0 imageCrop.SetInputConnection(inputImageDataConnection) xyzInt = [0, 0, 0] xyzInt = [_roundInt(value) for value in xyz] producer = inputImageDataConnection.GetProducer() dims = producer.GetOutput().GetDimensions() minDim = min(dims[0],dims[1]) imageSize = _roundInt(minDim/imageZoom/2.0) imin = max(0,xyzInt[0]-imageSize) imax = min(dims[0]-1, xyzInt[0]+imageSize) jmin = max(0,xyzInt[1]-imageSize) jmax = min(dims[1]-1, xyzInt[1]+imageSize) if (imin <= imax) and (jmin <= jmax): imageCrop.SetVOI(imin, imax, jmin, jmax, 0,0) imageCrop.Update() vtkImage = imageCrop.GetOutput() if vtkImage: qImage = qt.QImage() slicer.qMRMLUtils().vtkImageDataToQImage(vtkImage, qImage) imagePixmap = qt.QPixmap.fromImage(qImage) imagePixmap = imagePixmap.scaled(outputSize, qt.Qt.KeepAspectRatio, qt.Qt.FastTransformation) # draw crosshair painter.begin(imagePixmap) pen = qt.QPen() pen.setColor(crosshairColor) painter.setPen(pen) painter.drawLine(0, int(imagePixmap.height()/2), imagePixmap.width(), int(imagePixmap.height()/2)) painter.drawLine(int(imagePixmap.width()/2), 0, int(imagePixmap.width()/2), imagePixmap.height()) painter.end() return imagePixmap return None
def takeScreenshot(self, name, description, type=-1): # show the message even if not taking a screen shot slicer.util.delayDisplay(description) if self.enableScreenshots == 0: return lm = slicer.app.layoutManager() # switch on the type to get the requested window widget = 0 if type == slicer.qMRMLScreenShotDialog.FullLayout: # full layout widget = lm.viewport() elif type == slicer.qMRMLScreenShotDialog.ThreeD: # just the 3D window widget = lm.threeDWidget(0).threeDView() elif type == slicer.qMRMLScreenShotDialog.Red: # red slice window widget = lm.sliceWidget("Red") elif type == slicer.qMRMLScreenShotDialog.Yellow: # yellow slice window widget = lm.sliceWidget("Yellow") elif type == slicer.qMRMLScreenShotDialog.Green: # green slice window widget = lm.sliceWidget("Green") else: # default to using the full window widget = slicer.util.mainWindow() # reset the type so that the node is set correctly type = slicer.qMRMLScreenShotDialog.FullLayout # grab and convert to vtk image data qpixMap = qt.QPixmap().grabWidget(widget) qimage = qpixMap.toImage() imageData = vtk.vtkImageData() slicer.qMRMLUtils().qImageToVtkImageData(qimage, imageData) annotationLogic = slicer.modules.annotations.logic() annotationLogic.CreateSnapShot(name, description, type, self.screenshotScaleFactor, imageData)
def takeScreenshot(self,name,description,type=-1): # show the message even if not taking a screen shot self.delayDisplay(description) if self.enableScreenshots == 0: return lm = slicer.app.layoutManager() # switch on the type to get the requested window widget = 0 if type == slicer.qMRMLScreenShotDialog.FullLayout: # full layout widget = lm.viewport() elif type == slicer.qMRMLScreenShotDialog.ThreeD: # just the 3D window widget = lm.threeDWidget(0).threeDView() elif type == slicer.qMRMLScreenShotDialog.Red: # red slice window widget = lm.sliceWidget("Red") elif type == slicer.qMRMLScreenShotDialog.Yellow: # yellow slice window widget = lm.sliceWidget("Yellow") elif type == slicer.qMRMLScreenShotDialog.Green: # green slice window widget = lm.sliceWidget("Green") else: # default to using the full window widget = slicer.util.mainWindow() # reset the type so that the node is set correctly type = slicer.qMRMLScreenShotDialog.FullLayout # grab and convert to vtk image data qpixMap = qt.QPixmap().grabWidget(widget) qimage = qpixMap.toImage() imageData = vtk.vtkImageData() slicer.qMRMLUtils().qImageToVtkImageData(qimage,imageData) annotationLogic = slicer.modules.annotations.logic() annotationLogic.CreateSnapShot(name, description, type, self.screenshotScaleFactor, imageData)
def saveScreenshot(self, name, filePath, description): type = slicer.qMRMLScreenShotDialog.FullLayout lm = slicer.app.layoutManager() widget = lm.viewport() # grab and convert to vtk image data qpixMap = qt.QPixmap().grabWidget(widget) qimage = qpixMap.toImage() imageData = vtk.vtkImageData() slicer.qMRMLUtils().qImageToVtkImageData(qimage,imageData) annotationLogic = slicer.modules.annotations.logic() annotationLogic.CreateSnapShot(name, description, type, 1, imageData) snapshotNode = slicer.util.getNode(name) if not snapshotNode: print "Can't get snapshotNode" return if not slicer.util.saveNode(snapshotNode, filePath): print "Can't save " + filePath return
def __init__(self, parent=None, width=400, height=400, showWidget=False, scale=False): super(LayerReveal, self).__init__() self.width = width self.height = height self.showWidget = showWidget self.scale = scale self.renderer = None # utility Qt instances for use in methods self.gray = qt.QColor() self.gray.setRedF(0.5) self.gray.setGreenF(0.5) self.gray.setBlueF(0.5) # a painter to use for various jobs self.painter = qt.QPainter() # make a qwidget display if self.showWidget: self.frame = qt.QFrame(parent) mw = slicer.util.mainWindow() self.frame.setGeometry(mw.x, mw.y, self.width, self.height) self.frameLayout = qt.QVBoxLayout(self.frame) self.label = qt.QLabel() self.frameLayout.addWidget(self.label) self.frame.show() # make an image actor in the slice view self.vtkImage = vtk.vtkImageData() self.mrmlUtils = slicer.qMRMLUtils() self.imageMapper = vtk.vtkImageMapper() self.imageMapper.SetColorLevel(128) self.imageMapper.SetColorWindow(255) if vtk.VTK_MAJOR_VERSION <= 5: self.imageMapper.SetInput(self.vtkImage) else: self.imageMapper.SetInputData(self.vtkImage) self.actor2D = vtk.vtkActor2D() self.actor2D.SetMapper(self.imageMapper)
def __init__(self,parent=None,width=400,height=400,showWidget=False,scale=False): super(LayerReveal,self).__init__() self.width = width self.height = height self.showWidget = showWidget self.scale = scale self.renderer = None # utility Qt instances for use in methods self.gray = qt.QColor() self.gray.setRedF(0.5) self.gray.setGreenF(0.5) self.gray.setBlueF(0.5) # a painter to use for various jobs self.painter = qt.QPainter() # make a qwidget display if self.showWidget: self.frame = qt.QFrame(parent) mw = slicer.util.mainWindow() self.frame.setGeometry(mw.x, mw.y, self.width, self.height) self.frameLayout = qt.QVBoxLayout(self.frame) self.label = qt.QLabel() self.frameLayout.addWidget(self.label) self.frame.show() # make an image actor in the slice view self.vtkImage = vtk.vtkImageData() self.mrmlUtils = slicer.qMRMLUtils() self.imageMapper = vtk.vtkImageMapper() self.imageMapper.SetColorLevel(128) self.imageMapper.SetColorWindow(255) if vtk.VTK_MAJOR_VERSION <= 5: self.imageMapper.SetInput(self.vtkImage) else: self.imageMapper.SetInputData(self.vtkImage) self.actor2D = vtk.vtkActor2D() self.actor2D.SetMapper(self.imageMapper)
def generatePixmapForPrinting(self, filePath): if not self.getOutputFlattenedModelNode(): raise ValueError("Failed to access flattened baffle model") viewOwnerNode = self.getOutputFlattenedModelNode() if not self.printThreeDViewNode: self.printThreeDViewNode = slicer.mrmlScene.CreateNodeByClass( "vtkMRMLViewNode") self.printThreeDViewNode.UnRegister(None) self.printThreeDViewNode.SetSingletonTag("FlattenedBafflePrinter") self.printThreeDViewNode.SetName("FlattenedBafflePrinter") self.printThreeDViewNode.SetLayoutName("FlattenedBafflePrinter") self.printThreeDViewNode.SetLayoutLabel("FlattenedBafflePrinter") self.printThreeDViewNode.SetLayoutColor(1, 1, 0) self.printThreeDViewNode.SetAndObserveParentLayoutNodeID( viewOwnerNode.GetID()) self.printThreeDViewNode.SetRulerType( slicer.vtkMRMLAbstractViewNode.RulerTypeThin) self.printThreeDViewNode.SetRulerColor( slicer.vtkMRMLAbstractViewNode.RulerColorBlack) self.printThreeDViewNode = slicer.mrmlScene.AddNode( self.printThreeDViewNode) if not self.printThreeDWidget: self.printThreeDWidget = slicer.qMRMLThreeDWidget() self.printThreeDWidget.setObjectName( "self.printThreeDWidget" + self.printThreeDViewNode.GetLayoutLabel()) self.printThreeDWidget.viewLabel = self.printThreeDViewNode.GetLayoutLabel( ) self.printThreeDWidget.viewColor = qt.QColor.fromRgbF( *self.printThreeDViewNode.GetLayoutColor()) self.printThreeDWidget.setMRMLScene(slicer.mrmlScene) self.printThreeDWidget.setMRMLViewNode(self.printThreeDViewNode) self.printThreeDViewNode.SetAndObserveParentLayoutNodeID( viewOwnerNode.GetID()) self.printThreeDWidget.setMRMLViewNode(self.printThreeDViewNode) # Configure view and widget self.printThreeDViewNode.SetBoxVisible(0) self.printThreeDViewNode.SetAxisLabelsVisible(0) self.printThreeDViewNode.SetRenderMode( slicer.vtkMRMLViewNode.Orthographic) self.printThreeDViewNode.SetBackgroundColor((1, 1, 1)) self.printThreeDViewNode.SetBackgroundColor2((1, 1, 1)) # Set color and shading of flattened baffle and fixed points flattenedModelDisplayNode = self.getOutputFlattenedModelNode( ).GetDisplayNode() flattenedModelOriginalColor = flattenedModelDisplayNode.GetColor() flattenedModelDisplayNode.SetColor(0.0, 0.0, 0.0) flattenedFixedPointsNode = self.getOutputFlattenedFixedPointsNode() flattenedFixedPointsDisplayNode = flattenedFixedPointsNode.GetDisplayNode( ) flattenedFixedPointsOriginalColor = flattenedFixedPointsDisplayNode.GetColor( ) flattenedFixedPointsDisplayNode.SetColor(1.0, 0.5, 0.5) # Make sure nothing is visible in the print view other than the baffle and the fixed landmarks hiddenDisplayNodes = [] layoutThreeDViewNode = slicer.app.layoutManager().threeDWidget( 0).viewLogic().GetViewNode() allDisplayNodes = slicer.util.getNodesByClass('vtkMRMLDisplayNode') for displayNode in allDisplayNodes: if displayNode is not flattenedModelDisplayNode and displayNode is not flattenedFixedPointsDisplayNode: displayNode.AddViewNodeID(layoutThreeDViewNode.GetID()) hiddenDisplayNodes.append(displayNode) # Show 3D view self.printThreeDWidget.resize(self.printViewWidth, self.printViewHeight) self.printThreeDWidget.show() # Determine ROI for flattened baffle flattenedBaffePolyData = self.getOutputFlattenedModelNode( ).GetPolyData() bounds = [0] * 6 flattenedBaffePolyData.GetBounds(bounds) center = [(bounds[0] + bounds[1]) / 2.0, (bounds[2] + bounds[3]) / 2.0, (bounds[4] + bounds[5]) / 2.0] # Setup camera cameraPositionOffset = 100.0 cameraNode = slicer.modules.cameras.logic().GetViewActiveCameraNode( self.printThreeDViewNode) cameraNode.SetFocalPoint(center) cameraNode.SetPosition( center[0], center[1], center[2] + bounds[5] - bounds[4] + cameraPositionOffset) cameraNode.SetViewUp(0, 1, 0) cameraNode.GetCamera().SetClippingRange(cameraPositionOffset / 2.0, (bounds[5] - bounds[4]) * 2 + cameraPositionOffset * 2.0) windowSizeInPixels = self.printThreeDWidget.threeDView().renderWindow( ).GetSize() pixelSizeInMm = 25.4 / self.printYResolutionDpi printViewHeightOfViewportInMm = windowSizeInPixels[ 1] * pixelSizeInMm / self.printScale cameraNode.SetParallelScale(printViewHeightOfViewportInMm) threeDView = self.printThreeDWidget.threeDView() renderWindow = threeDView.renderWindow() renderer = renderWindow.GetRenderers().GetFirstRenderer() originalCameraUserTransform = cameraNode.GetCamera().GetUserTransform() originalPixelAspect = renderer.GetPixelAspect() cameraUserTransform = vtk.vtkTransform() cameraUserTransform.Scale( self.printXResolutionDpi / self.printYResolutionDpi, 1.0, 1.0) cameraNode.GetCamera().SetUserTransform(cameraUserTransform) if self.printTransparentBackground: originalAlphaBitPlanes = renderWindow.GetAlphaBitPlanes() renderWindow.SetAlphaBitPlanes(1) originalGradientBackground = renderer.GetGradientBackground() renderer.SetGradientBackground(False) # Render threeDView.forceRender() windowToImage = vtk.vtkWindowToImageFilter() if self.printTransparentBackground: windowToImage.SetInputBufferTypeToRGBA() renderWindow.Render() windowToImage.SetInput(renderWindow) # Write to file with custom DPI # (use Qt file writer to allow saving DPI values) windowToImage.Update() vtkImage = windowToImage.GetOutput() qImage = qt.QImage() slicer.qMRMLUtils().vtkImageDataToQImage(vtkImage, qImage) inchesPerMeter = 1000 / 25.4 qImage.setDotsPerMeterX(self.printXResolutionDpi * inchesPerMeter) qImage.setDotsPerMeterY(self.printYResolutionDpi * inchesPerMeter) imagePixmap = qt.QPixmap.fromImage(qImage) imagePixmap.save(filePath) self.printThreeDWidget.hide() # Restore settings cameraNode.GetCamera().SetUserTransform(originalCameraUserTransform) flattenedModelDisplayNode.SetColor(flattenedModelOriginalColor) flattenedFixedPointsDisplayNode.SetColor( flattenedFixedPointsOriginalColor) for displayNode in hiddenDisplayNodes: displayNode.RemoveAllViewNodeIDs() if self.printTransparentBackground: renderWindow.SetAlphaBitPlanes(originalAlphaBitPlanes) renderer.SetGradientBackground(originalGradientBackground)
def revealPixmap(self, xy): """fill a pixmap with an image that has a reveal pattern at xy with the fg drawn over the bg""" # Get QImages for the two layers bgVTKImage = self.layerLogics['B'].GetImageData() fgVTKImage = self.layerLogics['F'].GetImageData() bgQImage = qt.QImage() fgQImage = qt.QImage() slicer.qMRMLUtils().vtkImageDataToQImage(bgVTKImage, bgQImage) slicer.qMRMLUtils().vtkImageDataToQImage(fgVTKImage, fgQImage) # get the geometry of the focal point (xy) and images # noting that vtk has the origin at the bottom left and qt has # it at the top left. yy is the flipped version of y imageWidth = bgQImage.width() imageHeight = bgQImage.height() x,y=xy yy = imageHeight-y # # make a generally transparent image, # then fill quadrants with the fg image # overlayImage = qt.QImage(imageWidth, imageHeight, qt.QImage().Format_ARGB32) overlayImage.fill(0) halfWidth = imageWidth//2 halfHeight = imageHeight//2 topLeft = qt.QRect(0,0, x, yy) bottomRight = qt.QRect(x, yy, imageWidth-x-1, imageHeight-yy-1) self.painter.begin(overlayImage) self.painter.drawImage(topLeft, fgQImage, topLeft) self.painter.drawImage(bottomRight, fgQImage, bottomRight) self.painter.end() # draw the bg and fg on top of gray background compositePixmap = qt.QPixmap(self.width,self.height) compositePixmap.fill(self.gray) self.painter.begin(compositePixmap) self.painter.drawImage( -1 * (x -self.width//2), -1 * (yy -self.height//2), bgQImage) self.painter.drawImage( -1 * (x -self.width//2), -1 * (yy -self.height//2), overlayImage) self.painter.end() if self.scale: compositePixmap = self.scalePixmap(compositePixmap) # draw a border around the pixmap self.painter.begin(compositePixmap) self.pen = qt.QPen() self.color = qt.QColor("#FF0") self.color.setAlphaF(0.3) self.pen.setColor(self.color) self.pen.setWidth(5) self.pen.setStyle(3) # dotted line (Qt::DotLine) self.painter.setPen(self.pen) rect = qt.QRect(1, 1, self.width-2, self.height-2) self.painter.drawRect(rect) self.painter.end() return compositePixmap
def revealPixmap(self, xy): """fill a pixmap with an image that has a reveal pattern at xy with the fg drawn over the bg""" # Get QImages for the two layers bgVTKImage = self.layerLogics['B'].GetImageData() fgVTKImage = self.layerLogics['F'].GetImageData() bgQImage = qt.QImage() fgQImage = qt.QImage() slicer.qMRMLUtils().vtkImageDataToQImage(bgVTKImage, bgQImage) slicer.qMRMLUtils().vtkImageDataToQImage(fgVTKImage, fgQImage) # get the geometry of the focal point (xy) and images # noting that vtk has the origin at the bottom left and qt has # it at the top left. yy is the flipped version of y imageWidth = bgQImage.width() imageHeight = bgQImage.height() x, y = xy yy = imageHeight - y # # make a generally transparent image, # then fill quadrants with the fg image # overlayImage = qt.QImage(imageWidth, imageHeight, qt.QImage().Format_ARGB32) overlayImage.fill(0) halfWidth = imageWidth // 2 halfHeight = imageHeight // 2 topLeft = qt.QRect(0, 0, x, yy) bottomRight = qt.QRect(x, yy, imageWidth - x - 1, imageHeight - yy - 1) self.painter.begin(overlayImage) self.painter.drawImage(topLeft, fgQImage, topLeft) self.painter.drawImage(bottomRight, fgQImage, bottomRight) self.painter.end() # draw the bg and fg on top of gray background compositePixmap = qt.QPixmap(self.width, self.height) compositePixmap.fill(self.gray) self.painter.begin(compositePixmap) self.painter.drawImage(-1 * (x - self.width // 2), -1 * (yy - self.height // 2), bgQImage) self.painter.drawImage(-1 * (x - self.width // 2), -1 * (yy - self.height // 2), overlayImage) self.painter.end() if self.scale: compositePixmap = self.scalePixmap(compositePixmap) # draw a border around the pixmap self.painter.begin(compositePixmap) self.pen = qt.QPen() self.color = qt.QColor("#FF0") self.color.setAlphaF(0.3) self.pen.setColor(self.color) self.pen.setWidth(5) self.pen.setStyle(3) # dotted line (Qt::DotLine) self.painter.setPen(self.pen) rect = qt.QRect(1, 1, self.width - 2, self.height - 2) self.painter.drawRect(rect) self.painter.end() return compositePixmap
def _createMagnifiedPixmap(self, xyz, inputImageDataConnection, outputSize, crosshairColor, imageZoom=10): # Use existing instance of objects to avoid instantiating one at each event. imageCrop = self.imageCrop painter = self.painter pen = self.pen def _roundInt(value): try: return int(round(value)) except ValueError: return 0 imageCrop.SetInputConnection(inputImageDataConnection) xyzInt = [0, 0, 0] xyzInt = [_roundInt(value) for value in xyz] producer = inputImageDataConnection.GetProducer() dims = producer.GetOutput().GetDimensions() minDim = min(dims[0], dims[1]) imageSize = _roundInt(minDim / imageZoom / 2.0) imin = xyzInt[0] - imageSize imax = xyzInt[0] + imageSize jmin = xyzInt[1] - imageSize jmax = xyzInt[1] + imageSize imin_trunc = max(0, imin) imax_trunc = min(dims[0] - 1, imax) jmin_trunc = max(0, jmin) jmax_trunc = min(dims[1] - 1, jmax) # The extra complexity of the canvas is used here to maintain a fixed size # output due to the imageCrop returning a smaller image if the limits are # outside the input image bounds. Specially useful when zooming at the borders. canvas = self.canvas canvas.SetScalarType(producer.GetOutput().GetScalarType()) canvas.SetNumberOfScalarComponents( producer.GetOutput().GetNumberOfScalarComponents()) canvas.SetExtent(imin, imax, jmin, jmax, 0, 0) canvas.FillBox(imin, imax, jmin, jmax) canvas.Update() if (imin_trunc <= imax_trunc) and (jmin_trunc <= jmax_trunc): imageCrop.SetVOI(imin_trunc, imax_trunc, jmin_trunc, jmax_trunc, 0, 0) imageCrop.Update() vtkImageCropped = imageCrop.GetOutput() xyzBounds = [0] * 6 vtkImageCropped.GetBounds(xyzBounds) xyzBounds = [_roundInt(value) for value in xyzBounds] canvas.DrawImage(xyzBounds[0], xyzBounds[2], vtkImageCropped) canvas.Update() vtkImageFromCanvas = canvas.GetOutput() if vtkImageFromCanvas: qImage = qt.QImage() slicer.qMRMLUtils().vtkImageDataToQImage( vtkImageFromCanvas, qImage) imagePixmap = qt.QPixmap.fromImage(qImage) imagePixmap = imagePixmap.scaled(outputSize, qt.Qt.KeepAspectRatio, qt.Qt.FastTransformation) # draw crosshair painter.begin(imagePixmap) pen = qt.QPen() pen.setColor(crosshairColor) painter.setPen(pen) painter.drawLine(0, int(imagePixmap.height() / 2), imagePixmap.width(), int(imagePixmap.height() / 2)) painter.drawLine(int(imagePixmap.width() / 2), 0, int(imagePixmap.width() / 2), imagePixmap.height()) painter.end() return imagePixmap return None
def __init__(self, parent=None, width=400, height=400, showWidget=False, scale=False): super(ROIManager, self).__init__() self.width = width self.height = height self.showWidget = showWidget self.scale = scale self.renderer = None self.ROIRadius = 20.0 self.minValueBG = 0 self.maxValueBG = 0 self.minValueFG = 0 self.maxValueFG = 0 self.probeWidget = None self.drawOverlay = 0 # utility Qt instances for use in methods self.gray = qt.QColor() self.gray.setRedF(0.5) self.gray.setGreenF(0.5) self.gray.setBlueF(0.5) # a painter to use for various jobs self.painter = qt.QPainter() # make a qwidget display if self.showWidget: self.frame = qt.QFrame(parent) mw = slicer.util.mainWindow() self.frame.setGeometry(mw.x, mw.y, self.width, self.height) self.frameLayout = qt.QVBoxLayout(self.frame) self.label = qt.QLabel() self.frameLayout.addWidget(self.label) self.frame.show() # make an image actor in the slice view self.vtkImage = vtk.vtkImageData() self.mrmlUtils = slicer.qMRMLUtils() self.imageMapper = vtk.vtkImageMapper() self.imageMapper.SetColorLevel(128) self.imageMapper.SetColorWindow(255) if vtk.VTK_MAJOR_VERSION <= 5: self.imageMapper.SetInput(self.vtkImage) else: self.imageMapper.SetInputData(self.vtkImage) self.actor2D = vtk.vtkActor2D() self.actor2D.SetMapper(self.imageMapper) # make a circle actor self.circle = vtk.vtkRegularPolygonSource() self.circle.SetNumberOfSides(50) self.circle.SetRadius(5) self.circle.SetCenter(0, 0, 0) self.circle.GeneratePolylineOn() self.circle.GeneratePolygonOff() self.circle.Update() self.mapper = vtk.vtkPolyDataMapper2D() self.actor = vtk.vtkActor2D() if vtk.VTK_MAJOR_VERSION <= 5: self.mapper.SetInput(self.circle.GetOutput()) else: self.mapper.SetInputConnection(self.circle.GetOutputPort()) self.actor.SetMapper(self.mapper) property_ = self.actor.GetProperty() property_.SetColor(1, 1, 0) property_.SetLineWidth(1)