2.3. How to pad a ROIΒΆ

In this example, we copy a slice of a source ROI into different slices of a destination ROI, in order to pad a few slices at the lower Z and at the higher Z.

The source ROI is named sourceROI:

# Creating the destination ROI
destinationROI = sourceROI.copy()

# Adapting the shape of the destination ROI to contain the padding slices
countPaddingSlicesOnEachSide = 10
originalZSize = sourceROI.getZSize()
destinationROIZSize = originalZSize + 2*countPaddingSlicesOnEachSide
destinationROI.setZSize(destinationROIZSize)
destinationROIBox = destinationROI.getBox()
destinationROIDirection2 = destinationROIBox.getDirection2()
destinationROISpacingDirection2 = destinationROIBox.getDirection2Spacing()
destinationROIOrigin = destinationROIBox.getOrigin()
destinationROINewOrigin = destinationROIOrigin - countPaddingSlicesOnEachSide*destinationROISpacingDirection2*destinationROIDirection2
destinationROIBox.setOrigin(destinationROINewOrigin)
destinationROI.setBox(destinationROIBox)
destinationROI.clear()

timeStep = 0

# Copying common region data
# The method addROI uses a projection when the shape of the destination is not the same as the source
destinationROI.addROI(sourceROI)

# Padding at lower Z
# Getting the source data
tempROILowerZ = sourceROI.getSubset(0, 0, 0, timeStep, sourceROI.getXSize()-1, sourceROI.getYSize()-1, 0, timeStep, None, None)

# Copying the source data in all the padding slices
from ORSModel import Vector3
for zIndex in range(countPaddingSlicesOnEachSide):
    # Getting the coordinates of the origin to set in tempROILowerZ, to align tempROILowerZ with the padding slice to write
    originToSet = destinationROI.getVoxelToWorldCoordinates(Vector3(0, 0, zIndex))
    tempROILowerZ.setOrigin(originToSet)
    destinationROI.addROI(tempROILowerZ)

tempROILowerZ.deleteObject()

# Padding at higher Z
tempROIHigherZ = sourceROI.getSubset(0, 0, originalZSize-1, timeStep, sourceROI.getXSize()-1, sourceROI.getYSize()-1, originalZSize-1, timeStep, None, None)
for zIndex in range(countPaddingSlicesOnEachSide):
    originToSet = destinationROI.getVoxelToWorldCoordinates(Vector3(0, 0, destinationROIZSize-1-zIndex))
    tempROIHigherZ.setOrigin(originToSet)
    destinationROI.addROI(tempROIHigherZ)

tempROIHigherZ.deleteObject()

# Setting the properties of the new ROI
destinationROI.setTitle(sourceROI.getTitle() + ' (Padded)')

# Publishing the new object
destinationROI.publish()