예제 #1
0
void transform(string inputFilename, string outputFilename)
{
    typedef itk::Image< TPixelType, N > ImageType;
    typedef itk::ImageFileReader< ImageType > ReaderType;
    typedef itk::ImageFileWriter< ImageType > WriterType;
    
	typename ReaderType::Pointer reader = ReaderType::New();
    itk::NiftiImageIO::Pointer io = itk::NiftiImageIO::New();
	reader->SetImageIO(io);
	reader->SetFileName(inputFilename);
    try {
        reader->Update();
    } catch( itk::ExceptionObject & e ) {
		std::cerr << "Exception caught while reading image " << std::endl;
		std::cerr << e << std::endl;
	}
    
    typename WriterType::Pointer writer = WriterType::New();
	writer->SetImageIO(io);
	writer->SetFileName(outputFilename);
	writer->SetInput(reader->GetOutput());
	try {
		writer->Update();
	} catch( itk::ExceptionObject & e ) {
		std::cerr << "Exception caught while writing image " << std::endl;
		std::cerr << e << std::endl;
	}
}
예제 #2
0
mitk::BaseData::Pointer mitk::RawImageFileReaderService::TypedRead(const std::string& path, EndianityType endianity, int* size)
{
  typedef itk::Image< TPixel, VImageDimensions > ImageType;
  typedef itk::ImageFileReader< ImageType > ReaderType;
  typedef itk::RawImageIO< TPixel, VImageDimensions >  IOType;

  typename ReaderType::Pointer reader = ReaderType::New();
  typename IOType::Pointer io = IOType::New();

  io->SetFileDimensionality(VImageDimensions);

  for (unsigned short int dim = 0; dim < VImageDimensions; ++dim)
  {
    io->SetDimensions(dim, size[dim] );
  }

  if (endianity == LITTLE)
  {
    io->SetByteOrderToLittleEndian();
  }
  else if (endianity == BIG)
  {
    io->SetByteOrderToBigEndian();
  }
  else
  {
    MITK_INFO << "Warning: endianity not properly set. Resulting image might be incorrect";
  }

  reader->SetImageIO( io );
  reader->SetFileName(path);

  try
  {
    reader->Update();
  }
  catch( itk::ExceptionObject & err )
  {
    MITK_ERROR <<"An error occurred during the raw image reading process: ";
    MITK_INFO << err << std::endl;
  }

  mitk::Image::Pointer image = mitk::Image::New();
  mitk::CastToMitkImage(reader->GetOutput(), image);
  image->SetVolume(  reader->GetOutput()->GetBufferPointer());
  return image.GetPointer();
}
예제 #3
0
int changeOrientationMethod(string inputFilename, string outputFilename, OrientationType orientation, bool changeOrientation, bool displayInitialOrientation, bool displayAvailableOrientation)
{
    typedef itk::Image< TPixelType, N >	ImageType;
    typedef itk::ImageFileReader<ImageType> ReaderType;
    typedef itk::ImageFileWriter<ImageType> WriterType;
    
    typename ReaderType::Pointer reader = ReaderType::New();
	itk::NiftiImageIO::Pointer io = itk::NiftiImageIO::New();
	reader->SetImageIO(io);
	reader->SetFileName(inputFilename);
    
    OrientImage<ImageType> orientationFilter;
    orientationFilter.setInputImage(reader->GetOutput());
    
	if (displayInitialOrientation)
    {
        try {
            io->SetFileName(inputFilename);
            io->ReadImageInformation();
            //reader->Update();
        } catch( itk::ExceptionObject & e ) {
            std::cerr << "Exception caught while reading input image " << std::endl;
            std::cerr << e << std::endl;
        }
        
        typename ImageType::DirectionType direction;
        vector<double> dir0 = io->GetDirection(0);
        for (int i=0; i<dir0.size(); i++)
            direction(i,0) = dir0[i];
        vector<double> dir1 = io->GetDirection(1);
        for (int i=0; i<dir1.size(); i++)
            direction(i,1) = dir1[i];
        vector<double> dir2 = io->GetDirection(2);
        for (int i=0; i<dir2.size(); i++)
            direction(i,2) = dir2[i];
        cout << direction << endl;
        cout << "Input image orientation : " << FlagToString(orientationFilter.getOrientationFromDirection(direction)) << endl;
    }
    
	if (changeOrientation)
	{
        try {
            io->SetFileName(inputFilename);
            io->ReadImageInformation();
            //reader->Update();
        } catch( itk::ExceptionObject & e ) {
            std::cerr << "Exception caught while reading input image " << std::endl;
            std::cerr << e << std::endl;
        }
        
		orientationFilter.orientation(orientation);
        
		typename WriterType::Pointer writer = WriterType::New();
		writer->SetImageIO(io);
		writer->SetFileName(outputFilename);
		writer->SetInput(orientationFilter.getOutputImage());
		try {
			writer->Write();
		} catch( itk::ExceptionObject & e ) {
			std::cerr << "Exception caught while writing output image " << std::endl;
			std::cerr << e << std::endl;
		}
	}
    return EXIT_SUCCESS;
}