Exemplo n.º 1
0
itk::Object::Pointer QmitkRigid2DTransformView::GetTransform2(itk::Image<TPixelType, VImageDimension>* itkImage1)
{
  if (VImageDimension == 2)
  {
    typedef typename itk::Image< TPixelType, 2 >  FixedImage2DType;
    typedef typename itk::Image< TPixelType, 2 >  MovingImage2DType;

    // the fixedImage is the input parameter (fix for Bug #14626)
    typename FixedImage2DType::Pointer fixedImage2D = itkImage1;

    // the movingImage type is known, use the ImageToItk filter (fix for Bug #14626)
    typename mitk::ImageToItk<MovingImage2DType>::Pointer movingImageToItk =
        mitk::ImageToItk<MovingImage2DType>::New();
    movingImageToItk->SetInput(m_MovingImage);
    movingImageToItk->Update();
    typename MovingImage2DType::Pointer movingImage2D = movingImageToItk->GetOutput();

    typename itk::Rigid2DTransform< double >::Pointer transformPointer = itk::Rigid2DTransform< double >::New();
    transformPointer->SetIdentity();
    if (m_Controls.m_CenterForInitializerRigid2D->isChecked())
    {
      typedef typename itk::Rigid2DTransform< double > Rigid2DTransformType;
      typedef typename itk::CenteredTransformInitializer<Rigid2DTransformType, FixedImage2DType, MovingImage2DType> TransformInitializerType;
      typename TransformInitializerType::Pointer transformInitializer = TransformInitializerType::New();
      transformInitializer->SetFixedImage( fixedImage2D );
      transformInitializer->SetMovingImage( movingImage2D );
      transformInitializer->SetTransform( transformPointer );
      if (m_Controls.m_MomentsRigid2D->isChecked())
      {
        transformInitializer->MomentsOn();
      }
      else
      {
        transformInitializer->GeometryOn();
      }
      transformInitializer->InitializeTransform();
    }
    m_TransformObject = transformPointer.GetPointer();
    return transformPointer.GetPointer();
  }
  return NULL;
}
Exemplo n.º 2
0
itk::Object::Pointer QmitkAffineTransformView::GetTransform2(itk::Image<TPixelType, VImageDimension>* itkImage1)
{
  typedef typename itk::Image< TPixelType, VImageDimension >  FixedImageType;
  typedef typename itk::Image< TPixelType, VImageDimension >  MovingImageType;

  // the fixedImage is the input parameter (fix for Bug #14626)
  typename FixedImageType::Pointer fixedImage = itkImage1;

  // the movingImage type is known, use the ImageToItk filter (fix for Bug #14626)
  typename mitk::ImageToItk<MovingImageType>::Pointer movingImageToItk = mitk::ImageToItk<MovingImageType>::New();
  movingImageToItk->SetInput(m_MovingImage);
  movingImageToItk->Update();
  typename MovingImageType::Pointer movingImage = movingImageToItk->GetOutput();

  typename itk::AffineTransform< double, VImageDimension>::Pointer transformPointer = itk::AffineTransform< double, VImageDimension>::New();
  transformPointer->SetIdentity();
  if (m_Controls.m_CenterForInitializerAffine->isChecked())
  {
    typedef typename itk::AffineTransform< double, VImageDimension >    AffineTransformType;
    typedef typename itk::CenteredTransformInitializer<AffineTransformType, FixedImageType, MovingImageType> TransformInitializerType;
    typename TransformInitializerType::Pointer transformInitializer = TransformInitializerType::New();
    transformInitializer->SetFixedImage( fixedImage );
    transformInitializer->SetMovingImage( movingImage );
    transformInitializer->SetTransform( transformPointer );
    if (m_Controls.m_MomentsAffine->isChecked())
    {
      transformInitializer->MomentsOn();
    }
    else
    {
      transformInitializer->GeometryOn();
    }
    transformInitializer->InitializeTransform();
  }
  m_CenterX = transformPointer->GetCenter()[0];
  m_CenterY = transformPointer->GetCenter()[1];
  m_CenterZ = transformPointer->GetCenter()[2];
  m_TransformObject = transformPointer.GetPointer();
  return transformPointer.GetPointer();
}
itk::Object::Pointer QmitkCenteredSimilarity2DTransformView::GetTransform2(itk::Image<TPixelType, VImageDimension>* /* itkImage1 */)
{
  if (VImageDimension == 2)
  {
    typedef typename itk::Image< TPixelType, 2 >  FixedImage2DType;
    typedef typename itk::Image< TPixelType, 2 >  MovingImage2DType;
    typename FixedImage2DType::Pointer fixedImage2D;
    mitk::CastToItkImage(m_FixedImage, fixedImage2D);
    typename MovingImage2DType::Pointer movingImage2D;
    mitk::CastToItkImage(m_MovingImage, movingImage2D);

    typename itk::CenteredSimilarity2DTransform< double >::Pointer transformPointer = itk::CenteredSimilarity2DTransform< double >::New();
    transformPointer->SetIdentity();
    if (m_Controls.m_CenterForInitializerCenteredSimilarity2D->isChecked())
    {
      typedef typename itk::CenteredSimilarity2DTransform< double >    CenteredSimilarity2DTransformType;
      typedef typename itk::CenteredTransformInitializer<CenteredSimilarity2DTransformType, FixedImage2DType, MovingImage2DType> TransformInitializerType;
      typename TransformInitializerType::Pointer transformInitializer = TransformInitializerType::New();
      transformInitializer->SetFixedImage( fixedImage2D );
      transformInitializer->SetMovingImage( movingImage2D );
      transformInitializer->SetTransform( transformPointer );
      if (m_Controls.m_MomentsCenteredSimilarity2D->isChecked())
      {
        transformInitializer->MomentsOn();
      }
      else
      {
        transformInitializer->GeometryOn();
      }
      transformInitializer->InitializeTransform();        
    }
    transformPointer->SetScale( m_Controls.m_InitialScaleCenteredSimilarity2D->text().toFloat() );
    transformPointer->SetAngle( m_Controls.m_AngleCenteredSimilarity2D->text().toFloat() );
    m_CenterX = transformPointer->GetCenter()[0];
    m_CenterY = transformPointer->GetCenter()[1];
    m_TransformObject = transformPointer.GetPointer();
    return transformPointer.GetPointer();
  }
  return NULL;
}
itk::Object::Pointer QmitkScaleSkewVersor3DTransformView::GetTransform2(itk::Image<TPixelType, VImageDimension>* /*itkImage1*/)
{
  if (VImageDimension == 3)
  {
    typedef typename itk::Image< TPixelType, 3 >  FixedImage3DType;
    typedef typename itk::Image< TPixelType, 3 >  MovingImage3DType;
    typename FixedImage3DType::Pointer fixedImage3D;
    mitk::CastToItkImage(m_FixedImage, fixedImage3D);
    typename MovingImage3DType::Pointer movingImage3D;
    mitk::CastToItkImage(m_MovingImage, movingImage3D);
    typename itk::ScaleSkewVersor3DTransform< double >::Pointer transformPointer = itk::ScaleSkewVersor3DTransform< double >::New();
    transformPointer->SetIdentity();
    if (m_Controls.m_CenterForInitializerScaleSkewVersorRigid3D->isChecked())
    {
      typedef typename itk::ScaleSkewVersor3DTransform< double >    ScaleSkewVersor3DTransformType;
      typedef typename itk::CenteredTransformInitializer<ScaleSkewVersor3DTransformType, FixedImage3DType, MovingImage3DType> TransformInitializerType;
      typename TransformInitializerType::Pointer transformInitializer = TransformInitializerType::New();
      transformInitializer->SetFixedImage( fixedImage3D );
      transformInitializer->SetMovingImage( movingImage3D );
      transformInitializer->SetTransform( transformPointer );
      if (m_Controls.m_MomentsScaleSkewVersorRigid3D->isChecked())
      {
        transformInitializer->MomentsOn();
      }
      else
      {
        transformInitializer->GeometryOn();
      }
      transformInitializer->InitializeTransform();
    }
    m_CenterX = transformPointer->GetCenter()[0];
    m_CenterY = transformPointer->GetCenter()[1];
    m_CenterZ = transformPointer->GetCenter()[2];
    m_TransformObject = transformPointer.GetPointer();
    return transformPointer.GetPointer();
  }
  return NULL;
}
template<class FixedImageType,class MovingImageType,class TransformType> void RegisterPair(const typename FixedImageType::Pointer & fixedImage, const typename MovingImageType::Pointer & movingImage, typename TransformType::Pointer & transform ){


	typedef itk::LogImageAdaptor<FixedImageType,float> LogFixedImageType;

	typedef itk::CenteredTransformInitializer< TransformType,LogFixedImageType,MovingImageType> TransformInitializerType;
	typename TransformInitializerType::Pointer initializer = TransformInitializerType::New();

	typename LogFixedImageType::Pointer logAdaptor=LogFixedImageType::New();
	logAdaptor->SetImage(fixedImage);

	initializer->SetTransform( transform );
	initializer->SetFixedImage( logAdaptor );
	initializer->SetMovingImage( movingImage);

	initializer->MomentsOn();
	initializer->InitializeTransform();

	  // Optimizer Type
	  typedef itk::VersorRigid3DTransformOptimizer OptimizerType;
	 //typedef itk::ConjugateGradientOptimizer OptimizerType;
	  // Metric Type
	  typedef
	  //itk::NormalizedMutualInformationHistogramImageToImageMetric< FixedImageType, MovingImageType >

	  //itk::MutualInformationImageToImageMetric<FixedImageType,MovingImageType >
			  //itk::GradientDifferenceImageToImageMetric<FixedImageType,MovingImageType>
			  itk::NormalizedCorrelationImageToImageMetric< FixedImageType, MovingImageType >
	//		  itk::KullbackLeiblerCompareHistogramImageToImageMetric< FixedImageType, MovingImageType >
			  //itk::MattesMutualInformationImageToImageMetric<FixedImageType,MovingImageType>
			  //itk::MutualInformationHistogramImageToImageMetric<FixedImageType,MovingImageType>
			  //itk::MeanSquaresImageToImageMetric< FixedImageType, MovingImageType >
	  MetricType;


	  // Interpolation technique
	  typedef itk:: LinearInterpolateImageFunction<
	                                    MovingImageType,
	                                    double          >    InterpolatorType;

	  // Registration Method
	  typedef itk::ImageRegistrationMethod<
	                                    FixedImageType,
	                                    MovingImageType >    RegistrationType;


	  typename MetricType::Pointer         metric        = MetricType::New();

	  typename OptimizerType::Pointer      optimizer     = OptimizerType::New();
	  typename InterpolatorType::Pointer   interpolator  = InterpolatorType::New();
	  typename RegistrationType::Pointer   registration  = RegistrationType::New();

  /******************************************************************
   * Set up the optimizer.
   ******************************************************************/
	  typedef typename TransformType::VersorType VersorType;
	  typedef typename VersorType::VectorType VectorType;
	  VersorType rotation;
	  VectorType axis;
	  	axis[0] = 0.0;
	  	axis[1] = 0.0;
	  	axis[2] = 1.0;
	  	const double angle = 0;
	  	rotation.Set( axis, angle );
	  	transform->SetRotation( rotation );

	  	registration->SetInitialTransformParameters( transform->GetParameters() );
	  	// Software Guide : EndCodeSnippet
	  	typedef OptimizerType::ScalesType OptimizerScalesType;
	  	OptimizerScalesType optimizerScales( transform->GetNumberOfParameters() );
	  	const double translationScale = 1.0 / 1000.0;
	  	optimizerScales[0] = 1.0;
	  	optimizerScales[1] = 1.0;
	  	optimizerScales[2] = 1.0;
	  	optimizerScales[3] = translationScale;
	  	optimizerScales[4] = translationScale;
	  	optimizerScales[5] = translationScale;
	  	optimizer->SetScales( optimizerScales );
	  	//optimizer->SetMaximumStepLength( 0.1 );
	  	optimizer->SetMaximumStepLength( 1);
	  	optimizer->SetMinimumStepLength( 0.000000001 );
	  	optimizer->SetNumberOfIterations( 10 );
	  	optimizer->SetMinimize(true);



  /******************************************************************
   * Set up the metric.
   ******************************************************************/
  //metric->SetMovingImageStandardDeviation( 1.0 );
  //metric->SetFixedImageStandardDeviation( 1.0 );

  //metric->SetNumberOfSpatialSamples( 50000000 );
  metric->SetUseAllPixels(true);
  //metric->SetFixedImageRegion( fixedImage->GetBufferedRegion() );

  /******************************************************************
   * Set up the registrator.
   ******************************************************************/

  // connect up the components
  registration->SetMetric( metric );
  registration->SetOptimizer( optimizer );
  registration->SetTransform( transform );
  registration->SetFixedImage( fixedImage );
  registration->SetMovingImage( movingImage );
  registration->SetInterpolator( interpolator );

//  transform->SetIdentity();
//  // set initial parameters to identity
//  typename RegistrationType::ParametersType initialParameters(
//    transform->GetNumberOfParameters() );
//
//  initialParameters.Fill( 0.0 );
//  //initialParameters[3] = 1.0;


	CommandIterationUpdate::Pointer observer = CommandIterationUpdate::New();
	optimizer->AddObserver( itk::IterationEvent(), observer );
	try
	{
	registration->Update();
	std::cout << "Optimizer stop condition: "
	<< registration->GetOptimizer()->GetStopConditionDescription()
	<< std::endl;
	}
	catch( itk::ExceptionObject & err )
	{
	std::cerr << "ExceptionObject caught !" << std::endl;
	std::cerr << err << std::endl;
	//return EXIT_FAILURE;
	}
	OptimizerType::ParametersType finalParameters =
	registration->GetLastTransformParameters();
	const double versorX = finalParameters[0];
	const double versorY = finalParameters[1];
	const double versorZ = finalParameters[2];
	const double finalTranslationX = finalParameters[3];
	const double finalTranslationY = finalParameters[4];
	const double finalTranslationZ = finalParameters[5];
	const unsigned int numberOfIterations = optimizer->GetCurrentIteration();
	const double bestValue = optimizer->GetValue();
	// Print out results
	//
	std::cout << std::endl << std::endl;
	std::cout << "Result = " << std::endl;
	std::cout << " versor X = " << versorX << std::endl;
	std::cout << " versor Y = " << versorY << std::endl;
	std::cout << " versor Z = " << versorZ << std::endl;
	std::cout << " Translation X = " << finalTranslationX << std::endl;
	std::cout << " Translation Y = " << finalTranslationY << std::endl;
	std::cout << " Translation Z = " << finalTranslationZ << std::endl;
	std::cout << " Iterations = " << numberOfIterations << std::endl;
	std::cout << " Metric value = " << bestValue << std::endl;



}