int main(int argc, char* argv[])
{
if (argc < 2)
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0] << " DicomDirectory [outputFileName seriesName]"
<< std::endl;
return EXIT_FAILURE;
}
typedef signed short PixelType;
const unsigned int Dimension = 3;
NamesGeneratorType::Pointer nameGenerator = NamesGeneratorType::New();
nameGenerator->SetUseSeriesDetails(true);
nameGenerator->AddSeriesRestriction("0008|0021");
nameGenerator->SetDirectory(argv[1]);
try
{
gdcm::FileList *flist = nameGenerator->GetSeriesHelper()->GetFirstSingleSerieUIDFileSet();
if (flist)
{
std::cout << "The directory: ";
std::cout << argv[1] << std::endl;
std::cout << "Contains the following DICOM Series: ";
std::cout << std::endl;
}
else
{
std::cout << "No DICOMs in: " << argv[1] << std::endl;
return EXIT_SUCCESS;
}
typedef std::vector< std::string > SeriesIdContainer;
const SeriesIdContainer & seriesUID = nameGenerator->GetSeriesUIDs();
SeriesIdContainer::const_iterator seriesItr = seriesUID.begin();
SeriesIdContainer::const_iterator seriesEnd = seriesUID.end();
while (seriesItr != seriesEnd)
{
std::cout << seriesItr->c_str() << std::endl;
++seriesItr;
}
seriesItr = seriesUID.begin();
while (seriesItr != seriesUID.end())
{
std::string seriesIdentifier;
if (argc > 3)
{
seriesIdentifier = argv[3];
seriesItr = seriesUID.end();
}
else
{
seriesIdentifier = seriesItr->c_str();
seriesItr++;
}
std::cout << "\nReading: ";
std::cout << seriesIdentifier << std::endl;
typedef std::vector< std::string > FileNamesContainer;
FileNamesContainer fileNames;
fileNames = nameGenerator->GetFileNames(seriesIdentifier);
ReaderType::Pointer reader = ReaderType::New();
ImageIOType::Pointer dicomIO = ImageIOType::New();
reader->SetImageIO(dicomIO);
reader->SetFileNames(fileNames);
WriterType::Pointer writer = WriterType::New();
std::string outFileName;
if (argc > 3)
{
outFileName = argv[2];
}
else
{
outFileName = argv[1] + std::string("/") + seriesIdentifier + ".nrrd";
}
writer->SetFileName(outFileName);
writer->UseCompressionOn();
writer->SetInput(reader->GetOutput());
std::cout << "Writing: " << outFileName << std::endl;
try
{
writer->Update();
}
{
std::cout << ex << std::endl;
continue;
}
}
}
{
std::cout << ex << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}