|
Slicer 4.2
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
|
00001 from __main__ import vtk, qt, ctk, slicer 00002 00003 # 00004 # VectorToScalarVolume 00005 # 00006 00007 class VectorToScalarVolume: 00008 def __init__(self, parent): 00009 parent.title = "Vector to Scalar Volume" 00010 parent.categories = ["Converters"] 00011 parent.dependencies = [] 00012 parent.contributors = ["Steve Pieper (Isomics)",] 00013 parent.helpText = """ 00014 Make a scalar (1 component) volume from a vector volume 00015 """ 00016 parent.acknowledgementText = """ 00017 Developed by Steve Pieper, Isomics, Inc., 00018 partially funded by NIH grant 3P41RR013218-12S1 (NAC) and is part of the National Alliance 00019 for Medical Image Computing (NA-MIC), funded by the National Institutes of Health through the 00020 NIH Roadmap for Medical Research, Grant U54 EB005149.""" 00021 self.parent = parent 00022 00023 # 00024 # VectorToScalarVolumeWidget 00025 # 00026 00027 class VectorToScalarVolumeWidget: 00028 def __init__(self, parent = None): 00029 if not parent: 00030 self.parent = slicer.qMRMLWidget() 00031 self.parent.setLayout(qt.QVBoxLayout()) 00032 self.parent.setMRMLScene(slicer.mrmlScene) 00033 else: 00034 self.parent = parent 00035 self.layout = self.parent.layout() 00036 if not parent: 00037 self.setup() 00038 self.parent.show() 00039 00040 def setup(self): 00041 # Collapsible button 00042 self.selectionCollapsibleButton = ctk.ctkCollapsibleButton() 00043 self.selectionCollapsibleButton.text = "Selection" 00044 self.layout.addWidget(self.selectionCollapsibleButton) 00045 00046 # Layout within the collapsible button 00047 self.formLayout = qt.QFormLayout(self.selectionCollapsibleButton) 00048 00049 # 00050 # the volume selectors 00051 # 00052 self.inputFrame = qt.QFrame(self.selectionCollapsibleButton) 00053 self.inputFrame.setLayout(qt.QHBoxLayout()) 00054 self.formLayout.addWidget(self.inputFrame) 00055 self.inputSelector = qt.QLabel("Input Vector Volume: ", self.inputFrame) 00056 self.inputFrame.layout().addWidget(self.inputSelector) 00057 self.inputSelector = slicer.qMRMLNodeComboBox(self.inputFrame) 00058 self.inputSelector.nodeTypes = ( ("vtkMRMLVectorVolumeNode"), "" ) 00059 self.inputSelector.addEnabled = False 00060 self.inputSelector.removeEnabled = False 00061 self.inputSelector.setMRMLScene( slicer.mrmlScene ) 00062 self.inputFrame.layout().addWidget(self.inputSelector) 00063 00064 self.outputFrame = qt.QFrame(self.selectionCollapsibleButton) 00065 self.outputFrame.setLayout(qt.QHBoxLayout()) 00066 self.formLayout.addWidget(self.outputFrame) 00067 self.outputSelector = qt.QLabel("Output Scalar Volume: ", self.outputFrame) 00068 self.outputFrame.layout().addWidget(self.outputSelector) 00069 self.outputSelector = slicer.qMRMLNodeComboBox(self.outputFrame) 00070 self.outputSelector.nodeTypes = ( ("vtkMRMLScalarVolumeNode"), "" ) 00071 self.outputSelector.setMRMLScene( slicer.mrmlScene ) 00072 self.outputSelector.addEnabled = True 00073 self.outputSelector.renameEnabled = True 00074 self.outputSelector.baseName = "Scalar Volume" 00075 self.outputFrame.layout().addWidget(self.outputSelector) 00076 00077 # Apply button 00078 self.applyButton = qt.QPushButton("Apply") 00079 self.applyButton.toolTip = "Run Convert the vector to scalar." 00080 self.formLayout.addWidget(self.applyButton) 00081 self.applyButton.connect('clicked(bool)', self.onApply) 00082 00083 # Add vertical spacer 00084 self.layout.addStretch(1) 00085 00086 def onApply(self): 00087 inputVolume = self.inputSelector.currentNode() 00088 outputVolume = self.outputSelector.currentNode() 00089 # check for input data 00090 if not (inputVolume and outputVolume): 00091 qt.QMessageBox.critical( 00092 slicer.util.mainWindow(), 00093 'Luminance', 'Input and output volumes are required for conversion') 00094 return 00095 # check that data has enough components 00096 inputImage = inputVolume.GetImageData() 00097 if not inputImage or inputImage.GetNumberOfScalarComponents() < 3: 00098 qt.QMessageBox.critical( 00099 slicer.util.mainWindow(), 00100 'Vector to Scalar Volume', 'Input does not have enough components for conversion') 00101 return 00102 # run the filter 00103 # - extract the RGB portions 00104 extract = vtk.vtkImageExtractComponents() 00105 extract.SetComponents(0,1,2) 00106 extract.SetInput(inputVolume.GetImageData()) 00107 luminance = vtk.vtkImageLuminance() 00108 luminance.SetInput(extract.GetOutput()) 00109 luminance.GetOutput().Update() 00110 ijkToRAS = vtk.vtkMatrix4x4() 00111 inputVolume.GetIJKToRASMatrix(ijkToRAS) 00112 outputVolume.SetIJKToRASMatrix(ijkToRAS) 00113 outputVolume.SetAndObserveImageData(luminance.GetOutput()) 00114 00115 # make the output volume appear in all the slice views 00116 selectionNode = slicer.app.applicationLogic().GetSelectionNode() 00117 selectionNode.SetReferenceActiveVolumeID(outputVolume.GetID()) 00118 slicer.app.applicationLogic().PropagateVolumeSelection(0)
1.7.4