MINCExample01

From KitwarePublic
Jump to navigationJump to search

First itkMINC2IO example code (And test!)

/*=========================================================================

  Copyright (c) Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif

#include <stdio.h>
#include <fstream>
#include "itkImageFileReader.h"
#include "itkImage.h"

#include "itkImageIOFactory.h"
#include "itkMINC2ImageIOFactory.h"
#include "itkMINC2ImageIO.h"
#include "itkIOCommon.h"

#include "itkExceptionObject.h"


int itkMINC2ImageIOTest01(int ac, char *av[]){
   
   if(ac < 2){
      std::cerr << "Usage: " << av[0] << " Input.mnc\n";
      return EXIT_FAILURE;
      }
   
   std::cerr << "Testing MINC2 Image Input\n";
   
   // create an ImageType
   typedef itk::Image< float, 3 > ImageType;

   // and a reader type
   typedef itk::ImageFileReader< ImageType > ReaderType;
   
   // set up an instance of this reader
   ReaderType::Pointer reader = ReaderType::New();
   
   // assign a filename to read from
   reader->SetFileName(av[1]);
   
   // now set up an IO type
   typedef itk::MINC2ImageIO ImageIOType;
   ImageIOType::Pointer minc2ImageIO = ImageIOType::New();
   
   // assign it to the reader
   reader->SetImageIO(minc2ImageIO);

   // call Update to read the data
   try {
      reader->Update();
      }
   catch(itk::ExceptionObject & e){
      std::cerr << av[0] << "Exception reader->Update()" << std::endl;
      std::cerr << e.GetDescription() << std::endl;
      std::cerr << e.GetLocation() << std::endl;
      return EXIT_FAILURE;
      }
   
   // now do something with the data itself
   ImageType::Pointer image;
   try {
      image = reader->GetOutput();
      }
   catch(itk::ExceptionObject & e){
      std::cerr << av[0] << "Exception reader->GetOutput()" << std::endl;
      std::cerr << e.GetDescription() << std::endl;
      std::cerr << e.GetLocation() << std::endl;
      return EXIT_FAILURE;
      }
      
   // origin
   try {
      const ImageType::PointType& origin = image->GetOrigin();
      std::cout << "Origin = ";
      std::cout << origin[0] << ", " << origin[1] << ", " << origin[2] << std::endl;
      }
   catch(itk::ExceptionObject & e){
      std::cerr << av[0] << "Exception image->GetOrigin()" << std::endl;
      std::cerr << e.GetDescription() << std::endl;
      std::cerr << e.GetLocation() << std::endl;
      return EXIT_FAILURE;
      }

   // spacing
   try {
      const ImageType::SpacingType& spacing = image->GetSpacing();
      std::cout << "Spacing = ";
      std::cout << spacing[0] << ", " << spacing[1] << ", " << spacing[2] << std::endl;
      }
   catch(itk::ExceptionObject & e){
      std::cerr << av[0] << "Exception image->GetSpacing()" << std::endl;
      std::cerr << e.GetDescription() << std::endl;
      std::cerr << e.GetLocation() << std::endl;
      return EXIT_FAILURE;
      }

   // dimension order
   try {
      char *dimorder = minc2ImageIO->GetDimensionOrder();
      std::cout << "Dimension Order " << dimorder << std::endl;
      }
   catch(itk::ExceptionObject & e){
      std::cerr << av[0] << "Exception minc2ImageIO->GetDimensionOrder()" << std::endl;
      std::cerr << e.GetDescription() << std::endl;
      std::cerr << e.GetLocation() << std::endl;
      return EXIT_FAILURE;
      }
   
   // dimensions
   unsigned int dim[12];  // just in case
   unsigned int ndim;
   try {
      ndim = minc2ImageIO->GetNumberOfDimensions();
      std::cout << "Dimensions[" << ndim << "] = ";
      }
   catch(itk::ExceptionObject & e){
      std::cerr << av[0] << "Exception minc2ImageIO->GetNumberOfDimensions()" << std::endl;
      std::cerr << e.GetDescription() << std::endl;
      std::cerr << e.GetLocation() << std::endl;
      return EXIT_FAILURE;
      }
   for(unsigned int i = 0; i < ndim; i++){
      try {
         dim[i] = minc2ImageIO->GetDimensions(i);
         std::cout << dim[i] << "  ";
         }
      catch(itk::ExceptionObject & e){
         std::cerr << av[0] << "Exception minc2ImageIO->GetNumberOfDimensions()" << std::endl;
         std::cerr << e.GetDescription() << std::endl;
         std::cerr << e.GetLocation() << std::endl;
         return EXIT_FAILURE;
         }
      }
   std::cout << std::endl;
   
   // variable for getting data from a certain point
   int stuffup_flag = 0;
   ImageType::IndexType pixelIndex;
   ImageType::PointType point;
   ImageType::PixelType value;
   ImageType::PixelType supposedvalue;
   for(unsigned int i = 0; i < dim[0]; i++){
      for(unsigned int j = 0; j < dim[1]; j++){
         for(unsigned int k = 0; k < dim[2]; k++){
            pixelIndex[0] = i;
            pixelIndex[1] = j;
            pixelIndex[2] = k;

            try {
               value = image->GetPixel( pixelIndex );
               }
            catch(itk::ExceptionObject & e){
               std::cerr << av[0] << "Exception image->GetPixel()" << std::endl;
               std::cerr << e.GetDescription() << std::endl;
               std::cerr << e.GetLocation() << std::endl;
               return EXIT_FAILURE;
               }

            supposedvalue=static_cast<ImageType::PixelType>(i + 2*j + 10*k);
            if(value != supposedvalue){
               std::cerr << av[0] << " BOGUS! Value " << i << ", " << j << ", " << k << " is " <<
                  value << "  should be " << supposedvalue << std::endl;
               stuffup_flag = 1;
               }

             // figure ou where we are in the real world
            image->TransformIndexToPhysicalPoint(pixelIndex, point);
            std::cout << "Point[" << i << "," << j << "," << k << "] = [";
            std::cout << point[0] << "," << point[1] << "," << point[2] << "]" << std::endl;
            }
         }
      }
   
   if(stuffup_flag){
      std::cerr << av[0] << " Failed on value checking" << std::endl;
      return EXIT_FAILURE;
      }
      
   std::cerr << "Done\n";
   
   return EXIT_SUCCESS;
   }