[Insight-users] Reading Analyse 7.5 image

Luis Ibanez luis.ibanez at kitware.com
Thu Mar 25 18:23:23 EDT 2010


Hi Chaitanya,

Your approach to reading and writing the image seems
to be unnecessarily complicated.

You don't need to create the image by hand before you
read it.

you simply need the following code:
(as shown in the ITK Software Guide)


#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkImage.h"


int main( int argc, char ** argv )
{
  if( argc < 3 )
    {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0] << " inputImageFile  outputImageFile " << std::endl;
    return EXIT_FAILURE;
    }

  typedef short        PixelType;
  const   unsigned int Dimension = 2;
  typedef itk::Image< PixelType, Dimension >    ImageType;

  typedef itk::ImageFileReader< ImageType >  ReaderType;
  typedef itk::ImageFileWriter< ImageType >  WriterType;

  ReaderType::Pointer reader = ReaderType::New();
  WriterType::Pointer writer = WriterType::New();

  reader->SetFileName( argv[1] );
  writer->SetFileName( argv[2] );

  writer->SetInput( reader->GetOutput() );

  try
    {
    writer->Update();
    }
  catch( itk::ExceptionObject & err )
    {
    std::cerr << "ExceptionObject caught !" << std::endl;
    std::cerr << err << std::endl;
    return EXIT_FAILURE;
    }

  return EXIT_SUCCESS;
}


Please try this code above for reading your
image, and let us know if you still find any
problem.


     Thanks


          Luis


----------------------------------------
On Sat, Mar 20, 2010 at 8:29 PM, chaitanya chitale
<chitale at cse.ohio-state.edu> wrote:
> Hi,
>
> I am new to ITK and I am trying to read the Analyse 7.5 image.
> Its an image created by imageJ and is a 16 bit grayscale image of the
> dimension 512*512*24
>
> I am getting a segmentation fault while doing reader1->update().
> The code is a sample code which reads an image and writes it back. I am
> using iterators because they will help manipulate the image for other
> purposes.
>
> Heres the code.
>
> #include "itkImage.h"
> #include "itkImageRegionConstIterator.h"
> #include "itkImageRegionIterator.h"
> #include "itkImageFileReader.h"
> #include "itkImageFileWriter.h"
> #include "itkRGBPixel.h"
> #include "stdio.h"
> using namespace std;
>
> int main()
> {
>  const unsigned char Dimension = 3;
>
>  typedef signed short PixelType;
>  typedef itk::Image< PixelType, Dimension > ImageType;
>
>  typedef itk::ImageRegionConstIterator< ImageType > ConstIteratorType;
>  typedef itk::ImageRegionIterator< ImageType>       IteratorType;
>
>  typedef itk::ImageFileReader< ImageType > ReaderType;
>  typedef itk::ImageFileWriter< ImageType > WriterType;
>
>  ImageType::RegionType inputRegion;
>
>  ImageType::RegionType::IndexType inputStart;
>  // Imput image size
>  ImageType::RegionType::SizeType  size_ip;
>  // Output image size
>  ImageType::RegionType::SizeType  size_op;
>
>  inputStart[0] = 0;
>  inputStart[1] = 0;
>  inputStart[2] = 0;
>
>  size_ip[0]  = 512;
>  size_ip[1]  = 512;
>  size_ip[2]  = 24;
>
>  inputRegion.SetSize( size_ip );
>  inputRegion.SetIndex( inputStart );
>
>  ImageType::RegionType outputRegion;
>
>  ImageType::RegionType::IndexType outputStart;
>
>  outputStart[0] = 0;
>  outputStart[1] = 0;
>  outputStart[2] = 0;
>
>  outputRegion.SetSize( size_ip );
>  outputRegion.SetIndex( outputStart );
>
>
>  ReaderType::Pointer reader1 = ReaderType::New();
>  reader1->SetFileName( "psf-green-bead-35nm.img" );
>  try
>    {
>    reader1->Update();
>    }
>  catch ( itk::ExceptionObject &err)
>    {
>    std::cerr << "ExceptionObject caught !" << std::endl;
>    std::cerr << err << std::endl;
>    return -1;
>    }
>
>
>  ImageType::Pointer outputImage = ImageType::New();
>  outputImage->SetRegions( outputRegion );
>  const ImageType::SpacingType& spacing =
> reader1->GetOutput()->GetSpacing();
>  const ImageType::PointType& inputOrigin =
> reader1->GetOutput()->GetOrigin();
>  double   outputOrigin[ Dimension ];
>
>  for(unsigned int i=0; i< Dimension; i++)
>    {
>    outputOrigin[i] = inputOrigin[i] + spacing[i] * inputStart[i];
>    }
>
>  outputImage->SetSpacing( spacing );
>  outputImage->SetOrigin(  outputOrigin );
>  outputImage->Allocate();
>
>  ConstIteratorType inputIt1(   reader1->GetOutput(), inputRegion  );
>  IteratorType outputIt( outputImage,outputRegion );
>
>  inputIt1.GoToBegin();
>  outputIt.GoToBegin();
>
>  while( !inputIt1.IsAtEnd() )
>  {
>        outputIt.Set(inputIt1.Get());
>
>    ++inputIt1;
>        ++outputIt;
>  }
>
>  WriterType::Pointer writer = WriterType::New();
>  writer->SetFileName("copy.img");
>  writer->SetInput( outputImage );
>
>  try
>    {
>    writer->Update();
>    }
>  catch ( itk::ExceptionObject &err)
>    {
>    std::cerr << "ExceptionObject caught !" << std::endl;
>    std::cerr << err << std::endl;
>    return -1;
>    }
>
>
> }
>
>
> Can you please help?
> Thank you in advance.
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.html
>
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-users
>
>


More information about the Insight-users mailing list