Problem in extracting mnc file

Hi all, I am using the following code in order to convert MNC file to readable format for 3D slicer. But there are some errors in the code. for example in line 52 , it is mentioning qualified name is not allowed. I downloaded the whole project from this website ( the name of the project file is [MINC2_ITK_APR13.tar.gz ]

https://www.insight-journal.org/browse/publication/88
I really appreciate it if anyone can help me with this issue.

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

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile: itkJPEGImageIOTest.cxx,v $
  Language:  C++
  Date:      $Date: 2003/09/27 17:34:28 $
  Version:   $Revision: 1.1 $

  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 <fstream>
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkImage.h"
#include "itkOrientedImage.h"
#include "itkImageIOFactory.h"
#include "itkMINC2ImageIOFactory.h"
#include "itkMINC2ImageIO.h"
#include <stdio.h>
#include "itkMetaDataObject.h"
#include "itkIOCommon.h"
#include "itkPoint.h"
#include "itkResampleImageFilter.h"
#include "itkLinearInterpolateImageFunction.h"
#include "itkExceptionObject.h"
#include "itkVector.h"
#include "itkAffineTransform.h"
#include "itkNearestNeighborInterpolateImageFunction.h"
#include "itkIdentityTransform.h"

int main( int ac, char* av[] )
{
   if(ac < 3)
     {
     std::cerr << "Usage: " << av[0] << " Input" << av[1] << " Output\n";
     return EXIT_FAILURE;
     }
 
  typedef unsigned char PixelType;
  // using itkOrientedImage to make sure direction cosines 
  // are taken into account! (itkImage will not do that)
  typedef itk::OrientedImage<PixelType, 3> myImage;
  myImage::Pointer imagefixed;
  myImage::IndexType pixelIndex;

  typedef itk::OrientedImage<float, 3> ImageType; 
  ImageType::PointType point;

  

  // Read the image in MINC2.0 format
  std::cout << "Reading MINC2.0 Format Image: " << av[1] << std::endl;
  typedef itk::ImageFileReader<myImage>  ReaderType;
  ReaderType::Pointer reader = ReaderType::New();
  reader->SetFileName( av[1] );
  
  typedef itk::MINC2ImageIO        ImageIOType;
  ImageIOType::Pointer minc2ImageIO = ImageIOType::New();

  reader->SetImageIO( minc2ImageIO );
  try
    {
    reader->Update();
    }
  catch (itk::ExceptionObject & e)
    {
    std::cerr << "exception in file reader " << std::endl;
    std::cerr << e.GetDescription() << std::endl;
    std::cerr << e.GetLocation() << std::endl;
    return EXIT_FAILURE;
    }

  std::cout << "Image Origin Rotated!" << std::endl;
  std::cout <<reader->GetOutput()->GetOrigin()  << std::endl;
  std::cout << "Image Spacing" << std::endl;
  std::cout <<reader->GetOutput()->GetSpacing()  << std::endl;
  std::cout << "Image Dimensions" << std::endl;
  std::cout <<reader->GetOutput()->GetImageDimension()  << std::endl;
  std::cout << "Direction Cosines" << std::endl;
  std::cout <<reader->GetOutput()->GetDirection()  << std::endl;

  std::cout << "Converting random index to world coordinates" << std::endl;
  pixelIndex[0] = 13; pixelIndex[1] = 33; pixelIndex[2] = 63;
  reader->GetOutput()->TransformIndexToPhysicalPoint(pixelIndex, point);
  std::cout << "Index " << pixelIndex[0] << " " << pixelIndex[1] << " " << pixelIndex[2] << std::endl;
  std::cout << "Point " << point[0] << " " << point[1] << " " << point[2] << std::endl;
 
  // Rewrite the image in MINC2.0 format
  std::cout << "Writing in MINC2.0 Format: " << av[2] << std::endl;
  typedef itk::ImageFileWriter< myImage >  WriterType;
  WriterType::Pointer writer = WriterType::New();
  writer->SetFileName( av[2] );
  writer->SetInput( reader->GetOutput() );
  writer->SetImageIO( minc2ImageIO );

  try
    {
    writer->Update();
    }
  catch (itk::ExceptionObject & e)
    {
    std::cerr << "exception in file writer " << std::endl;
    std::cerr << e.GetDescription() << std::endl;
    std::cerr << e.GetLocation() << std::endl;
    return EXIT_FAILURE;
    }

  

  return EXIT_SUCCESS;

}

That code was written for ITKv3, which was then current version in 2006. If you need a one-off conversion process, you could consider compiling ITKv3 and then the mentioned code against it. Otherwise consider following ITKv4 and ITKv5 migration guides. Note: v4 migration guide link is currently broken.

1 Like

Recent versions of ITK come with MINC2 support by default. In C++:

https://itk.org/ITKExamples/src/IO/ImageBase/ConvertFileFormats/Documentation.html

In Python:

import itk

image = itk.imread(file_name)
itk.imwrite(image, file_name + '.mnc')

But 3D Slicer likely now also has MINC support via ITK.

2 Likes