[Insight-users] replacing the pixels of a DICOM image by a RAW FILE
Stéphane CALANDE
scalande at gmail.com
Fri Oct 24 06:10:44 EDT 2008
If someone is interrested, here is the code of a program that takes as input
:
> a repertory of a DICOM series
> a volume .MHD having the same resolution, spacing, origin, number of
slices,... than the series (containing )
> an output directory
an that gives, as output, a DICOM Series with the headers of the first DICOM
series but with the pixels of the volume MHD
(It's a modification of the code
"Examples/IO/DicomSeriesReadSeriesWrite.cxx")
++
Stéphane
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
#ifdef __BORLANDC__
#define ITK_LEAN_AND_MEAN
#endif
#include "itkGDCMImageIO.h"
#include "itkGDCMSeriesFileNames.h"
#include "itkImageSeriesReader.h"
#include "itkImageSeriesWriter.h"
#include <vector>
#include <itksys/SystemTools.hxx>
int main( int argc, char* argv[] )
{
if( argc < 4 )
{
std::cerr << "Usage: " << argv[0] <<
" DicomDirectory_having_good_headers
MHD_volume_to_convert_to_DICOMseies OutputDicomDirectory" << 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::ImageFileReader< ImageType > ReaderMHDType;
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 ); // ici on précise qu'on est en train de lire
une image au format DICOM
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;
}
// à partir d'ici, reader->getOutput = le volume chargé en mémoire
contenant toutes les slices
// ici on va loader le volume MHD
ReaderMHDType::Pointer readerMHD = ReaderMHDType::New();
readerMHD->SetFileName( argv[2] );
const char * outputDirectory = argv[3];
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( readerMHD->GetOutput() ); // ici on écrit le
volume du fichier MHD au lieu de l'original
seriesWriter->SetImageIO( gdcmIO ); // ici on précise qu'on va écrire une
image au format DICOM
namesGenerator->SetOutputDirectory( outputDirectory );
seriesWriter->SetFileNames( namesGenerator->GetOutputFileNames() );
// l'instruction qui suit est très importante, elle copie toutes les infos
provenant des headers DICOM (renseignements patients,...)
// et ici, on prend bien le bon reader (celui de la série DICOM qu'on a
chargé au tout début)
seriesWriter->SetMetaDataDictionaryArray(
reader->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;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20081024/2e7e5a68/attachment-0001.htm>
More information about the Insight-users
mailing list