[Insight-users] Problem with DicomImageReadWrite.cxx
edoardo.belletti at alice.it
edoardo.belletti at alice.it
Thu Mar 4 17:31:48 EST 2010
Hi
I have a problem with the example: Examples/IO/DicomImageReadWrite.cxx of the ItkSoftwareGuide
I don't understand why when I run it (with this command ./DicomImageReadWrite carotid.dcm out1.dcm out2.dcm out3.dcm) with a dicom image as input (carotid.dcm) my output is that:
exception in file writer [1]
itk::ExceptionObject (0xa0c25a8)
Location: "virtual void itk::GDCMImageIO::Write(const void*)"
File: /usr/local/itk/InsightToolkit-3.16.0/Code/IO/itkGDCMImageIO.cxx
Line: 1827
Description: itk::ERROR: GDCMImageIO(0xa0c11a0): Cannot write the requested file:out1.dcm
Reason: Success
My code is:
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
#ifdef __BORLANDC__
#define ITK_LEAN_AND_MEAN
#endif
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkRescaleIntensityImageFilter.h"
#include "itkGDCMImageIO.h"
#include <list>
#include <fstream>
int main( int argc, char* argv[] )
{
// Verify the number of parameters in the command line
if( argc < 5 )
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0] << " DicomImage OutputDicomImage ";
std::cerr << " OutputImage RescaleDicomImage\n";
return EXIT_FAILURE;
}
// Declare the pixel type and image dimension, and use them for
// instantiating the image type to be read.
typedef signed short InputPixelType;
const unsigned int InputDimension = 2;
typedef itk::Image< InputPixelType, InputDimension > InputImageType;
// Instantiate the type of the reader, create one,
// and set the filename of the image to be read.
typedef itk::ImageFileReader< InputImageType > ReaderType;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName( argv[1] );
// The GDCMImageIO object is constructed here and connected to
// the ImageFileReader.
typedef itk::GDCMImageIO ImageIOType;
ImageIOType::Pointer gdcmImageIO = ImageIOType::New();
reader->SetImageIO( gdcmImageIO );
// Trigger the reading process by invoking the Update() method.
try
{
reader->Update();
}
catch (itk::ExceptionObject & e)
{
std::cerr << "exception in file reader " << std::endl;
std::cerr << e << std::endl;
return EXIT_FAILURE;
}
// Instantiate an ImageFileWriter type.
typedef itk::ImageFileWriter< InputImageType > Writer1Type;
Writer1Type::Pointer writer1 = Writer1Type::New();
writer1->SetFileName( argv[2] );
writer1->SetInput( reader->GetOutput() );
// Set the proper image IO (GDCMImageIO) to the writer filter since the input
// DICOM dictionary is being passed along the writing process.
writer1->SetImageIO( gdcmImageIO );
// The writing process is triggered by invoking the Update() method.
try
{
writer1->Update();
}
catch (itk::ExceptionObject & e)
{
std::cerr << "exception in file writer [1] " << std::endl;
std::cerr << e << std::endl;
return EXIT_FAILURE;
}
// Rescale the image into a rescaled image one using the rescale intensity image filter.
// For this purpose we use a better suited pixel type: unsigned char instead of signed
// short. The minimum and maximum values of the output image are explicitly defined in
// the rescaling filter.
typedef unsigned char WritePixelType;
typedef itk::Image< WritePixelType, 2 > WriteImageType;
typedef itk::RescaleIntensityImageFilter< InputImageType, WriteImageType > RescaleFilterType;
RescaleFilterType::Pointer rescaler = RescaleFilterType::New();
rescaler->SetOutputMinimum( 0 );
rescaler->SetOutputMaximum( 255 );
// Create a second writer object that will save the rescaled image into a
// file. This time not in DICOM format.
typedef itk::ImageFileWriter< WriteImageType > Writer2Type;
Writer2Type::Pointer writer2 = Writer2Type::New();
writer2->SetFileName( argv[3] );
rescaler->SetInput( reader->GetOutput() );
writer2->SetInput( rescaler->GetOutput() );
// The writer can be executed by invoking the Update() method from inside a
// try/catch block.
try
{
writer2->Update();
}
catch (itk::ExceptionObject & e)
{
std::cerr << "exception in file writer [2]" << std::endl;
std::cerr << e << std::endl;
return EXIT_FAILURE;
}
// Save the same rescaled image into a file in DICOM format.
typedef itk::ImageFileWriter< WriteImageType > Writer3Type;
Writer3Type::Pointer writer3 = Writer3Type::New();
writer3->SetFileName( argv[4] );
writer3->SetInput( rescaler->GetOutput() );
// Explicitly set the proper image IO (GDCMImageIO).
writer3->UseInputMetaDataDictionaryOff ();
writer3->SetImageIO( gdcmImageIO );
// Trigger the execution of the DICOM writer by invoking the
// Update() method from inside a try/catch block.
try
{
writer3->Update();
}
catch (itk::ExceptionObject & e)
{
std::cerr << "Exception in file writer [3]" << std::endl;
std::cerr << e << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Thank you very much
Edoardo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20100304/de901192/attachment.htm>
More information about the Insight-users
mailing list