#include "itkImage.h" #include "itkImageFileReader.h" #include "itkImageFileWriter.h" #include "itkMultipleUnlabeledImagesToLabeledImageFilter.h" int main(int ac, char *av[]) { if(ac < 4) { std::cerr << "Missing Parameters!!!" << std::endl; std::cerr << "Usage: " << av[0]; std::cerr << " InputImageList OutputImage IntensityThreshold LowerOrUpper" << std::endl; return EXIT_FAILURE; } // The image dimension. Eventhough imageDimension is set to 3, a 2D image can still be used. const unsigned int imageDimension = 3; // Image typedefs. typedef unsigned char PixelType; typedef itk::Image ImageType; // Initialize the image reader. typedef itk::ImageFileReader ImageReaderType; // Initialize the image writer. typedef itk::ImageFileWriter FileWriterType; FileWriterType::Pointer fileWriter = FileWriterType::New(); // Initialize a pointer to the filter. typedef itk::MultipleUnlabeledImagesToLabeledImageFilter MultipleUnlabeledImagesToLabeledImageFilterType; MultipleUnlabeledImagesToLabeledImageFilterType::Pointer labelsFilter = MultipleUnlabeledImagesToLabeledImageFilterType::New(); // Read the input images in and store them in a std::vector std::vector inputImageList; std::ifstream inFile(av[1]); std::string filename; while(true) { if(inFile.eof()) break; inFile >> filename; ImageReaderType::Pointer imageReader = ImageReaderType::New(); imageReader->SetFileName(filename.c_str()); imageReader->Update(); inputImageList.push_back(imageReader->GetOutput()); } labelsFilter->SetInputImageList(inputImageList); typedef std::vector * PixelVectorType; PixelVectorType thresholds = new std::vector; // for storing the thresholds for each image PixelType thresholdValue = std::atoi(av[3]); // this value will be used for each of the thresholds // Determine whether to use the threshold value as a lower or upper threshold bool useLowerThreshold; if(std::string(av[4]) == std::string("lower")) { useLowerThreshold = true; } else if(std::string(av[4]) == std::string("upper")) { useLowerThreshold = false; } else { itk::ExceptionObject exceptionObject; exceptionObject.SetDescription("Use lower or upper as input for argument 5!\n"); throw exceptionObject; } // Fill the threshold vector with the threshold value. // Note: If different threshold values are to be used, they // must be set differently for(unsigned int i = 0; i < inputImageList.size(); i++) thresholds->push_back(thresholdValue); // Initialize parameters for the filter labelsFilter->SetIntensityThresholds(thresholds); labelsFilter->SetUseLowerThreshold(useLowerThreshold); labelsFilter->SetInputImageList(inputImageList); try { labelsFilter->Update(); } catch(itk::ExceptionObject & e) { std::cerr << "Exception detected: " << e.GetDescription(); return EXIT_FAILURE; } // Print the member variables of the filter. std::cerr << labelsFilter << std::endl; // Save the output image to disk. fileWriter->SetInput(labelsFilter->GetOutput()); fileWriter->SetFileName(av[2]); fileWriter->Update(); return EXIT_SUCCESS; }