/*========================================================================= Program: Insight Segmentation & Registration Toolkit Module: $RCSfile: seg_from_mask.cxx,v $ Language: C++ Date: $Date: 2010/01/23 05:28:41 $ Version: $Revision: 1.1 $ The program is used to cut the image area marked by the mask image file. =========================================================================*/ #if defined(_MSC_VER) #pragma warning ( disable : 4786 ) #endif // Software Guide : BeginLatex // // This example illustrates the use of the \doxygen{BSplineDeformableTransform} // class for performing registration of two $3D$ images and for the case of // multi-modality images. The image metric of choice in this case is the // \doxygen{MattesMutualInformationImageToImageMetric}. // // \index{itk::BSplineDeformableTransform} // \index{itk::BSplineDeformableTransform!DeformableRegistration} // \index{itk::LBFGSBOptimizer} // // // Software Guide : EndLatex #include "itkImage.h" #include "itkTimeProbesCollectorBase.h" #ifdef ITK_USE_REVIEW #include "itkMemoryProbesCollectorBase.h" #define itkProbesCreate() \ itk::TimeProbesCollectorBase chronometer; \ itk::MemoryProbesCollectorBase memorymeter #define itkProbesStart( text ) memorymeter.Start( text ); chronometer.Start( text ) #define itkProbesStop( text ) chronometer.Stop( text ); memorymeter.Stop( text ) #define itkProbesReport( stream ) chronometer.Report( stream ); memorymeter.Report( stream ) #else #define itkProbesCreate() \ itk::TimeProbesCollectorBase chronometer #define itkProbesStart( text ) chronometer.Start( text ) #define itkProbesStop( text ) chronometer.Stop( text ) #define itkProbesReport( stream ) chronometer.Report( stream ) #endif #include "itkImageFileReader.h" #include "itkImageFileWriter.h" #include "itkResampleImageFilter.h" #include "itkCastImageFilter.h" int main( int argc, char *argv[] ) { if( argc < 3 ) { std::cerr << "Missing Parameters " << std::endl; std::cerr << "Usage: " << argv[0]; std::cerr << " inputImage maskImage outputImage" < FixedImageType; typedef itk::Image< PixelType, ImageDimension > MovingImageType; // Software Guide : BeginLatex // // We instantiate now the type of the \code{BSplineDeformableTransform} using // as template parameters the type for coordinates representation, the // dimension of the space, and the order of the BSpline. // // \index{BSplineDeformableTransform!New} // \index{BSplineDeformableTransform!Instantiation} // // Software Guide : EndLatex // Software Guide : BeginCodeSnippet const unsigned int SpaceDimension = ImageDimension; const unsigned int SplineOrder = 3; typedef double CoordinateRepType; typedef itk::ImageFileReader< FixedImageType > FixedImageReaderType; FixedImageReaderType::Pointer fixedImageReader = FixedImageReaderType::New(); FixedImageReaderType::Pointer maskImageReader = FixedImageReaderType::New(); fixedImageReader->SetFileName( argv[1] ); maskImageReader->SetFileName( argv[2] ); FixedImageType::Pointer fixedImage = fixedImageReader->GetOutput(); FixedImageType::Pointer maskImage = maskImageReader->GetOutput(); fixedImageReader->Update(); maskImageReader->Update(); // Software Guide : BeginLatex // // Here we define the parameters of the BSplineDeformableTransform grid. We // arbitrarily decide to use a grid with $5 \times 5$ nodes within the image. // The reader should note that the BSpline computation requires a // finite support region ( 1 grid node at the lower borders and 2 // grid nodes at upper borders). Therefore in this example, we set // the grid size to be $8 \times 8$ and place the grid origin such that // grid node (1,1) coincides with the first pixel in the fixed image. // // \index{BSplineDeformableTransform} // // Software Guide : EndLatex typedef signed float OutputPixelType; typedef itk::Image< OutputPixelType, ImageDimension > OutputImageType; OutputImageType::Pointer outputImage = fixedImage; // http://www.cmake.org/pipermail/insight-users/2008-October/027784.html //You should decide if you want to create the images from //scratch or whether you want to read an image from disk, //disconnect it from the reader, and then proceed to modify //its pixel values. // outputImage->DisconnectPipeline(); OutputImageType::IndexType pixelLocation; int i = 0; int j = 0; int k = 0; OutputPixelType pixelValue = 0; OutputPixelType inputPixelValue = 0; OutputPixelType maskPixelValue = 0; std::cerr << "Before loop" << std::endl; for(i = 0; i < 128; i++) { std::cerr << i << std::endl; for(j = 0; j < 128; j++) { for(k = 0; k < 124; k++) { pixelLocation[0] = i; pixelLocation[1] = j; pixelLocation[2] = k; inputPixelValue = fixedImage->GetPixel(pixelLocation); maskPixelValue = maskImage->GetPixel(pixelLocation); pixelValue = inputPixelValue * maskPixelValue; outputImage->SetPixel( pixelLocation, pixelValue); } } } typedef itk::CastImageFilter< FixedImageType, OutputImageType > CastFilterType; typedef itk::ImageFileWriter< OutputImageType > WriterType; WriterType::Pointer writer = WriterType::New(); CastFilterType::Pointer caster = CastFilterType::New(); writer->SetFileName( argv[3] ); caster->SetInput( outputImage ); writer->SetInput( caster->GetOutput() ); try { writer->Update(); } catch( itk::ExceptionObject & err ) { std::cerr << "ExceptionObject caught !" << std::endl; std::cerr << err << std::endl; return EXIT_FAILURE; } return EXIT_SUCCESS; } #undef itkProbesCreate #undef itkProbesStart #undef itkProbesStop #undef itkProbesReport