# Polydata to 3d image!

**URL:** <https://discourse.vtk.org/t/polydata-to-3d-image/10331>\
**Category:** Development\
**Tags:** python\
**Created:** [December 28, 2022, 10:09am UTC](https://discourse.vtk.org/t/polydata-to-3d-image/10331 "2022-12-28T10:09:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jongzzi\_GanJang](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jongzzi_ganjang/32/6318_2.png) [@jongzzi\_GanJang](https://discourse.vtk.org/u/jongzzi_GanJang)\
**Post date:** [December 28, 2022, 10:09am UTC](https://discourse.vtk.org/t/polydata-to-3d-image/10331/1 "2022-12-28T10:09:02Z")

</div>

The code below is the process of extracting polydata from the contour sequence and converting it into a volume image.

```auto
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?

 ![스크린샷 2022-12-28 오후 6.56.57](https://discourse.vtk.org/uploads/default/original/2X/e/e4694e9105cb4d757f4490590a870f70b5937665.jpeg)
