title: how to write a read DICOM file using VTK or ITK libraries?
body: <p>I used vtkDICOMImageReader to read the DICOM file. I used the vtkImageThreshold to threshold a CT image. And now i want to write it back to my hard disk before further processing.</p>
  
  <p>I tried vtkImageWriter library to write it back. But it is not working when i try to open the file using 3D slicer. I am much grateful if anyone can suggest me a methodology for writing Dicom files. </p>
  
  <p>i have included my code here and i am trying to threshold a dicom image and viewing it. Then i would like to save the thresholded image as a dicom file. But i could not succeed in doing that. please help me.  </p>
  
  <p>thanks in advance. </p>
  
  <pre><code>#include &lt;itkImageToVTKImageFilter.h&gt;
  #include &lt;vtkSmartPointer.h&gt;
  #include &lt;vtkImageData.h&gt;
  #include &lt;vtkImageThreshold.h&gt;
  #include &lt;vtkRenderWindow.h&gt;
  #include &lt;vtkRenderWindowInteractor.h&gt;
  #include &lt;vtkInteractorStyleImage.h&gt;
  #include &lt;vtkRenderer.h&gt;
  #include &lt;vtkImageMapper3D.h&gt;
  #include &lt;vtkImageActor.h&gt;
  #include &lt;vtkImageCast.h&gt;
  #include &lt;vtkNIFTIImageWriter.h&gt;
  #include &lt;vtkImageMandelbrotSource.h&gt;
  #include &lt;vtkImageViewer2.h&gt;
  #include &lt;vtkDICOMImageReader.h&gt;
  
  int main(int argc, char* argv[])
  {
  
  std::string folder = argv[1];
  
  vtkSmartPointer&lt;vtkDICOMImageReader&gt; reader =
        vtkSmartPointer&lt;vtkDICOMImageReader&gt;::New();
  reader-&gt;SetFileName(folder.c_str());
  reader-&gt;Update();
  
  vtkSmartPointer&lt;vtkImageViewer2&gt; imageViewer =
    vtkSmartPointer&lt;vtkImageViewer2&gt;::New();
  imageViewer-&gt;SetInputConnection(reader-&gt;GetOutputPort());
  
  // threshold the images
  vtkSmartPointer&lt;vtkImageThreshold&gt; imageThreshold = 
      vtkSmartPointer&lt;vtkImageThreshold&gt;::New();
  imageThreshold-&gt;SetInputConnection(reader-&gt;GetOutputPort());
  // unsigned char lower = 127;
  unsigned char upper = 511;
  
  imageThreshold-&gt;ThresholdByLower(upper);
  imageThreshold-&gt;ReplaceInOn();
  imageThreshold-&gt;SetInValue(0);
  imageThreshold-&gt;ReplaceOutOn();
  imageThreshold-&gt;SetOutValue(511);
  imageThreshold-&gt;Update();
  
  // Create actors
  vtkSmartPointer&lt;vtkImageActor&gt; inputActor =
    vtkSmartPointer&lt;vtkImageActor&gt;::New();
  inputActor-&gt;GetMapper()-&gt;SetInputConnection(
    reader-&gt;GetOutputPort());
  
  vtkSmartPointer&lt;vtkImageActor&gt; thresholdedActor =
  vtkSmartPointer&lt;vtkImageActor&gt;::New();
  thresholdedActor-&gt;GetMapper()-&gt;SetInputConnection(
  imageThreshold-&gt;GetOutputPort());
  
  // There will be one render window
  vtkSmartPointer&lt;vtkRenderWindow&gt; renderWindow =
    vtkSmartPointer&lt;vtkRenderWindow&gt;::New();
  renderWindow-&gt;SetSize(600, 300);
  
  // And one interactor
  vtkSmartPointer&lt;vtkRenderWindowInteractor&gt; interactor =
  vtkSmartPointer&lt;vtkRenderWindowInteractor&gt;::New();
  interactor-&gt;SetRenderWindow(renderWindow);
  
  // Define viewport ranges
  // (xmin, ymin, xmax, ymax)
  double leftViewport[4] = {0.0, 0.0, 0.5, 1.0};
  double rightViewport[4] = {0.5, 0.0, 1.0, 1.0};
  
  // Setup both renderers
  vtkSmartPointer&lt;vtkRenderer&gt; leftRenderer =
    vtkSmartPointer&lt;vtkRenderer&gt;::New();
  renderWindow-&gt;AddRenderer(leftRenderer);
  leftRenderer-&gt;SetViewport(leftViewport);
  leftRenderer-&gt;SetBackground(.6, .5, .4);
  
  vtkSmartPointer&lt;vtkRenderer&gt; rightRenderer =
    vtkSmartPointer&lt;vtkRenderer&gt;::New();
  renderWindow-&gt;AddRenderer(rightRenderer);
  rightRenderer-&gt;SetViewport(rightViewport);
  rightRenderer-&gt;SetBackground(.4, .5, .6);
  
  leftRenderer-&gt;AddActor(inputActor);
  rightRenderer-&gt;AddActor(thresholdedActor);
  
  leftRenderer-&gt;ResetCamera();
  rightRenderer-&gt;ResetCamera();
  
  renderWindow-&gt;Render();
  interactor-&gt;Start();
  
  vtkSmartPointer&lt;vtkNIFTIImageWriter&gt; writer =
    vtkSmartPointer&lt;vtkNIFTIImageWriter&gt;::New();
  writer-&gt;SetInputConnection(reader-&gt;GetOutputPort());
  writer-&gt;SetFileName("output");
  writer-&gt;Write();
  
  // writing the thresholded image to the hard drive.
  //this is the part i am not able to code. Please can somebody help me     please? 
  
  return EXIT_SUCCESS;
  
   }
  </code></pre>