[Insight-users] read and write image series

alex Dowson alexdowson at hotmail.com
Wed Sep 19 00:15:24 EDT 2012


Hi Shirani

 
First of all you need to learn C++ properly and Read the ITK software user guide can be download from here. Remember one advice for free “Half effect doesn’t produce Half Result , It produce nothing.”  

http://www.itk.org/ItkSoftwareGuide.pdf 


Also in your sample it doesnot make any sense what you trying to do because since you reading .mhd file there is no need for ImageSeriesReader because 
when you load .mhd file it will load the associate .raw file with sequence given in .mhd file. Also in your e-mail your not telling peoples what you want to do 
in that case no one can help you . 


So Correct you code like as given. It will load your 3D RGB image and produce the slice as color png images. Is that what you loooking for ?


#include "itkImage.h"
#include "itkRGBPixel.h"
#include "itkImageFileReader.h"
#include "itkImageSeriesWriter.h"
#include "itkNumericSeriesFileNames.h"



typedef unsigned char                       PixelType;
const unsigned int   Dimension = 3;

typedef itk::RGBPixel<PixelType>           RGBPixelType;


typedef itk::Image< RGBPixelType, Dimension>  ImageType;

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


ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName( "D:\\To_SMK\\VHPAbdomen%03d.mhd" );




const unsigned int first = atoi( argv[1] );
const unsigned int last  = atoi( argv[2] );
const char * outputFilename = argv[3];

typedef itk::NumericSeriesFileNames    NameGeneratorType;
NameGeneratorType::Pointer nameGenerator = NameGeneratorType::New();
nameGenerator->SetSeriesFormat( "vwe%03d.png" );
nameGenerator->SetStartIndex( first );
nameGenerator->SetEndIndex( last );
nameGenerator->SetIncrementIndex( 1 );


typedef itk::Image< RGBPixelType, 2 >     Image2DType;

typedef itk::ImageSeriesWriter< ImageType, Image2DType > SeriesWriterType;
SeriesWriterType::Pointer seriesWriter = SeriesWriterType::New();
seriesWriter->SetInput( reader->GetOutput() );
seriesWriter->SetFileNames( nameGenerator->GetFileNames() );


try
{ 
    seriesWriter->Update();
}
catch( itk::ExceptionObject & excp )
{
    std::cerr << "Error reading the series " << std::endl;
    std::cerr << excp << std::endl;
}



Alex






From: Bill Lorensen 
Sent: Wednesday, September 19, 2012 4:55 AM
To: shirani kannangara 
Cc: insight-users at itk.org 
Subject: Re: [Insight-users] read and write image series

Change this line: 
  if( argc <4 );
to
  if( argc <4 )



On Tue, Sep 18, 2012 at 6:23 AM, shirani kannangara <mangalika673 at gmail.com> wrote:


  Hi vtk/itk users

  I am constructing a program to read and write medical image(3D) data set , 
  image data(raw data -VHPAbdomen.mhd)in ITk version 4.2 ,CMake2.8.9. IT built with 2 warnings.
  itk codes are as follows.But it does not display output properly. It is as follows.My raw data file is attached herewith.I used ImageSeriesReadWrite.cxx
  #include "itkImage.h"
  #include "itkImageSeriesReader.h"
  #include "itkImageFileWriter.h"
  #include "itkNumericSeriesFileNames.h"
  #include "itkPNGImageIO.h"




  codes

     #include <iostream>
          #include <string>
     using namespace std;
      
  int main(  int argc, char **argv[])

  {
      if( argc <4 );
      
    {std::cout << "Usage: " << std::endl;
      std::cout<<argv[0] <<  " firstSliceValue lastSliceValue  outputImageFile " << std::endl;
     return EXIT_FAILURE;
      } 
     
    //std::cout<<"sliceValue:"<<std::endl;
    typedef unsigned char                       PixelType;
    const unsigned int   Dimension = 3;

    typedef itk::Image< PixelType, Dimension>  ImageType;

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


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

    const unsigned int first = 1;
    const unsigned int last  = 450;
    //char * outputFilename = argv[3];
     char**outputFilename=  argv[3];
   
    typedef itk::NumericSeriesFileNames NameGeneratorType;

    NameGeneratorType::Pointer nameGenerator = NameGeneratorType::New();
   
    nameGenerator->SetSeriesFormat( "D:\\To_SMK\\VHPAbdomen%03d.mhd" );
    nameGenerator->SetStartIndex( first );
    nameGenerator->SetEndIndex( last);
    nameGenerator->SetIncrementIndex(1);

    reader->SetImageIO( itk::PNGImageIO::New() );

    reader->SetFileNames( nameGenerator->GetFileNames()  );
    writer->SetInput( reader->GetOutput() );
    writer->SetFileName("test.mhd");
  try
      {
      writer->Update();
      }
    catch( itk::ExceptionObject & err )
      {
      std::cerr << "ExceptionObject caught !" << std::endl;
      std::cerr << err << std::endl;
      return EXIT_FAILURE;
      };
   
    return 0;

    }
   
  OUTPUT 

  D:\image\Debug>ImageReadWrite.exe
  Usage:
  0022D9E0 firstSliceValue lastSliceValue  outputImageFile

  D:\image\Debug>ImageReadWrite.exe(1 450 test.mhd)
  Usage:
  00E71324 firstSliceValue lastSliceValue  outputImageFile

  D:\image\Debug>ImageReadWrite.exe(1,450,test.mhd)
  Usage:
  0115131C firstSliceValue lastSliceValue  outputImageFile

  D:\image\Debug>

  PL help me to get it corrected
  Thankas 

  Shirani


  _____________________________________
  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.php

  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






-- 
Unpaid intern in BillsBasement at noware dot com




--------------------------------------------------------------------------------
_____________________________________
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.php

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20120919/6c483022/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/png
Size: 1041 bytes
Desc: not available
URL: <http://www.itk.org/pipermail/insight-users/attachments/20120919/6c483022/attachment-0001.png>


More information about the Insight-users mailing list