diff --git a/Modules/DiffusionImaging/DiffusionCore/Algorithms/Registration/mitkBatchedRegistration.cpp b/Modules/DiffusionImaging/DiffusionCore/Algorithms/Registration/mitkBatchedRegistration.cpp index 2dbeafe76a..878a0b9a1b 100644 --- a/Modules/DiffusionImaging/DiffusionCore/Algorithms/Registration/mitkBatchedRegistration.cpp +++ b/Modules/DiffusionImaging/DiffusionCore/Algorithms/Registration/mitkBatchedRegistration.cpp @@ -1,155 +1,183 @@ #include "mitkBatchedRegistration.h" #include "mitkPyramidImageRegistrationMethod.h" #include "mitkDiffusionImage.h" #include #include #include "itkB0ImageExtractionImageFilter.h" #include #include // VTK #include +// DEBUG +#include + + mitk::BatchedRegistration::BatchedRegistration() : m_RegisteredImagesValid(false) { } void mitk::BatchedRegistration::SetFixedImage(mitk::Image::Pointer& fixedImage) { m_FixedImage = fixedImage; } void mitk::BatchedRegistration::SetMovingReferenceImage(Image::Pointer &movingImage) { m_MovingReference = movingImage; m_RegisteredImagesValid = false; } void mitk::BatchedRegistration::SetBatch(std::vector imageBatch) { m_ImageBatch.clear(); m_ImageBatch = imageBatch; } std::vector mitk::BatchedRegistration::GetRegisteredImages() { if (!m_RegisteredImagesValid) { m_RegisteredImages.clear(); // First transform moving reference image TransformType transf = GetTransformation(m_FixedImage, m_MovingReference); // store it as first element in vector ApplyTransformationToImage(m_MovingReference,transf); m_RegisteredImages.push_back(m_MovingReference); // apply transformation to whole batch std::vector::const_iterator itEnd = m_ImageBatch.end(); for (std::vector::iterator it = m_ImageBatch.begin(); it != itEnd; ++it) { ApplyTransformationToImage(*it,transf); m_RegisteredImages.push_back(*it); } } return m_RegisteredImages; } void mitk::BatchedRegistration::ApplyTransformationToImage(mitk::Image::Pointer &img, const mitk::BatchedRegistration::TransformType &transformation) const { - itk::ScalableAffineTransform::Pointer rotationTransform; + typedef mitk::DiffusionImage DiffusionImageType; + vtkMatrix4x4* transformationMatrix = vtkMatrix4x4::New(); vnl_matrix_fixed vnlRotationMatrix; for (int i = 0; i < 4;++i) for (int j = 0; j < 4;++j) { transformationMatrix->Element[i][j] = transformation.get(i,j); if (i < 3 && j < 3) vnlRotationMatrix.set(i,j,transformation.get(i,j)); } - rotationTransform = itk::ScalableAffineTransform::New(); + img->GetGeometry()->Compose( transformationMatrix); - itk::ScalableAffineTransform::Pointer geometryTransform = img->GetGeometry()->GetIndexToWorldTransform(); - img->GetGeometry()->Compose( transformationMatrix); //SetIndexToWorldTransform(geometryTransform); - - if (dynamic_cast*> (img.GetPointer()) != NULL) + if (dynamic_cast (img.GetPointer()) != NULL) { - mitk::DiffusionImage::Pointer diffImages = - dynamic_cast*>(img.GetPointer()); + DiffusionImageType::Pointer diffImages = + dynamic_cast(img.GetPointer()); + + // Changes to default geometry does not affect the presentation + // of Diffusion Images. (No idea why) + // Workaround: + // Extracting a B0-image + // Casting B0 to an MITK image, so we have an image with + // the same dimensions and space + // Apply the transformation to this image + // Cast back to ITK image + // Copy Transformation informations from B0-ITK-Image to + // diffusion vector image. + itk::B0ImageExtractionImageFilter::Pointer b0Extraction = + itk::B0ImageExtractionImageFilter::New(); + b0Extraction->SetInput(diffImages->GetVectorImage()); + b0Extraction->SetDirections(diffImages->GetDirections()); + b0Extraction->Update(); + mitk::Image::Pointer tmp = mitk::Image::New(); + tmp->InitializeByItk(b0Extraction->GetOutput()); + tmp->SetVolume(b0Extraction->GetOutput()->GetBufferPointer()); + + tmp->GetGeometry()->Compose(transformationMatrix); + itk::Image::Pointer itkTmp; + mitk::CastToItkImage >(tmp, itkTmp); + + diffImages->GetVectorImage()->SetOrigin(itkTmp->GetOrigin()); + diffImages->GetVectorImage()->SetDirection(itkTmp->GetDirection()); + diffImages->GetVectorImage()->SetSpacing(itkTmp->GetSpacing()); mitk::DiffusionImageCorrectionFilter::Pointer correctionFilter = mitk::DiffusionImageCorrectionFilter::New(); + // For Diff. Images: Need to rotate the gradients correctionFilter->SetImage(diffImages); - // apply transformation to all gradients correctionFilter->CorrectDirections(vnlRotationMatrix); - //img = correctionFilter->GetOutput(); } } mitk::BatchedRegistration::TransformType mitk::BatchedRegistration::GetTransformation(mitk::Image::Pointer fixedImage, mitk::Image::Pointer movingImage, mitk::Image::Pointer mask) { // Handle case that fixed or moving image is a DWI image mitk::DiffusionImage* fixedDwi = dynamic_cast*> (fixedImage.GetPointer()); mitk::DiffusionImage* movingDwi = dynamic_cast*> (movingImage.GetPointer()); itk::B0ImageExtractionImageFilter::Pointer b0Extraction = itk::B0ImageExtractionImageFilter::New(); if (fixedDwi != NULL) { b0Extraction->SetInput(fixedDwi->GetVectorImage()); b0Extraction->SetDirections(fixedDwi->GetDirections()); b0Extraction->Update(); mitk::Image::Pointer tmp = mitk::Image::New(); tmp->InitializeByItk(b0Extraction->GetOutput()); tmp->SetVolume(b0Extraction->GetOutput()->GetBufferPointer()); fixedImage = tmp; } if (movingDwi != NULL) { b0Extraction->SetInput(movingDwi->GetVectorImage()); b0Extraction->SetDirections(movingDwi->GetDirections()); b0Extraction->Update(); mitk::Image::Pointer tmp = mitk::Image::New(); tmp->InitializeByItk(b0Extraction->GetOutput()); tmp->SetVolume(b0Extraction->GetOutput()->GetBufferPointer()); movingImage = tmp; } // Start registration mitk::PyramidImageRegistrationMethod::Pointer registrationMethod = mitk::PyramidImageRegistrationMethod::New(); registrationMethod->SetFixedImage( fixedImage ); if (mask.IsNotNull()) { registrationMethod->SetFixedImageMask(mask); registrationMethod->SetUseFixedImageMask(true); } else { registrationMethod->SetUseFixedImageMask(false); } registrationMethod->SetTransformToRigid(); registrationMethod->SetCrossModalityOn(); registrationMethod->SetMovingImage(movingImage); registrationMethod->Update(); TransformType transformation; mitk::PyramidImageRegistrationMethod::TransformMatrixType rotationMatrix; rotationMatrix = registrationMethod->GetLastRotationMatrix().transpose(); double param[6]; registrationMethod->GetParameters(param); // first three: euler angles, last three translation for (unsigned int i = 0; i < 3; i++) { for (unsigned int j = 0; j < 3; ++j) { double value = rotationMatrix.get(i,j); transformation.set(i,j, value); } } transformation.set(0,3,-param[3]); transformation.set(1,3,-param[4]); transformation.set(2,3,-param[5]); transformation.set(3,3,1); return transformation; }