//////////////////////////////////////////////////////// // File: SegmentVentricle.cxx // Author: Patrick Marion // Data: 10-17-2007 // Include Image, ImageReader, ImageWriter classes #include "itkImage.h" #include "itkImageFileReader.h" #include "itkImageFileWriter.h" // Include filters for segmentation and slice extraction #include "itkConnectedThresholdImageFilter.h" #include "itkExtractImageFilter.h" // Use an enumeration to keep track of argument indices. enum args { InputImage = 1, OutputImage, SeedX, SeedY, SeedZ, LowerBound, UpperBound }; int main( int argc, char *argv[] ) { // Check command line arguments if( argc < 7 ) { std::cerr << "Missing Parameters " << std::endl; std::cerr << "Usage: " << argv[0]; std::cerr << " inputImage outputImage seedX seedY seedZ lower upper" << std::endl; return 1; } // Define some types typedef float InputPixelType; typedef float OutputPixelType; const unsigned int InputDimension = 3; typedef itk::Image< InputPixelType, InputDimension > InputImageType; typedef itk::Image< OutputPixelType, InputDimension > OutputImageType; typedef itk::ImageFileReader< InputImageType > ReaderType; typedef itk::ImageFileWriter< OutputImageType > WriterType; // Create the input reader and output writer ReaderType::Pointer reader = ReaderType::New(); WriterType::Pointer writer = WriterType::New(); // Set the filenames given as command line arguments reader->SetFileName( argv[InputImage] ); writer->SetFileName( argv[OutputImage] ); // Create the connected threshhold image filter. typedef itk::ConnectedThresholdImageFilter< InputImageType, OutputImageType > ConnectedFilterType; ConnectedFilterType::Pointer connectedThreshold = ConnectedFilterType::New(); // Connect inputs and outputs on the data flow pipeline. connectedThreshold->SetInput( reader->GetOutput() ); writer->SetInput( connectedThreshold->GetOutput() ); // Get the seed index from the command line args InputImageType::IndexType seedIndex; seedIndex[0] = atoi( argv[SeedX] ); seedIndex[1] = atoi( argv[SeedY] ); seedIndex[2] = atoi( argv[SeedZ] ); // Set the seed point for the filter connectedThreshold->SetSeed( seedIndex ); // Set the filter's upper and lower bounds as given on the command line. connectedThreshold->SetLower( atoi( argv[LowerBound] ) ); connectedThreshold->SetUpper( atoi( argv[UpperBound] ) ); // Set the filter's replace value to 255. connectedThreshold->SetReplaceValue( 255 ); // Run the filter and write segmented data to disk. try { writer->Update(); } catch( itk::ExceptionObject & excep ) { std::cerr << "Exception caught !" << std::endl; std::cerr << excep << std::endl; } /////////////////////////////////////////////////////////////// // Now lets extract a slice of the data // We will extract a slice that contains the seed point given as input. // The slice will be saved as a two dimensional PNG file. // First define some types. typedef itk::Image SliceOutputType; typedef itk::ExtractImageFilter ExtractType; typedef itk::ImageFileWriter SliceWriterType; typedef itk::ImageRegion RegionType; // Create a size object the size of one slice. // The size will have the same dimensions as the input data InputImageType::SizeType sliceSize; sliceSize = connectedThreshold->GetOutput()->GetLargestPossibleRegion().GetSize(); // Set the Z dimension to zero. // The zero causes the ExtractImageFilter to collapse the Z dimension // The index object defined below will determine which index to collapse on. sliceSize[2] = 0; // Create an index to the slice. // The index Z value will match the seed point's Z coordinate. // The index will have the same dimensions as the input data InputImageType::IndexType sliceIndex; sliceIndex[0] = 0; sliceIndex[1] = 0; sliceIndex[2] = atoi( argv[SeedZ] ); // Create a region with the defined index and size. RegionType region; region.SetIndex(sliceIndex); region.SetSize(sliceSize); // Create an ExtractImageFilter object ExtractType::Pointer extract = ExtractType::New(); // Set the extract filter's input to the output of the connectedThreshold filter. extract->SetInput( connectedThreshold->GetOutput() ); // Set the region of extraction extract->SetExtractionRegion(region); // Create a writer object for the extracted region SliceWriterType::Pointer sliceWriter = SliceWriterType::New(); sliceWriter->SetInput(extract->GetOutput()); // Set the filename for the slice writer to use. std::string sliceOutputFilename = std::string(argv[OutputImage]) + ".png"; sliceWriter->SetFileName( sliceOutputFilename.c_str()); // Now write the extracted region to a PNG file. try { sliceWriter->Update(); } catch( itk::ExceptionObject & excep ) { std::cerr << "Exception caught !" << std::endl; std::cerr << excep << std::endl; } return 0; }