[ITK] Read raw image and save as nii
Matt McCormick
matt.mccormick at kitware.com
Fri May 20 14:03:38 EDT 2016
Hi Paulo,
Your code looks good. Is the image pixel type correct for the input data?
HTH,
Matt
On Fri, May 20, 2016 at 12:36 PM, Paulo Guilherme Freire
<pauloguilhermefreire at gmail.com> wrote:
> 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."
>
> _______________________________________________
> Community mailing list
> Community at itk.org
> http://public.kitware.com/mailman/listinfo/community
>
More information about the Community
mailing list