def takeScreenshot(self,name,description,type=-1,screenshotScaleFactor=1):
    lm = slicer.app.layoutManager()
    # switch on the type to get the requested window
    widget = 0
    if type == -1:
      # full window
      widget = slicer.util.mainWindow()
    elif 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")

    # 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, screenshotScaleFactor, imageData)
Example #2
0
  def run(self,node,enableScreenshots=0,screenshotScaleFactor=1):
    """
    Run the actual algorithm
    """
    self.enableScreenshots = enableScreenshots
    self.screenshotScaleFactor = screenshotScaleFactor
    name = 'Lung001'
    #self.takeScreenshot('Lung001_PT',slicer.qMRMLScreenShotDialog().ThreeD)
    self.takeScreenshot(name,slicer.qMRMLScreenShotDialog().ThreeD)

    print "Finished!"
    return True
Example #3
0
    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 == -1:
            # full window
            widget = slicer.util.mainWindow()
        elif 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")

        # 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 onSave(self):
    """save the label statistics
    """
    self.onApplyButton() #selects Apply in case it is accidentally not pressed
    self.logic.takeScreenshot('Slice-label','slice',slicer.qMRMLScreenShotDialog().Red)
    baseDir = os.path.dirname(self.resultsFilePath)
    folderName = self.helper.master.GetName()
    dirName = os.path.join(baseDir, folderName)
    if not os.path.exists(dirName):
      os.mkdir(dirName)
    l = slicer.app.applicationLogic()
    l.SaveSceneToSlicerDataBundleDirectory(dirName, None)

    # saves the csv files to selected folder
    csvFileName = os.path.join(dirName, "{0}_waist_circumference.csv".format(folderName))
    self.logic.saveStats(csvFileName)
    self.logic.appendStats(self.resultsFilePath)
    self.resetTableModel()
    self.logic.startNextImage()
    def run(self, enableScreenshots=0, screenshotScaleFactor=1):
        """
    Run the actual algorithm
    """

        self.delayDisplay('Running the aglorithm')

        self.enableScreenshots = enableScreenshots
        self.screenshotScaleFactor = screenshotScaleFactor

        # Start in conventional layout
        lm = slicer.app.layoutManager()
        lm.setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutConventionalView)
        # without this delayed display, when running from the cmd line Slicer starts
        # up in a different layout and the seed won't get rendered in the right spot
        self.delayDisplay("Conventional view")

        # Download MRHead from sample data
        import SampleData
        sampleDataLogic = SampleData.SampleDataLogic()
        print("Getting MR Head Volume")
        mrHeadVolume = sampleDataLogic.downloadMRHead()

        # Place a fiducial on the red slice
        markupsLogic = slicer.modules.markups.logic()
        eye = [33.4975, 79.4042, -10.2143]
        fidIndex = markupsLogic.AddFiducial(eye[0], eye[1], eye[2])
        fidID = markupsLogic.GetActiveListID()
        fidNode = slicer.mrmlScene.GetNodeByID(fidID)
        self.delayDisplay("Placed a fiducial")

        # Pan and zoom
        sliceWidget = slicer.app.layoutManager().sliceWidget('Red')
        sliceLogic = sliceWidget.sliceLogic()
        compositeNode = sliceLogic.GetSliceCompositeNode()
        sliceNode = sliceLogic.GetSliceNode()
        sliceNode.SetXYZOrigin(-71.7, 129.7, 0.0)
        sliceNode.SetFieldOfView(98.3, 130.5, 1.0)
        self.delayDisplay("Panned and zoomed")

        # Get the seed widget seed location
        startingSeedDisplayCoords = [0.0, 0.0, 0.0]
        helper = self.getFiducialSliceDisplayableManagerHelper('Red')
        if helper != None:
            seedWidget = helper.GetWidget(fidNode)
            seedRepresentation = seedWidget.GetSeedRepresentation()
            handleRep = seedRepresentation.GetHandleRepresentation(fidIndex)
            startingSeedDisplayCoords = handleRep.GetDisplayPosition()
            print('Starting seed display coords = %d, %d, %d' %
                  (startingSeedDisplayCoords[0], startingSeedDisplayCoords[1],
                   startingSeedDisplayCoords[2]))
        self.takeScreenshot('FiducialLayoutSwitchBug1914-StartingPosition',
                            'Fiducial starting position',
                            slicer.qMRMLScreenShotDialog().Red)

        # Switch to red slice only
        lm.setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutOneUpRedSliceView)
        self.delayDisplay("Red Slice only")

        # Switch to conventional layout
        lm.setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutConventionalView)
        self.delayDisplay("Conventional layout")

        # Get the current seed widget seed location
        endingSeedDisplayCoords = [0.0, 0.0, 0.0]
        helper = self.getFiducialSliceDisplayableManagerHelper('Red')
        if helper != None:
            seedWidget = helper.GetWidget(fidNode)
            seedRepresentation = seedWidget.GetSeedRepresentation()
            handleRep = seedRepresentation.GetHandleRepresentation(fidIndex)
            endingSeedDisplayCoords = handleRep.GetDisplayPosition()
            print('Ending seed display coords = %d, %d, %d' %
                  (endingSeedDisplayCoords[0], endingSeedDisplayCoords[1],
                   endingSeedDisplayCoords[2]))
        self.takeScreenshot('FiducialLayoutSwitchBug1914-EndingPosition',
                            'Fiducial ending position',
                            slicer.qMRMLScreenShotDialog().Red)

        # Compare to original seed widget location
        diff = math.pow(
            (startingSeedDisplayCoords[0] - endingSeedDisplayCoords[0]),
            2) + math.pow(
                (startingSeedDisplayCoords[1] - endingSeedDisplayCoords[1]),
                2) + math.pow((startingSeedDisplayCoords[2] -
                               endingSeedDisplayCoords[2]), 2)
        if diff != 0.0:
            diff = math.sqrt(diff)
        self.delayDisplay(
            "Difference between starting and ending seed display coordinates = %g"
            % diff)

        if diff > 1.0:
            # raise Exception("Display coordinate difference is too large!\nExpected < 1.0 but got %g" % (diff))
            print(
                "Display coordinate difference is too large!\nExpected < 1.0 but got %g"
                % (diff))
            return False

        if enableScreenshots == 1:
            # compare the screen snapshots
            startView = slicer.mrmlScene.GetFirstNodeByName(
                'FiducialLayoutSwitchBug1914-StartingPosition')
            startShot = startView.GetScreenShot()
            endView = slicer.mrmlScene.GetFirstNodeByName(
                'FiducialLayoutSwitchBug1914-EndingPosition')
            endShot = endView.GetScreenShot()
            imageMath = vtk.vtkImageMathematics()
            imageMath.SetOperationToSubtract()
            imageMath.SetInput1(startShot)
            imageMath.SetInput2(endShot)
            imageMath.Update()
            shotDiff = imageMath.GetOutput()
            # save it as a scene view
            annotationLogic = slicer.modules.annotations.logic()
            annotationLogic.CreateSnapShot(
                "FiducialLayoutSwitchBug1914-Diff",
                "Difference between starting and ending fiducial seed positions",
                slicer.qMRMLScreenShotDialog().Red, screenshotScaleFactor,
                shotDiff)
            # calculate the image difference
            imageStats = vtk.vtkImageHistogramStatistics()
            imageStats.SetInput(shotDiff)
            imageStats.GenerateHistogramImageOff()
            imageStats.Update()
            meanVal = imageStats.GetMean()
            self.delayDisplay("Mean of image difference = %g" % meanVal)
            if meanVal > 5.0:
                # raise Exception("Image difference is too great!\nExpected <= 5.0, but got %g" % (meanVal))
                print(
                    "Image difference is too great!\nExpected <= 5.0, but got %g"
                    % (meanVal))
                return False

        self.delayDisplay('Test passed!')
        return True
Example #6
0
  def takeScreenshot(self,name,type=-1):
    # show the message even if not taking a screen shot
    
    filePath = '/u/kanderle/Transfer/Movie/'+name+'.png'

    if self.enableScreenshots == 0:
      return

    lm = slicer.app.layoutManager()
    description = '4D'
    # switch on the type to get the requested window
    widget = 0
    if type == -1:
      # full window
      widget = slicer.util.mainWindow()
    elif 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")

    # grab and convert to vtk image data
    
    
    
    if type == slicer.qMRMLScreenShotDialog().ThreeD:
      degrees = 71
      n = 0 #
      heartBeat = 0
      heartDisplayNode = None
      color = [0.56,0,0]
      widget = lm.threeDWidget(0).threeDView()
      for i in range(0,degrees):
	if i < 10:
	  number = '0' + str(i)
	else:
	  number = str(i)
	filePath = '/u/kanderle/Transfer/Movie/'+name+'_'+number+'.png'
	#if i%heartBeat == 0:
	  #if heartDisplayNode:
	    #heartDisplayNode.SetVisibility(0)
	  #n += 1
	  #if n>10:
	    #n = 1
          #print "Changing state to: " + str(n)
	#heartNode = slicer.util.getNode('Heart_' + str(n))
	#if not heartNode:
	  #print "No Heart Node"
	  #n = n+1
	  #continue
	#heartDisplayNode = heartNode.GetDisplayNode()
	#heartDisplayNode.SetColor(color)
	#heartDisplayNode.SetVisibility(1)
	#heartDisplayNode.SetOpacity(0.8)
	  
	rw = widget.renderWindow()
	wti = vtk.vtkWindowToImageFilter()
	wti.SetInput(rw)
	wti.Update()
	writer = vtk.vtkPNGWriter()
	writer.SetFileName(filePath)
	writer.SetInputConnection(wti.GetOutputPort())
	writer.Write()
	widget.yaw()
	widget.forceRender()
	
    else:
      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 run(self,enableScreenshots=0,screenshotScaleFactor=1):
    """
    Run the actual algorithm
    """

    self.delayDisplay('Running the aglorithm')

    self.enableScreenshots = enableScreenshots
    self.screenshotScaleFactor = screenshotScaleFactor

    # Start in conventional layout
    lm = slicer.app.layoutManager()
    lm.setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutConventionalView)
    # without this delayed display, when running from the cmd line Slicer starts
    # up in a different layout and the seed won't get rendered in the right spot
    self.delayDisplay("Conventional view")

    # Download MRHead from sample data
    import SampleData
    sampleDataLogic = SampleData.SampleDataLogic()
    print("Getting MR Head Volume")
    mrHeadVolume = sampleDataLogic.downloadMRHead()

    # Place a fiducial on the red slice
    markupsLogic = slicer.modules.markups.logic()
    eye = [33.4975, 79.4042, -10.2143]
    fidIndex = markupsLogic.AddFiducial(eye[0], eye[1], eye[2])
    fidID = markupsLogic.GetActiveListID()
    fidNode = slicer.mrmlScene.GetNodeByID(fidID)
    self.delayDisplay("Placed a fiducial")

    # Pan and zoom
    sliceWidget = slicer.app.layoutManager().sliceWidget('Red')
    sliceLogic = sliceWidget.sliceLogic()
    compositeNode = sliceLogic.GetSliceCompositeNode()
    sliceNode = sliceLogic.GetSliceNode()
    sliceNode.SetXYZOrigin(-71.7, 129.7, 0.0)
    sliceNode.SetFieldOfView(98.3, 130.5, 1.0)
    self.delayDisplay("Panned and zoomed")

    # Get the seed widget seed location
    startingSeedDisplayCoords = [0.0, 0.0, 0.0]
    helper = self.getFiducialSliceDisplayableManagerHelper('Red')
    if helper != None:
     seedWidget = helper.GetWidget(fidNode)
     seedRepresentation = seedWidget.GetSeedRepresentation()
     handleRep = seedRepresentation.GetHandleRepresentation(fidIndex)
     startingSeedDisplayCoords = handleRep.GetDisplayPosition()
     print('Starting seed display coords = %d, %d, %d' % (startingSeedDisplayCoords[0], startingSeedDisplayCoords[1], startingSeedDisplayCoords[2]))
    self.takeScreenshot('FiducialLayoutSwitchBug1914-StartingPosition','Fiducial starting position',slicer.qMRMLScreenShotDialog().Red)

    # Switch to red slice only
    lm.setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutOneUpRedSliceView)
    self.delayDisplay("Red Slice only")

    # Switch to conventional layout
    lm.setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutConventionalView)
    self.delayDisplay("Conventional layout")

    # Get the current seed widget seed location
    endingSeedDisplayCoords = [0.0, 0.0, 0.0]
    helper = self.getFiducialSliceDisplayableManagerHelper('Red')
    if helper != None:
     seedWidget = helper.GetWidget(fidNode)
     seedRepresentation = seedWidget.GetSeedRepresentation()
     handleRep = seedRepresentation.GetHandleRepresentation(fidIndex)
     endingSeedDisplayCoords = handleRep.GetDisplayPosition()
     print('Ending seed display coords = %d, %d, %d' % (endingSeedDisplayCoords[0], endingSeedDisplayCoords[1], endingSeedDisplayCoords[2]))
    self.takeScreenshot('FiducialLayoutSwitchBug1914-EndingPosition','Fiducial ending position',slicer.qMRMLScreenShotDialog().Red)

    # Compare to original seed widget location
    diff = math.pow((startingSeedDisplayCoords[0] - endingSeedDisplayCoords[0]),2) + math.pow((startingSeedDisplayCoords[1] - endingSeedDisplayCoords[1]),2) + math.pow((startingSeedDisplayCoords[2] - endingSeedDisplayCoords[2]),2)
    if diff != 0.0:
      diff = math.sqrt(diff)
    self.delayDisplay("Difference between starting and ending seed display coordinates = %g" % diff)

    if diff > 1.0:
      # raise Exception("Display coordinate difference is too large!\nExpected < 1.0 but got %g" % (diff))
      print("Display coordinate difference is too large!\nExpected < 1.0 but got %g" % (diff))
      return False

    if enableScreenshots == 1:
      # compare the screen snapshots
      startView = slicer.mrmlScene.GetFirstNodeByName('FiducialLayoutSwitchBug1914-StartingPosition')
      startShot = startView.GetScreenShot()
      endView = slicer.mrmlScene.GetFirstNodeByName('FiducialLayoutSwitchBug1914-EndingPosition')
      endShot = endView.GetScreenShot()
      imageMath = vtk.vtkImageMathematics()
      imageMath.SetOperationToSubtract()
      imageMath.SetInput1(startShot)
      imageMath.SetInput2(endShot)
      imageMath.Update()
      shotDiff = imageMath.GetOutput()
      # save it as a scene view
      annotationLogic = slicer.modules.annotations.logic()
      annotationLogic.CreateSnapShot("FiducialLayoutSwitchBug1914-Diff", "Difference between starting and ending fiducial seed positions",slicer.qMRMLScreenShotDialog().Red, screenshotScaleFactor, shotDiff)
      # calculate the image difference
      imageStats = vtk.vtkImageHistogramStatistics()
      imageStats.SetInput(shotDiff)
      imageStats.GenerateHistogramImageOff()
      imageStats.Update()
      meanVal = imageStats.GetMean()
      self.delayDisplay("Mean of image difference = %g" % meanVal)
      if meanVal > 5.0:
        # raise Exception("Image difference is too great!\nExpected <= 5.0, but got %g" % (meanVal))
        print("Image difference is too great!\nExpected <= 5.0, but got %g" % (meanVal))
        return False

    self.delayDisplay('Test passed!')
    return True