<div dir="ltr"><div>Why have you commented the lines that set the file names?<br><br>// movingWriter->SetFileName("moving.png");<br><br></div>David<br></div><div class="gmail_extra"><br clear="all"><div><br>David</div>
<br><br><div class="gmail_quote">On Thu, Apr 4, 2013 at 1:57 PM, Bui Toan <span dir="ltr"><<a href="mailto:toanhoi@skku.edu" target="_blank">toanhoi@skku.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div link="blue" vlink="purple" lang="EN-US"><div><p class="MsoNormal">Hello everybody,<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">I downloaded AffineTranformation form Wiki Example and modify it to affine two DICOM files. I build it successfully. But when I run it. It have a error messenger:<u></u><u></u></p>
<p class="MsoNormal">terminate called after throwing an instance of 'itk::ExceptionObject'<u></u><u></u></p><p class="MsoNormal"> what(): /usr/local/include/ITK-4.4/itkImageFileWriter.hxx:123:<u></u><u></u></p><p class="MsoNormal">
itk::ERROR: ImageFileWriter(0x9808fd8): No filename was specified<u></u><u></u></p><p class="MsoNormal">Aborted (core dumped)<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">This is my editing code. Please help me edit it. I am using ITK version newest 4.3.1 for Linux<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">#include "itkCastImageFilter.h"<u></u><u></u></p><p class="MsoNormal">#include "itkEllipseSpatialObject.h"<u></u><u></u></p><p class="MsoNormal">
#include "itkImage.h"<u></u><u></u></p><p class="MsoNormal">#include "itkImageRegistrationMethod.h"<u></u><u></u></p><p class="MsoNormal">#include "itkLinearInterpolateImageFunction.h"<u></u><u></u></p>
<p class="MsoNormal">#include "itkImageFileReader.h"<u></u><u></u></p><p class="MsoNormal">#include "itkImageFileWriter.h"<u></u><u></u></p><p class="MsoNormal">#include "itkMeanSquaresImageToImageMetric.h"<u></u><u></u></p>
<p class="MsoNormal">#include "itkRegularStepGradientDescentOptimizer.h"<u></u><u></u></p><p class="MsoNormal">#include "itkResampleImageFilter.h"<u></u><u></u></p><p class="MsoNormal">#include "itkRescaleIntensityImageFilter.h"<u></u><u></u></p>
<p class="MsoNormal">#include "itkSpatialObjectToImageFilter.h"<u></u><u></u></p><p class="MsoNormal">#include "itkAffineTransform.h"<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">
<u></u> <u></u></p><p class="MsoNormal">#include "itkGDCMImageIO.h"<u></u><u></u></p><p class="MsoNormal">// Software Guide : EndCodeSnippet<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">
#include <list><u></u><u></u></p><p class="MsoNormal">#include <fstream><u></u><u></u></p><p class="MsoNormal">const unsigned int Dimension = 2;<u></u><u></u></p><p class="MsoNormal">typedef unsigned char PixelType;<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">typedef itk::Image< PixelType, Dimension > ImageType;<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">static void CreateEllipseImage(ImageType::Pointer image);<u></u><u></u></p>
<p class="MsoNormal">static void CreateSphereImage(ImageType::Pointer image);<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">int main(int, char *[] )<u></u><u></u></p><p class="MsoNormal">{<u></u><u></u></p>
<p class="MsoNormal"> // The transform that will map the fixed image into the moving image.<u></u><u></u></p><p class="MsoNormal"> typedef itk::AffineTransform< double, Dimension > TransformType;<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> // An optimizer is required to explore the parameter space of the transform<u></u><u></u></p><p class="MsoNormal"> // in search of optimal values of the metric.<u></u><u></u></p>
<p class="MsoNormal"> typedef itk::RegularStepGradientDescentOptimizer OptimizerType;<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> // The metric will compare how well the two images match each other. Metric<u></u><u></u></p>
<p class="MsoNormal"> // types are usually parameterized by the image types as it can be seen in<u></u><u></u></p><p class="MsoNormal"> // the following type declaration.<u></u><u></u></p><p class="MsoNormal"> typedef itk::MeanSquaresImageToImageMetric<<u></u><u></u></p>
<p class="MsoNormal"> ImageType,<u></u><u></u></p><p class="MsoNormal"> ImageType > MetricType;<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> // Finally, the type of the interpolator is declared. The interpolator will<u></u><u></u></p>
<p class="MsoNormal"> // evaluate the intensities of the moving image at non-grid positions.<u></u><u></u></p><p class="MsoNormal"> typedef itk:: LinearInterpolateImageFunction<<u></u><u></u></p><p class="MsoNormal">
ImageType,<u></u><u></u></p><p class="MsoNormal"> double > InterpolatorType;<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> // The registration method type is instantiated using the types of the<u></u><u></u></p>
<p class="MsoNormal"> // fixed and moving images. This class is responsible for interconnecting<u></u><u></u></p><p class="MsoNormal"> // all the components that we have described so far.<u></u><u></u></p><p class="MsoNormal">
typedef itk::ImageRegistrationMethod<<u></u><u></u></p><p class="MsoNormal"> ImageType,<u></u><u></u></p><p class="MsoNormal"> ImageType > RegistrationType;<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"> // Create components<u></u><u></u></p><p class="MsoNormal"> MetricType::Pointer metric = MetricType::New();<u></u><u></u></p><p class="MsoNormal"> TransformType::Pointer transform = TransformType::New();<u></u><u></u></p>
<p class="MsoNormal"> OptimizerType::Pointer optimizer = OptimizerType::New();<u></u><u></u></p><p class="MsoNormal"> InterpolatorType::Pointer interpolator = InterpolatorType::New();<u></u><u></u></p><p class="MsoNormal">
RegistrationType::Pointer registration = RegistrationType::New();<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> // Each component is now connected to the instance of the registration method.<u></u><u></u></p>
<p class="MsoNormal"> registration->SetMetric( metric );<u></u><u></u></p><p class="MsoNormal"> registration->SetOptimizer( optimizer );<u></u><u></u></p><p class="MsoNormal"> registration->SetTransform( transform );<u></u><u></u></p>
<p class="MsoNormal"> registration->SetInterpolator( interpolator );<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> // Write the two synthetic inputs<u></u><u></u></p><p class="MsoNormal">
<u></u><u></u></p><p class="MsoNormal"> typedef itk::Image< PixelType, Dimension > FixedImageType;<u></u><u></u></p><p class="MsoNormal"> typedef itk::Image< PixelType, Dimension > MovingImageType;<u></u><u></u></p>
<p class="MsoNormal"> // Software Guide : EndCodeSnippet<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> // Set up the file readers<u></u><u></u></p><p class="MsoNormal"> typedef itk::ImageFileReader< FixedImageType > FixedImageReaderType;<u></u><u></u></p>
<p class="MsoNormal"> typedef itk::ImageFileReader< MovingImageType > MovingImageReaderType;<u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal"> FixedImageReaderType::Pointer fixedImageReader = FixedImageReaderType::New();<u></u><u></u></p>
<p class="MsoNormal"> MovingImageReaderType::Pointer movingImageReader = MovingImageReaderType::New();<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> fixedImageReader->SetFileName("fix.dcm" );<u></u><u></u></p>
<p class="MsoNormal"> movingImageReader->SetFileName( "mov.dcm" );<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> typedef itk::ImageFileWriter< ImageType > WriterType;<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> WriterType::Pointer fixedWriter = WriterType::New();<u></u><u></u></p><p class="MsoNormal"> //ixedWriter->SetFileName("fixed.png");<u></u><u></u></p>
<p class="MsoNormal"> fixedWriter->SetInput( fixedImageReader->GetOutput());<u></u><u></u></p><p class="MsoNormal"> fixedWriter->Update();<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">
WriterType::Pointer movingWriter = WriterType::New();<u></u><u></u></p><p class="MsoNormal"> // movingWriter->SetFileName("moving.png");<u></u><u></u></p><p class="MsoNormal"> movingWriter->SetInput( movingImageReader->GetOutput());<u></u><u></u></p>
<p class="MsoNormal"> movingWriter->Update();<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> // Set the registration inputs<u></u><u></u></p><p class="MsoNormal"> registration->SetFixedImage(fixedImageReader->GetOutput());<u></u><u></u></p>
<p class="MsoNormal"> registration->SetMovingImage(movingImageReader->GetOutput());<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> registration->SetFixedImageRegion(<u></u><u></u></p>
<p class="MsoNormal"> fixedImageReader->GetOutput()->GetLargestPossibleRegion() );<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> // Initialize the transform<u></u><u></u></p><p class="MsoNormal">
typedef RegistrationType::ParametersType ParametersType;<u></u><u></u></p><p class="MsoNormal"> ParametersType initialParameters( transform->GetNumberOfParameters() );<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"> // rotation matrix<u></u><u></u></p><p class="MsoNormal"> initialParameters[0] = 1.0; // R(0,0)<u></u><u></u></p><p class="MsoNormal"> initialParameters[1] = 0.0; // R(0,1)<u></u><u></u></p><p class="MsoNormal">
initialParameters[2] = 0.0; // R(1,0)<u></u><u></u></p><p class="MsoNormal"> initialParameters[3] = 1.0; // R(1,1)<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> // translation vector<u></u><u></u></p>
<p class="MsoNormal"> initialParameters[4] = 0.0;<u></u><u></u></p><p class="MsoNormal"> initialParameters[5] = 0.0;<u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal"> registration->SetInitialTransformParameters( initialParameters );<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> optimizer->SetMaximumStepLength( .1 ); // If this is set too high, you will get a<u></u><u></u></p><p class="MsoNormal"> //"itk::ERROR: MeanSquaresImageToImageMetric(0xa27ce70): Too many samples map outside moving image buffer: 1818 / 10000" error<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> optimizer->SetMinimumStepLength( 0.01 );<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> // Set a stopping criterion<u></u><u></u></p>
<p class="MsoNormal"> optimizer->SetNumberOfIterations( 200 );<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> // Connect an observer<u></u><u></u></p><p class="MsoNormal"> //CommandIterationUpdate::Pointer observer = CommandIterationUpdate::New();<u></u><u></u></p>
<p class="MsoNormal"> //optimizer->AddObserver( itk::IterationEvent(), observer );<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> try<u></u><u></u></p><p class="MsoNormal"> {<u></u><u></u></p>
<p class="MsoNormal"> registration->Update();<u></u><u></u></p><p class="MsoNormal"> }<u></u><u></u></p><p class="MsoNormal"> catch( itk::ExceptionObject & err )<u></u><u></u></p><p class="MsoNormal"> {<u></u><u></u></p>
<p class="MsoNormal"> std::cerr << "ExceptionObject caught !" << std::endl;<u></u><u></u></p><p class="MsoNormal"> std::cerr << err << std::endl;<u></u><u></u></p><p class="MsoNormal">
return EXIT_FAILURE;<u></u><u></u></p><p class="MsoNormal"> }<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> // The result of the registration process is an array of parameters that<u></u><u></u></p>
<p class="MsoNormal"> // defines the spatial transformation in an unique way. This final result is<u></u><u></u></p><p class="MsoNormal"> // obtained using the \code{GetLastTransformParameters()} method.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> ParametersType finalParameters = registration->GetLastTransformParameters();<u></u><u></u></p><p class="MsoNormal"> std::cout << "Final parameters: " << finalParameters << std::endl;<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal"> // The value of the image metric corresponding to the last set of parameters<u></u><u></u></p><p class="MsoNormal"> // can be obtained with the \code{GetValue()} method of the optimizer.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> const double bestValue = optimizer->GetValue();<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> // Print out results<u></u><u></u></p>
<p class="MsoNormal"> //<u></u><u></u></p><p class="MsoNormal"> std::cout << "Result = " << std::endl;<u></u><u></u></p><p class="MsoNormal"> std::cout << " Metric value = " << bestValue << std::endl;<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> // It is common, as the last step of a registration task, to use the<u></u><u></u></p><p class="MsoNormal"> // resulting transform to map the moving image into the fixed image space.<u></u><u></u></p>
<p class="MsoNormal"> // This is easily done with the \doxygen{ResampleImageFilter}.<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> typedef itk::ResampleImageFilter<<u></u><u></u></p>
<p class="MsoNormal"> ImageType,<u></u><u></u></p><p class="MsoNormal"> ImageType > ResampleFilterType;<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> ResampleFilterType::Pointer resampler = ResampleFilterType::New();<u></u><u></u></p>
<p class="MsoNormal"> resampler->SetInput( movingImageReader->GetOutput());<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> // The Transform that is produced as output of the Registration method is<u></u><u></u></p>
<p class="MsoNormal"> // also passed as input to the resampling filter. Note the use of the<u></u><u></u></p><p class="MsoNormal"> // methods \code{GetOutput()} and \code{Get()}. This combination is needed<u></u><u></u></p>
<p class="MsoNormal"> // here because the registration method acts as a filter whose output is a<u></u><u></u></p><p class="MsoNormal"> // transform decorated in the form of a \doxygen{DataObject}. For details in<u></u><u></u></p>
<p class="MsoNormal"> // this construction you may want to read the documentation of the<u></u><u></u></p><p class="MsoNormal"> // \doxygen{DataObjectDecorator}.<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"> resampler->SetTransform( registration->GetOutput()->Get() );<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> // As described in Section \ref{sec:ResampleImageFilter}, the<u></u><u></u></p>
<p class="MsoNormal"> // ResampleImageFilter requires additional parameters to be specified, in<u></u><u></u></p><p class="MsoNormal"> // particular, the spacing, origin and size of the output image. The default<u></u><u></u></p>
<p class="MsoNormal"> // pixel value is also set to a distinct gray level in order to highlight<u></u><u></u></p><p class="MsoNormal"> // the regions that are mapped outside of the moving image.<u></u><u></u></p><p class="MsoNormal">
<u></u> <u></u></p><p class="MsoNormal"> resampler->SetSize( fixedImageReader->GetOutput()->GetLargestPossibleRegion().GetSize() );<u></u><u></u></p><p class="MsoNormal"> resampler->SetOutputOrigin( fixedImageReader->GetOutput()->GetOrigin() );<u></u><u></u></p>
<p class="MsoNormal"> resampler->SetOutputSpacing( fixedImageReader->GetOutput()->GetSpacing() );<u></u><u></u></p><p class="MsoNormal"> resampler->SetOutputDirection( fixedImageReader->GetOutput()->GetDirection() );<u></u><u></u></p>
<p class="MsoNormal"> resampler->SetDefaultPixelValue( 100 );<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> // The output of the filter is passed to a writer that will store the<u></u><u></u></p>
<p class="MsoNormal"> // image in a file. An \doxygen{CastImageFilter} is used to convert the<u></u><u></u></p><p class="MsoNormal"> // pixel type of the resampled image to the final type used by the<u></u><u></u></p>
<p class="MsoNormal"> // writer. The cast and writer filters are instantiated below.<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> typedef unsigned char OutputPixelType;<u></u><u></u></p>
<p class="MsoNormal"> typedef itk::Image< OutputPixelType, Dimension > OutputImageType;<u></u><u></u></p><p class="MsoNormal"> typedef itk::CastImageFilter<<u></u><u></u></p><p class="MsoNormal"> ImageType,<u></u><u></u></p>
<p class="MsoNormal"> ImageType > CastFilterType;<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"> WriterType::Pointer writer = WriterType::New();<u></u><u></u></p><p class="MsoNormal">
CastFilterType::Pointer caster = CastFilterType::New();<u></u><u></u></p><p class="MsoNormal"> writer->SetFileName("output.png");<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">
caster->SetInput( resampler->GetOutput() );<u></u><u></u></p><p class="MsoNormal"> writer->SetInput( caster->GetOutput() );<u></u><u></u></p><p class="MsoNormal"> writer->Update();<u></u><u></u></p><p class="MsoNormal">
<u></u> <u></u></p><p class="MsoNormal"> return EXIT_SUCCESS;<u></u><u></u></p><p class="MsoNormal">}<u></u><u></u></p></div></div><br>_____________________________________<br>
Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
<br>
Visit other Kitware open-source projects at<br>
<a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Kitware offers ITK Training Courses, for more information visit:<br>
<a href="http://www.kitware.com/products/protraining.php" target="_blank">http://www.kitware.com/products/protraining.php</a><br>
<br>
Please keep messages on-topic and check the ITK FAQ at:<br>
<a href="http://www.itk.org/Wiki/ITK_FAQ" target="_blank">http://www.itk.org/Wiki/ITK_FAQ</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://www.itk.org/mailman/listinfo/insight-users" target="_blank">http://www.itk.org/mailman/listinfo/insight-users</a><br>
<br></blockquote></div><br></div>