Skip to content

Instantly share code, notes, and snippets.

@pieper
Created June 27, 2022 16:21
Show Gist options
  • Save pieper/10ee6add544633f4c75dbb293ef087bc to your computer and use it in GitHub Desktop.
Save pieper/10ee6add544633f4c75dbb293ef087bc to your computer and use it in GitHub Desktop.
reading multichannel tiff
"""
p = "/Volumes/GoogleDrive/My Drive/hacks/sardana.py"
exec(open(p).read())
Screencapture of result posted here (slide 7): https://docs.google.com/presentation/d/12cDI-HHQ4OhNTN4oqx-yFiyPAqvDB5sxKrimrWgdAOY/edit?usp=sharing
"""
#
# make RGBA of three
#
referenceNode = getNode("1_post_uint8")
redArray = array(getNode("1_post_uint8").GetID())
greenArray = array(getNode("16_post_uint8").GetID())
blueArray = array(getNode("30_post_uint8").GetID())
try:
rgbaNode = getNode("RGBAVolume")
except slicer.util.MRMLNodeNotFoundException:
rgbaNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLVectorVolumeNode")
rgbaNode.SetName("RGBAVolume")
referenceImage = referenceNode.GetImageData()
rgbaImage = vtk.vtkImageData()
rgbaImage.SetDimensions(referenceImage.GetDimensions())
rgbaImage.AllocateScalars(referenceImage.GetScalarType(), 4)
rgbaNode.SetAndObserveImageData(rgbaImage)
rgbaArray = slicer.util.array(rgbaNode.GetID())
rgbaArray[:,:,:,0] = redArray
rgbaArray[:,:,:,1] = greenArray
rgbaArray[:,:,:,2] = blueArray
rgbaArray[:,:,:,3] = (redArray + blueArray + greenArray) / 3
slicer.util.arrayFromVolumeModified(rgbaNode)
"""
volumeRenderingLogic = slicer.modules.volumerendering.logic()
displayNode = volumeRenderingLogic.CreateDefaultVolumeRenderingNodes(rgbaNode)
displayNode.SetVisibility(True)
# Enable direct RGBA color mapping
displayNode.GetVolumePropertyNode().GetVolumeProperty().SetIndependentComponents(False)
"""
#
# make RGB of three
#
referenceNode = getNode("1_post_uint8")
redArray = array(getNode("1_post_uint8").GetID())
greenArray = array(getNode("16_post_uint8").GetID())
blueArray = array(getNode("30_post_uint8").GetID())
try:
rbgNode = getNode("RGBVolume")
except slicer.util.MRMLNodeNotFoundException:
rbgNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLVectorVolumeNode")
rbgNode.SetName("RGBVolume")
referenceImage = referenceNode.GetImageData()
rbgImage = vtk.vtkImageData()
rbgImage.SetDimensions(referenceImage.GetDimensions())
rbgImage.AllocateScalars(referenceImage.GetScalarType(), 3)
rbgNode.SetAndObserveImageData(rbgImage)
rbgArray = slicer.util.array(rbgNode.GetID())
rbgArray[:,:,:,0] = redArray
rbgArray[:,:,:,1] = greenArray
rbgArray[:,:,:,2] = blueArray
slicer.util.arrayFromVolumeModified(rbgNode)
#
# make RGB of all
#
referenceNode = getNode("1_post_uint8")
referenceImage = referenceNode.GetImageData()
nodes = getNodes('*_post_uint8')
nodeKeyList = list(nodes.keys())
nodeIndex = 0
nodeCount = len(nodeKeyList)
for iteration in range(int(nodeCount/3)):
rbgNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLVectorVolumeNode")
rbgNode.SetName("RGBVolume" + str(iteration))
rbgImage = vtk.vtkImageData()
rbgImage.SetDimensions(referenceImage.GetDimensions())
rbgImage.AllocateScalars(referenceImage.GetScalarType(), 3)
rbgNode.SetAndObserveImageData(rbgImage)
redArray = slicer.util.array(nodes[nodeKeyList[nodeIndex+0]].GetID())
greenArray = slicer.util.array(nodes[nodeKeyList[nodeIndex+1]].GetID())
blueArray = slicer.util.array(nodes[nodeKeyList[nodeIndex+2]].GetID())
nodeIndex += 3
rbgArray = slicer.util.array(rbgNode.GetID())
rbgArray[:,:,:,0] = redArray
rbgArray[:,:,:,1] = greenArray
rbgArray[:,:,:,2] = blueArray
slicer.util.arrayFromVolumeModified(rbgNode)
print("finished iteration " + str(iteration))
# rename files based on marker names
import csv
import shutil
markerNames = []
with open("/opt/data/idc/htan/Sardana/markers_SARDANA_csv.csv") as fp:
reader = csv.reader(fp)
for row in reader:
markerNames.append(row[2])
rgbPath = "/opt/data/idc/htan/Sardana/RGB"
markerIndex = 1
for iteration in range(int(len(markerNames)/3)):
destFileName = f"{rgbPath}/{markerNames[markerIndex]}+{markerNames[markerIndex+1]}+{markerNames[markerIndex+2]}.nrrd"
srcFileName = f"{rgbPath}/RGBVolume{iteration}.nrrd"
print(f"copy {srcFileName} {destFileName}")
shutil.copyfile(srcFileName, destFileName)
markerIndex += 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment