[Insight-users] read rgb values

alex Dowson alexdowson at hotmail.com
Tue Sep 18 03:03:47 EDT 2012


Hi

Please make CC to itk user list so other can see the conversion and get help from it.

I seen your had some mistakes.

1) In Series name gernerator is wrong
nameGenerator->SetSeriesFormat("D:\\To_SMK\\VHPAbdomen.mhd");
should be something like
nameGenerator->SetSeriesFormat("D:\\To_SMK\\VHPAbdomen%03d.mhd");
that it will start format the image. Suppose number 12 image name will be VHPAbdomen012.mhd

2)   writer->SetFileName( "test.png" );  You can’t save the 3D image as png. You have to save like meta image (i.e. .mha)

3)   nameGenerator->SetSeriesFormat( "test.png" ); also simlar to nameGenerator->SetSeriesFormat( "vwe%03d.png" );



For more details see the attached example from ITK.  Pls check all this and let us know the result.


alex







 

From: shirani kannangara 
Sent: Tuesday, September 18, 2012 11:54 AM
To: alexdowson at hotmail.com 
Subject: Re: read rgb values

Dear Alex
1. My input file name is VHPabdomen.mhd(raw data file).It consist of  series of 2D image slices of the abdomen.Image dimensions are 675,401,450.spacing0.333.

2I wrote codes to read  rgb values   of sreries of images.

Thanks Shirani

  Dear Alex
Thanks a lot you for  showing me the error and now it runs with zero errors (but there are three warnings).So far I couldn't read RGB values.Is there anything missing in my codes.Pl help me.
codes are given below.
Thanks
Shirani





//  RGB images are commonly used for representing data acquired from cryogenic
//  sections, optical microscopy and endoscopy. This example illustrates how to
//  read RGB color images from a set of files containing individual 2D slices
//  in order to compose a 3D color dataset. Then save it into a single 3D file,
//  and finally save it again as a set of 2D slices with other names.


#include "itkRGBPixel.h"
#include "itkImage.h"
#include "itkImageSeriesReader.h"
#include "itkImageSeriesWriter.h"
#include "itkNumericSeriesFileNames.h"
#include "itkPNGImageIO.h"


#include <string>


using namespace std;



// Software Guide : EndCodeSnippet


int main( int argc, char * argv[] )




{
char test;
  // Verify the number of parameters in the command line
  if( argc < 4 )
    {
    std::cout << "Usage: " << std::endl;
    std::cout << argv[0] << "first last  outputRGBImageFile " << std::endl;
    return EXIT_FAILURE;
    }




  
  // The \doxygen{RGBPixel} class is templated over the type used to
  // represent each one of the Red, Green and Blue components. A typical
  // instantiation of the RGB image class might be as follows.
    
  typedef itk::RGBPixel< unsigned char >        PixelType;
  const unsigned int Dimension = 3;


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

  // The image type is used as a template parameter to instantiate
  // the series reader and the volumetric writer.

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


  SeriesReaderType::Pointer seriesReader = SeriesReaderType::New();
  WriterType::Pointer       writer       = WriterType::New();
  

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


  
  // We use a NumericSeriesFileNames class in order to generate the filenames of
  // the slices to be read. Later on in this example we will reuse this object in
  // order to generate the filenames of the slices to be written.
   
  typedef itk::NumericSeriesFileNames NameGeneratorType;


  NameGeneratorType::Pointer nameGenerator = NameGeneratorType::New();
  
  nameGenerator->SetStartIndex( first );
  nameGenerator->SetEndIndex( last );
  nameGenerator->SetIncrementIndex( 0.333 );


//nameGenerator->SetSeriesFormat( "test.png" );
  nameGenerator->SetSeriesFormat("D:\\To_SMK\\VHPAbdomen.mhd");
  //  The ImageIO object that actually performs the read process
  //  is now connected to the ImageSeriesReader.
  
  seriesReader->SetImageIO( itk::PNGImageIO::New() );
  
  //  The filenames of the input slices are taken from the names generator and
  //  passed to the series reader.
  
  seriesReader->SetFileNames( nameGenerator->GetFileNames()  );
  
  // The name of the volumetric output image is passed to the image writer, and
  // we connect the output of the series reader to the input of the volumetric
  // writer.
  //
  
  writer->SetFileName( "test.png" );


  writer->SetInput( seriesReader->GetOutput() );
  
  //  Finally, execution of the pipeline can be triggered by invoking the
  //  Update() method in the volumetric writer. This, of course, is done from
  //  inside a try/catch block.

  try
    {
    writer->Update();
    }
  catch( itk::ExceptionObject & excp )
    {
    std::cerr << "Error reading the series " << std::endl;
    std::cerr << excp << std::endl;
    }
  // We now proceed to save the same volumetric dataset as a set of slices. This
  // is done only to illustrate the process for saving a volume as a series of 2D
  // individual datasets. The type of the series writer must be instantiated
  // taking into account that the input file is a 3D volume and the output files
  // are 2D images.  Additionally, the output of the series reader is connected
  // as input to the series writer.
  
  typedef itk::Image< PixelType, 2 >     Image2DType;


  typedef itk::ImageSeriesWriter< ImageType, Image2DType > SeriesWriterType;


  SeriesWriterType::Pointer seriesWriter = SeriesWriterType::New();


  seriesWriter->SetInput( seriesReader->GetOutput() );
  
  // We now reuse the filenames generator in order to produce the list of
  // filenames for the output series. In this case we just need to modify the
  // format of the filenames generator. Then, we pass the list of output
  // filenames to the series writer.
  
  nameGenerator->SetSeriesFormat( "test.png" );


  seriesWriter->SetFileNames( nameGenerator->GetFileNames() );
  
  // Finally we trigger the execution of the series writer from inside a
  // try/catch block.
  
  try
    {
    seriesWriter->Update();
    }
  catch( itk::ExceptionObject & excp )
    {
    std::cerr << "Error reading the series " << std::endl;
    std::cerr << excp << std::endl;
    }
  
  //  You may have noticed that apart from the declaration of the
  //  \code{PixelType} there is nothing in this code that is specific for RGB
  //  images. All the actions required to support color images are implemented
  //  internally in the \doxygen{ImageIO} objects.
  
  return EXIT_SUCCESS;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20120918/c81eff37/attachment-0001.htm>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: ImageSeriesReadWrite.cxx
URL: <http://www.itk.org/pipermail/insight-users/attachments/20120918/c81eff37/attachment-0001.txt>


More information about the Insight-users mailing list