# Writing polyData as structured .VTK Imagedata

**URL:** https://discourse.vtk.org/t/writing-polydata-as-structured-vtk-imagedata/2608
**Category:** Support
**Created:** [February 13, 2020, 2:03pm UTC](https://discourse.vtk.org/t/writing-polydata-as-structured-vtk-imagedata/2608 "2020-02-13T14:03:21Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ShaneKirkbride](https://discourse.vtk.org/user_avatar/discourse.vtk.org/shanekirkbride/32/1392_2.png) [@ShaneKirkbride](https://discourse.vtk.org/u/ShaneKirkbride)
#### Post date: [February 13, 2020, 2:03pm UTC](https://discourse.vtk.org/t/writing-polydata-as-structured-vtk-imagedata/2608/1 "2020-02-13T14:03:21Z")

</div>

I can write polyData to a .vtk file but it is an unstructured blob. I need a header to preserve my geometry of my object (I want to read it into an ITK registration application).

I saw the example on PolyData to ImageData: [https://vtk.org/Wiki/VTK/Examples/Cxx/PolyData/PolyDataToImageData](https://vtk.org/Wiki/VTK/Examples/Cxx/PolyData/PolyDataToImageData) but this writes it to a .mhd file. Are there any examples that write to a .vtk file?  
thanks,  
~Shane

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [February 13, 2020, 7:22pm UTC](https://discourse.vtk.org/t/writing-polydata-as-structured-vtk-imagedata/2608/2 "2020-02-13T19:22:36Z")

</div>

VTK and ITK can read .mhd (metaimage files), so I would recommend to use this format.

ITK cannot read .vtk image file, the file format can store image or various mesh types, which often can cause misunderstandings, and it is a legacy file format (replaced by XML-based vti, vtp, vtu, … format), so vtk would not be my preferred format.

---

<div class="post-metadata">

### Author: ![ShaneKirkbride](https://discourse.vtk.org/user_avatar/discourse.vtk.org/shanekirkbride/32/1392_2.png) [@ShaneKirkbride](https://discourse.vtk.org/u/ShaneKirkbride)
#### Post date: [February 13, 2020, 7:47pm UTC](https://discourse.vtk.org/t/writing-polydata-as-structured-vtk-imagedata/2608/3 "2020-02-13T19:47:20Z")

</div>

@lassoan I came to the same conclusion. I rebuilt this morning with the MetalIO factory to read these but I think there has to be another way to convert a vtkImage to an ITK Image. I hacked out this way:

```
    reader->SetDirectoryName(target.c_str());
    //reader->SetDirectoryName(argv[1]);
    reader->Update();
    targetVolume->DeepCopy(reader->GetOutput());
    targetIsoValue = 150; // atof(150);
    //Put the target image into ITK
    VTKImageToImageType::Pointer vtkImageTargetToImageFilter = VTKImageToImageType::New();
    vtkImageTargetToImageFilter->SetInput(targetVolume);
    vtkImageTargetToImageFilter->Update();
    ImageType::Pointer targetImage = ImageType::New();
    targetImage->Graft(vtkImageTargetToImageFilter->GetOutput()); // Need to do this because QuickView can't accept const

```

It seems to be working as far as letting the software compile and registering correctly.

Does this way look right to you? Thank you!!!

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [February 13, 2020, 7:50pm UTC](https://discourse.vtk.org/t/writing-polydata-as-structured-vtk-imagedata/2608/4 "2020-02-13T19:50:28Z")

</div>

Without seeing the complete code, I cannot tell much. If the question is if it is better to keep the image in memory to pass it from VTK to ITK (using ITKVTKGlue or VTK image export/ITK image import) than writing to file/reading from file - then the answer is yes.
