[ITK] Read raw image and save as nii

Paulo Guilherme Freire pauloguilhermefreire at gmail.com
Fri May 20 12:36:17 EDT 2016


Hello, everyone.

I am trying to do something quite simple: I have a raw image (and its
corresponding .nhdr file) and I want to read it and save it in .nii format.
The image type, according to the .nhdr file, is short. Below is the code
I'm using.

My question is: what am I missing here? After I save the image to file and
open it, I don't get the expected result; rather, I get an image that
reminds a TV static.

Could you please help me?

Thanks in advance!

Best,
Paulo

#include <iostream>
#include <itkImage.h>
#include <itkImageFileWriter.h>
#include <itkImageFileReader.h>

int main(int argc, char* argv[])
{

  typedef itk::Image<short, 3> ImageType;

  if(argc < 3)
  {
    // programName <filename>.nhdr <filename>.nii
    std::cerr << "Usage: " << argv[0] << " Output1.nhdr Output2.nii\n";
    return EXIT_FAILURE;
  }

  // Read .nhdr file
  typedef itk::ImageFileReader< ImageType > ReaderType;
  ReaderType::Pointer reader = ReaderType::New();
  reader->SetFileName(argv[1]);
  reader->Update();

  // Save it as nii
  typedef itk::ImageFileWriter< ImageType > WriterType;
  WriterType::Pointer writer = WriterType::New();
  writer->UseInputMetaDataDictionaryOn(); // is this necessary?
  writer->SetInput(reader->GetOutput());
  writer->SetFileName(argv[2]);

  try
  {
    writer->Update();
  }
  catch(itk::ExceptionObject e)
  {
    std::cout << "Could not save image to file\n";
  }

  return EXIT_SUCCESS;
}


-- 
"Se podes olhar, vê. Se podes ver, repara."
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/community/attachments/20160520/51557c56/attachment-0001.html>


More information about the Community mailing list