def onApply(self): inputVolume = self.inputSelector.currentNode() outputVolume = self.outputSelector.currentNode() # check for input data if not (inputVolume and outputVolume): qt.QMessageBox.critical( slicer.util.mainWindow(), 'Luminance', 'Input and output volumes are required for conversion') return # check that data has enough components inputImage = inputVolume.GetImageData() if not inputImage or inputImage.GetNumberOfScalarComponents() < 3: qt.QMessageBox.critical( slicer.util.mainWindow(), 'Vector to Scalar Volume', 'Input does not have enough components for conversion') return # run the filter # - extract the RGB portions extract = vtk.vtkImageExtractComponents() extract.SetComponents(0,1,2) extract.SetInput(inputVolume.GetImageData()) luminance = vtk.vtkImageLuminance() luminance.SetInput(extract.GetOutput()) luminance.GetOutput().Update() ijkToRAS = vtk.vtkMatrix4x4() inputVolume.GetIJKToRASMatrix(ijkToRAS) outputVolume.SetIJKToRASMatrix(ijkToRAS) outputVolume.SetAndObserveImageData(luminance.GetOutput()) # make the output volume appear in all the slice views selectionNode = slicer.app.applicationLogic().GetSelectionNode() selectionNode.SetReferenceActiveVolumeID(outputVolume.GetID()) slicer.app.applicationLogic().PropagateVolumeSelection(0)
def onApply(self): inputVolume = self.inputSelector.currentNode() outputVolume = self.outputSelector.currentNode() # check for input data if not (inputVolume and outputVolume): qt.QMessageBox.critical( slicer.util.mainWindow(), 'Luminance', 'Input and output volumes are required for conversion') return # check that data has enough components inputImage = inputVolume.GetImageData() if not inputImage or inputImage.GetNumberOfScalarComponents() < 3: qt.QMessageBox.critical( slicer.util.mainWindow(), 'Vector to Scalar Volume', 'Input does not have enough components for conversion') return # run the filter # - extract the RGB portions extract = vtk.vtkImageExtractComponents() extract.SetComponents(0, 1, 2) extract.SetInput(inputVolume.GetImageData()) luminance = vtk.vtkImageLuminance() luminance.SetInput(extract.GetOutput()) luminance.GetOutput().Update() ijkToRAS = vtk.vtkMatrix4x4() inputVolume.GetIJKToRASMatrix(ijkToRAS) outputVolume.SetIJKToRASMatrix(ijkToRAS) outputVolume.SetAndObserveImageData(luminance.GetOutput()) # make the output volume appear in all the slice views selectionNode = slicer.app.applicationLogic().GetSelectionNode() selectionNode.SetReferenceActiveVolumeID(outputVolume.GetID()) slicer.app.applicationLogic().PropagateVolumeSelection(0)
def onApply(self): fixedImage = self.fixedSelector.currentNode() movingImage = self.movingSelector.currentNode() #outputImage = self.outputSelector.currentNode() fixedExtract = vtk.vtkImageExtractComponents() fixedExtract.SetComponents(0,1,2) fixedLuminance = vtk.vtkImageLuminance() fixedExtract.SetInputConnection(fixedImage.GetImageDataConnection()) fixedLuminance.SetInputConnection(fixedExtract.GetOutputPort()) fixedLuminance.Update() fixedIJKToRAS = vtk.vtkMatrix4x4() fixedImage.GetIJKToRASMatrix(fixedIJKToRAS) fixedScalarVolume = slicer.vtkMRMLScalarVolumeNode() fixedScalarVolume.SetName('fixedScalarImage') fixedScalarVolume.SetIJKToRASMatrix(fixedIJKToRAS) fixedScalarVolume.SetImageDataConnection(fixedLuminance.GetOutputPort()) slicer.mrmlScene.AddNode(fixedScalarVolume) movingExtract = vtk.vtkImageExtractComponents() movingExtract.SetComponents(0,1,2) movingLuminance = vtk.vtkImageLuminance() movingExtract.SetInputConnection(movingImage.GetImageDataConnection()) movingLuminance.SetInputConnection(movingExtract.GetOutputPort()) movingLuminance.Update() movingIJKToRAS = vtk.vtkMatrix4x4() movingImage.GetIJKToRASMatrix(movingIJKToRAS) movingScalarVolume = slicer.vtkMRMLScalarVolumeNode() movingScalarVolume.SetName('movingScalarImage') movingScalarVolume.SetIJKToRASMatrix(movingIJKToRAS) movingScalarVolume.SetImageDataConnection(movingLuminance.GetOutputPort()) slicer.mrmlScene.AddNode(movingScalarVolume) anglesNumber = self.numberOfAnglesBox.value print anglesNumber imageRegistration = slicer.modules.imageregistrationcli parameters = { "fixedImage": fixedScalarVolume, "movingImage": movingScalarVolume, "anglesNumber": anglesNumber, } slicer.cli.run( imageRegistration,None,parameters,wait_for_completion=True )
def onApply(self): inputVolume = self.inputSelector.currentNode() outputVolume = self.outputSelector.currentNode() if not (inputVolume and outputVolume): qt.QMessageBox.critical( slicer.util.mainWindow(), 'Luminance', 'Input and output volumes are required for conversion') return # run the filter luminance = vtk.vtkImageLuminance() luminance.SetInput(inputVolume.GetImageData()) luminance.GetOutput().Update() ijkToRAS = vtk.vtkMatrix4x4() inputVolume.GetIJKToRASMatrix(ijkToRAS) outputVolume.SetIJKToRASMatrix(ijkToRAS) outputVolume.SetAndObserveImageData(luminance.GetOutput()) # make the output volume appear in all the slice views selectionNode = slicer.app.applicationLogic().GetSelectionNode() selectionNode.SetReferenceActiveVolumeID(outputVolume.GetID()) slicer.app.applicationLogic().PropagateVolumeSelection(0)
def DoIt(inputDir, rgbDir, outputDir): # # Read the input DICOM series as a volume # dcmList = [] for dcm in os.listdir(inputDir): if len(dcm)-dcm.rfind('.dcm') == 4: dcmList.append(inputDir+'/'+dcm) scalarVolumePlugin = slicer.modules.dicomPlugins['DICOMScalarVolumePlugin']() print 'Will examine: ',dcmList indexer = ctk.ctkDICOMIndexer() indexer.addDirectory(slicer.dicomDatabase, inputDir) indexer.waitForImportFinished() loadables = scalarVolumePlugin.examine([dcmList]) if len(loadables) == 0: print 'Could not parse the DICOM Study!' exit() inputVolume = scalarVolumePlugin.load(loadables[0]) sNode = slicer.vtkMRMLVolumeArchetypeStorageNode() ''' sNode.ResetFileNameList() for f in loadables[0].files: sNode.AddFileName(f) sNode.SetFileName(loadables[0].files[0]) sNode.SetSingleFile(0) inputVolume = slicer.vtkMRMLScalarVolumeNode() sNode.ReadData(inputVolume) ''' sNode.SetWriteFileFormat('nrrd') sNode.SetFileName(os.path.join(outputDir,'input_volume.nrrd')) sNode.WriteData(inputVolume) # # Order the input RGBs and rename in a temp directory # rgbList = [] for rgb in os.listdir(rgbDir): if len(rgb)-rgb.rfind('.bmp') == 4: rgbList.append(rgb) tmpDir = slicer.app.settings().value('Modules/TemporaryDirectory') tmpDir = tmpDir+'/PNGStackLabelConverter' if not os.path.exists(tmpDir): os.mkdir(tmpDir) oldFiles = os.listdir(tmpDir) # just in case there is anything in that directory for f in oldFiles: os.unlink(tmpDir+'/'+f) rgbOrdered = [None] * len(loadables[0].files) rgbCnt = 0 rgbExt = rgbList[0][rgbList[0].rfind('.')+1:len(rgbList[0])] print 'Extension for RGBs: ',rgbExt dcmFileList = loadables[0].files rgbRenamedList = [] print 'Number of dcm files: ',len(dcmFileList), ' and rgb files: ',len(rgbOrdered) dcmIdx = 0 for dcm in dcmFileList: rgbIdx = 0 for rgb in rgbList: dcmPrefix = dcm[dcm.rfind('/')+1:dcm.rfind('.')] if rgb.find(dcmPrefix) != -1: name = string.zfill(str(dcmIdx),5) rgbCnt = rgbCnt+1 src = rgbDir+'/'+rgb dest = tmpDir+'/'+name+'.'+rgbExt rgbRenamedList.append(dest) shutil.copy(src,dest) break rgbIdx = rgbIdx+1 # remove the matched DICOM file from the list if rgbIdx == len(rgbList): print('ERROR: failed to find matching label file for DICOM file '+dcm) return del rgbList[rgbIdx] dcmIdx = dcmIdx+1 if len(rgbRenamedList) == 0: print 'Could not parse the DICOM Study!' return sNode = slicer.vtkMRMLVolumeArchetypeStorageNode() sNode.ResetFileNameList() for f in rgbRenamedList: sNode.AddFileName(f) sNode.SetFileName(rgbRenamedList[0]) sNode.SetSingleFile(0) inputRGBVolume = slicer.vtkMRMLVectorVolumeNode() sNode.ReadData(inputRGBVolume) # run the filter # - extract the RGB portions extract = vtk.vtkImageExtractComponents() extract.SetComponents(0,1,2) if vtk.vtkVersion().GetVTKMajorVersion() < 6: extract.SetInput(inputRGBVolume.GetImageData()) else: extract.SetInputData(inputRGBVolume.GetImageData()) luminance = vtk.vtkImageLuminance() if vtk.vtkVersion().GetVTKMajorVersion() < 6: luminance.SetInput(extract.GetOutput()) else: luminance.SetInputData(extract.GetOutput()) cast = vtk.vtkImageCast() if vtk.vtkVersion().GetVTKMajorVersion() < 6: cast.SetInput(luminance.GetOutput()) else: cast.SetInputData(luminance.GetOutput()) cast.SetOutputScalarTypeToShort() cast.GetOutput().Update() ijkToRAS = vtk.vtkMatrix4x4() inputVolume.GetIJKToRASMatrix(ijkToRAS) outputLabel = slicer.vtkMRMLLabelMapVolumeNode() outputLabel.SetIJKToRASMatrix(ijkToRAS) outputLabel.SetAndObserveImageData(cast.GetOutput()) reportingLogic = slicer.modules.reporting.logic() displayNode = slicer.vtkMRMLLabelMapVolumeDisplayNode() displayNode.SetAndObserveColorNodeID(reportingLogic.GetDefaultColorNode().GetID()) slicer.mrmlScene.AddNode(displayNode) outputLabel.SetAndObserveDisplayNodeID(displayNode.GetID()) sNode.SetWriteFileFormat('nrrd') sNode.SetFileName(os.path.join(outputDir,'label_output.nrrd')) sNode.WriteData(outputLabel) # save as DICOM SEG labelCollection = vtk.vtkCollection() labelCollection.AddItem(outputLabel) slicer.mrmlScene.AddNode(inputVolume) outputLabel.SetAttribute('AssociatedNodeID',inputVolume.GetID()) slicer.mrmlScene.AddNode(outputLabel) # initialize the DICOM DB for Reporting logic settings = qt.QSettings() dbFileName = settings.value('DatabaseDirectory','') if dbFileName =='': print('ERROR: database must be initialized') else: dbFileName = dbFileName +'/ctkDICOM.sql' reportingLogic.InitializeDICOMDatabase(dbFileName) reportingLogic.DicomSegWrite(labelCollection, outputDir)
def DoIt(inputDir, rgbDir, outputDir): # # Read the input DICOM series as a volume # dcmList = [] for dcm in os.listdir(inputDir): if len(dcm) - dcm.rfind('.dcm') == 4: dcmList.append(inputDir + '/' + dcm) scalarVolumePlugin = slicer.modules.dicomPlugins[ 'DICOMScalarVolumePlugin']() print 'Will examine: ', dcmList indexer = ctk.ctkDICOMIndexer() indexer.addDirectory(slicer.dicomDatabase, inputDir) indexer.waitForImportFinished() loadables = scalarVolumePlugin.examine([dcmList]) if len(loadables) == 0: print 'Could not parse the DICOM Study!' exit() inputVolume = scalarVolumePlugin.load(loadables[0]) sNode = slicer.vtkMRMLVolumeArchetypeStorageNode() ''' sNode.ResetFileNameList() for f in loadables[0].files: sNode.AddFileName(f) sNode.SetFileName(loadables[0].files[0]) sNode.SetSingleFile(0) inputVolume = slicer.vtkMRMLScalarVolumeNode() sNode.ReadData(inputVolume) ''' sNode.SetWriteFileFormat('nrrd') sNode.SetFileName(os.path.join(outputDir, 'input_volume.nrrd')) sNode.WriteData(inputVolume) # # Order the input RGBs and rename in a temp directory # rgbList = [] for rgb in os.listdir(rgbDir): if len(rgb) - rgb.rfind('.bmp') == 4: rgbList.append(rgb) tmpDir = slicer.app.settings().value('Modules/TemporaryDirectory') tmpDir = tmpDir + '/PNGStackLabelConverter' if not os.path.exists(tmpDir): os.mkdir(tmpDir) oldFiles = os.listdir(tmpDir) # just in case there is anything in that directory for f in oldFiles: os.unlink(tmpDir + '/' + f) rgbOrdered = [None] * len(loadables[0].files) rgbCnt = 0 rgbExt = rgbList[0][rgbList[0].rfind('.') + 1:len(rgbList[0])] print 'Extension for RGBs: ', rgbExt dcmFileList = loadables[0].files rgbRenamedList = [] print 'Number of dcm files: ', len(dcmFileList), ' and rgb files: ', len( rgbOrdered) dcmIdx = 0 for dcm in dcmFileList: rgbIdx = 0 for rgb in rgbList: dcmPrefix = dcm[dcm.rfind('/') + 1:dcm.rfind('.')] if rgb.find(dcmPrefix) != -1: name = string.zfill(str(dcmIdx), 5) rgbCnt = rgbCnt + 1 src = rgbDir + '/' + rgb dest = tmpDir + '/' + name + '.' + rgbExt rgbRenamedList.append(dest) shutil.copy(src, dest) break rgbIdx = rgbIdx + 1 # remove the matched DICOM file from the list if rgbIdx == len(rgbList): print('ERROR: failed to find matching label file for DICOM file ' + dcm) return del rgbList[rgbIdx] dcmIdx = dcmIdx + 1 if len(rgbRenamedList) == 0: print 'Could not parse the DICOM Study!' return sNode = slicer.vtkMRMLVolumeArchetypeStorageNode() sNode.ResetFileNameList() for f in rgbRenamedList: sNode.AddFileName(f) sNode.SetFileName(rgbRenamedList[0]) sNode.SetSingleFile(0) inputRGBVolume = slicer.vtkMRMLVectorVolumeNode() sNode.ReadData(inputRGBVolume) # run the filter # - extract the RGB portions extract = vtk.vtkImageExtractComponents() extract.SetComponents(0, 1, 2) if vtk.vtkVersion().GetVTKMajorVersion() < 6: extract.SetInput(inputRGBVolume.GetImageData()) else: extract.SetInputData(inputRGBVolume.GetImageData()) luminance = vtk.vtkImageLuminance() if vtk.vtkVersion().GetVTKMajorVersion() < 6: luminance.SetInput(extract.GetOutput()) else: luminance.SetInputData(extract.GetOutput()) cast = vtk.vtkImageCast() if vtk.vtkVersion().GetVTKMajorVersion() < 6: cast.SetInput(luminance.GetOutput()) else: cast.SetInputData(luminance.GetOutput()) cast.SetOutputScalarTypeToShort() cast.GetOutput().Update() ijkToRAS = vtk.vtkMatrix4x4() inputVolume.GetIJKToRASMatrix(ijkToRAS) outputLabel = slicer.vtkMRMLLabelMapVolumeNode() outputLabel.SetIJKToRASMatrix(ijkToRAS) outputLabel.SetAndObserveImageData(cast.GetOutput()) reportingLogic = slicer.modules.reporting.logic() displayNode = slicer.vtkMRMLLabelMapVolumeDisplayNode() displayNode.SetAndObserveColorNodeID( reportingLogic.GetDefaultColorNode().GetID()) slicer.mrmlScene.AddNode(displayNode) outputLabel.SetAndObserveDisplayNodeID(displayNode.GetID()) sNode.SetWriteFileFormat('nrrd') sNode.SetFileName(os.path.join(outputDir, 'label_output.nrrd')) sNode.WriteData(outputLabel) # save as DICOM SEG labelCollection = vtk.vtkCollection() labelCollection.AddItem(outputLabel) slicer.mrmlScene.AddNode(inputVolume) outputLabel.SetAttribute('AssociatedNodeID', inputVolume.GetID()) slicer.mrmlScene.AddNode(outputLabel) # initialize the DICOM DB for Reporting logic settings = qt.QSettings() dbFileName = settings.value('DatabaseDirectory', '') if dbFileName == '': print('ERROR: database must be initialized') else: dbFileName = dbFileName + '/ctkDICOM.sql' reportingLogic.InitializeDICOMDatabase(dbFileName) reportingLogic.DicomSegWrite(labelCollection, outputDir)