[Insight-users] DICOM Reader Rescale Writer
Ignacio García Fenoll
igfenoll at gmail.com
Mon Mar 31 11:56:12 EDT 2008
Good evening, insight-users!!
I am trying to get a program that firstly read DICOM series from a
directory, later extract all the images without considering headings and at
last copy the image series (all the images png for example) into a new
directory. When I read the errors, the compilator shows that there is an
incompatibility of types, but I am not able to find what is the matter with
that. Could you help me?
This is my code:
#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 "itkGDCMSeriesFileNames.h"
#include "itkImageSeriesReader.h"
#include "itkImageSeriesWriter.h"
#include <list>
#include <fstream>
#include <vector>
#include <itksys/SystemTools.hxx>
int main( int argc, char* argv[] )
{
if( argc < 3 )
{
std::cerr << "Usage: " << argv[0] <<
" DicomDirectory OutputPngSeriesDirectory" << std::endl;
return EXIT_FAILURE;
}
typedef signed short PixelType;
const unsigned int Dimension = 3;
typedef itk::Image< PixelType, Dimension > ImageType;
typedef itk::ImageSeriesReader< ImageType > ReaderType;
typedef itk::GDCMImageIO ImageIOType;
typedef itk::GDCMSeriesFileNames NamesGeneratorType;
ImageIOType::Pointer gdcmIO = ImageIOType::New();
NamesGeneratorType::Pointer namesGenerator = NamesGeneratorType::New();
namesGenerator->SetInputDirectory( argv[1] );
const ReaderType::FileNamesContainer & filenames =
namesGenerator->GetInputFileNames();
unsigned int numberOfFilenames = filenames.size();
std::cout << numberOfFilenames << std::endl;
for(unsigned int fni = 0; fni<numberOfFilenames; fni++)
{
std::cout << "filename # " << fni << " = ";
std::cout << filenames[fni] << std::endl;
}
ReaderType::Pointer reader = ReaderType::New();
reader->SetImageIO( gdcmIO );
reader->SetFileNames( filenames );
try
{
reader->Update();
}
catch (itk::ExceptionObject &excp)
{
std::cerr << "Exception thrown while writing the image" << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
typedef signed short InputPixelType;
const unsigned int InputDimension = 2;
typedef itk::Image< InputPixelType, InputDimension > InputImageType;
typedef itk::ImageFileReader< InputImageType > ReaderType2;
ReaderType::Pointer reader2 = ReaderType2::New();
reader2->SetFileName(reader->GetOutput() );
typedef itk::GDCMImageIO ImageIOType;
ImageIOType::Pointer gdcmImageIO = ImageIOType::New();
reader2->SetImageIO( gdcmImageIO );
try
{
reader2->Update();
}
catch (itk::ExceptionObject & e)
{
std::cerr << "exception in file reader " << std::endl;
std::cerr << e << std::endl;
return EXIT_FAILURE;
}
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 );
typedef itk::ImageFileWriter< WriteImageType > Writer2Type;
Writer2Type::Pointer writer2 = Writer2Type::New();
writer2->SetFileName( argv[2] );
rescaler->SetInput( reader2->GetOutput() );
writer2->SetInput( rescaler->GetOutput() );
try
{
writer2->Update();
}
catch (itk::ExceptionObject & e)
{
std::cerr << "exception in file writer " << std::endl;
std::cerr << e << std::endl;
return EXIT_FAILURE;
}
const char * outputDirectory = argv[2];
itksys::SystemTools::MakeDirectory( outputDirectory );
typedef signed short OutputPixelType;
const unsigned int OutputDimension = 2;
typedef itk::Image< OutputPixelType, OutputDimension > Image2DType;
typedef itk::ImageSeriesWriter<
ImageType, Image2DType > SeriesWriterType;
SeriesWriterType::Pointer seriesWriter = SeriesWriterType::New();
seriesWriter->SetInput( reader2->GetOutput() );
seriesWriter->SetImageIO( gdcmIO );
namesGenerator->SetOutputDirectory( outputDirectory );
seriesWriter->SetFileNames( namesGenerator->GetOutputFileNames() );
seriesWriter->SetMetaDataDictionaryArray(
reader2->GetMetaDataDictionaryArray() );
try
{
seriesWriter->Update();
}
catch( itk::ExceptionObject & excp )
{
std::cerr << "Exception thrown while writing the series " << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Thank you very much!! I would be very grateful if you could help me a bit!!
Regards!
--
Ignacio García Fenoll
Grupo de Imágenes Médicas
Departamento de Teoría de la Señal y Comunicaciones
Universidad de Sevilla
e-mail: igfenoll at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20080331/1968c980/attachment.htm>
More information about the Insight-users
mailing list