]> Creatis software - cpPlugins.git/commitdiff
...
authorLeonardo Florez-Valencia <florez-l@javeriana.edu.co>
Wed, 9 Sep 2015 16:10:08 +0000 (18:10 +0200)
committerLeonardo Florez-Valencia <florez-l@javeriana.edu.co>
Wed, 9 Sep 2015 16:10:08 +0000 (18:10 +0200)
appli/examples/CMakeLists.txt
appli/examples/example_ExtractDICOMSeries.cxx [new file with mode: 0644]

index d2165feda3f4026c41c339c22a3487a331467a6b..97801d4928898e69bdeb9f3ce34186b2601bf57d 100644 (file)
@@ -29,6 +29,7 @@ ENDFOREACH(prog)
 
 SET(
   NOPLUGINS_EXAMPLES_PROGRAMS
+  example_ExtractDICOMSeries
   example_ImageGaussianModelEstimator
   example_ReadQuadEdgeMeshWithoutPlugins
   example_RenderQuadEdgeMeshWithoutPlugins
diff --git a/appli/examples/example_ExtractDICOMSeries.cxx b/appli/examples/example_ExtractDICOMSeries.cxx
new file mode 100644 (file)
index 0000000..a885c9b
--- /dev/null
@@ -0,0 +1,94 @@
+#include <cstdlib>
+#include <iostream>
+#include <string>
+#include <vector>
+
+#include <itkImage.h>
+#include <itkGDCMImageIO.h>
+#include <itkGDCMSeriesFileNames.h>
+#include <itkImageSeriesReader.h>
+#include <itkImageFileWriter.h>
+
+// -------------------------------------------------------------------------
+typedef short      TPixel;
+const unsigned int Dim = 3;
+typedef itk::Image< TPixel, Dim > TImage;
+
+// -------------------------------------------------------------------------
+int main( int argc, char* argv[] )
+{
+  if( argc < 3 )
+  {
+    std::cerr
+      << "Usage: " << std::endl
+      << argv[ 0 ] << " dicom_dir output_extension"
+      << std::endl;
+    return( 1 );
+
+  } // fi
+  std::string dicom_dir = argv[ 1 ];
+  std::string output_extension = argv[ 2 ];
+
+  typedef itk::GDCMSeriesFileNames TNameGenerator;
+  TNameGenerator::Pointer name_gen = TNameGenerator::New( );
+  name_gen->SetUseSeriesDetails( true );
+  name_gen->AddSeriesRestriction( "0008|0021" );
+  name_gen->SetDirectory( dicom_dir );
+  try
+  {
+    typedef std::vector< std::string > TSeriesIdContainer;
+    const TSeriesIdContainer& seriesUID = name_gen->GetSeriesUIDs( );
+
+    TSeriesIdContainer::const_iterator serieIt = seriesUID.begin( );
+    for( ; serieIt != seriesUID.end( ); ++serieIt )
+    {
+      // Get series identifier
+      std::string serieId = serieIt->c_str( );
+      std::cout
+        << "\nReading: "
+        << serieId << std::endl;
+
+      // Get filenames for actual serie
+      typedef std::vector< std::string > TFileNamesContainer;
+      TFileNamesContainer filenames;
+      filenames = name_gen->GetFileNames( serieId );
+
+      // Read image
+      itk::ImageSeriesReader< TImage >::Pointer reader =
+        itk::ImageSeriesReader< TImage >::New( );
+      itk::GDCMImageIO::Pointer dicomIO = itk::GDCMImageIO::New( );
+      reader->SetImageIO( dicomIO );
+      reader->SetFileNames( filenames );
+
+      // Write image
+      std::string serie_filename;
+      serie_filename = dicom_dir + "/" + serieId + "." + output_extension;
+
+      itk::ImageFileWriter< TImage >::Pointer writer =
+        itk::ImageFileWriter< TImage >::New( );
+      writer->SetFileName( serie_filename );
+      writer->UseCompressionOff( );
+      writer->SetInput( reader->GetOutput( ) );
+      std::cout << "Writing: " << serie_filename << std::endl;
+      try
+      {
+        writer->Update( );
+      }
+      catch( itk::ExceptionObject& err )
+      {
+        std::cerr << " Error caught: " << err << std::endl;
+
+      } // yrt
+
+    } // rof
+  }
+  catch( itk::ExceptionObject& err )
+  {
+    std::cerr << "Error caught: " << std::endl << err << std::endl;
+    return( 1 );
+
+  } // yrt
+  return( 0 );
+}
+
+// eof - $RCSfile$