The code below is the process of extracting polydata from the contour sequence and converting it into a volume image.
def contourSequence2Volume(cs, referenceImage):
polylines = contourSequence2PolyLines(cs)
whiteImage = vtk.vtkImageData()
whiteImage.SetDimensions(referenceImage.GetDicomImages().GetVolumeArray().shape)
whiteImage.SetOrigin(referenceImage.GetDicomImages().GetOrigin())
whiteImage.SetSpacing(referenceImage.GetDicomImages().GetSpacing())
whiteImage.AllocateScalars(vtk.VTK_UNSIGNED_CHAR, 1)
stencil = vtk.vtkPolyDataToImageStencil()
stencil.SetInputData(polylines)
stencil.SetOutputOrigin(referenceImage.GetDicomImages().GetOrigin())
stencil.SetOutputSpacing(referenceImage.GetDicomImages().GetSpacing())
stencil.SetOutputWholeExtent(whiteImage.GetExtent())
stencil.Update()
sten2img = vtk.vtkImageStencilToImage()
sten2img.SetInputConnection(stencil.GetOutputPort())
sten2img.SetOutsideValue(0)
sten2img.SetInsideValue(255)
sten2img.Update()
vtkimg = sten2img.GetOutput()
return vtkimg
The result is the same as in the picture.
I hope it becomes a smoother Contouring.
Can you suggest a good way?