[Insight-users] Reading and Writing Images-Image Type Conversion

Oleksandr Dzyubak adzyubak at gmail.com
Tue Aug 18 11:28:33 EDT 2009


Hi Gideon,

I simplified your code a bit just to make sure the read/write functions 
work (see simplified version below).
I tested the simplified version and it works. It writes gray image though.
You find the output image in the directory where you run the code.
If you really need RGB (and/or alpha), see the detailed read/write jpg/tiff
ITK documentation (plus jpg/tiff wiki).

Regards,

Alex

******** Start code ********


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


int main( int argc, char ** argv )
{
  // Verify the number of parameters in the command line
  if( argc < 3 )
    {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0] << " inputImageFile  outputImageFile " << 
std::endl;
    return EXIT_FAILURE;
    }

//  typedef short      PixelType;
  typedef unsigned char      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();

// const char * inputFilename  = argv[1];
// const char * outputFilename = argv[2];

// reader->SetFileName( "original.jpg"  );
// writer->SetFileName( "original.tiff" );

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;
}

********End code ********


Gideon Nimako wrote:
> Dear all
>
>
> I am converting an image from jpg to tiff and cant seem to find the 
> converted image in my directory. Where the converted image go. When 
> you WRITE an image where does it go.
>
> Please help me.
>
> Its very urgent
>
> The Code is as below
> .....................................................................................................................
>
> #include "itkImageFileReader.h"
> #include "itkImageFileWriter.h"
> #include "itkImage.h"
>
>
> int main( int argc, char ** argv )
> {
>   // Verify the number of parameters in the command line
>   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();
>
>  const char * inputFilename  = argv[1];
>   const char * outputFilename = argv[2];
>
> reader->SetFileName( "original.jpg"  );
>   writer->SetFileName( "original.tiff" );
>
> 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;
> }
>
>
> ------------------------------------------------------------------------
>
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.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