diff --git a/Modules/MatchPointRegistration/Helper/mitkMaskedAlgorithmHelper.cpp b/Modules/MatchPointRegistration/Helper/mitkMaskedAlgorithmHelper.cpp index 97736cc969..f38021161a 100644 --- a/Modules/MatchPointRegistration/Helper/mitkMaskedAlgorithmHelper.cpp +++ b/Modules/MatchPointRegistration/Helper/mitkMaskedAlgorithmHelper.cpp @@ -1,183 +1,175 @@ /*============================================================================ The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center (DKFZ) All rights reserved. Use of this source code is governed by a 3-clause BSD license that can be found in the LICENSE file. ============================================================================*/ #include "mitkMaskedAlgorithmHelper.h" #include // Mitk #include // MatchPoint #include "mapMaskedRegistrationAlgorithmInterface.h" #include namespace mitk { MaskedAlgorithmHelper::MaskedAlgorithmHelper(map::algorithm::RegistrationAlgorithmBase* algorithm) : m_AlgorithmBase(algorithm) { } bool MaskedAlgorithmHelper::HasMaskedRegistrationAlgorithmInterface(const map::algorithm::RegistrationAlgorithmBase* algorithm) { using MaskedInterface2D = const ::map::algorithm::facet::MaskedRegistrationAlgorithmInterface<2, 2>; using MaskedInterface3D = const ::map::algorithm::facet::MaskedRegistrationAlgorithmInterface<3, 3>; return dynamic_cast(algorithm) != nullptr && dynamic_cast(algorithm) != nullptr; }; bool MaskedAlgorithmHelper:: CheckSupport(const mitk::Image* movingMask, const mitk::Image* targetMask) const { if (! m_AlgorithmBase) mapDefaultExceptionStaticMacro(<< "Error, cannot check data. Helper has no algorithm defined."); unsigned int movingDim = m_AlgorithmBase->getMovingDimensions(); unsigned int targetDim = m_AlgorithmBase->getTargetDimensions(); bool result = movingDim == targetDim; if ( movingMask) { result = result && (movingMask->GetDimension() == movingDim); - - if (movingDim == 2) - { - typedef itk::Image MaskImageType; - mitk::PixelType maskPixelType = mitk::MakePixelType(); - - result = result && (maskPixelType == movingMask->GetPixelType()); - } - else if (movingDim == 3) - { - typedef itk::Image MaskImageType; - mitk::PixelType maskPixelType = mitk::MakePixelType(); - - result = result && (maskPixelType == movingMask->GetPixelType()); - } } if ( targetMask) { result = result && (targetMask->GetDimension() == targetDim); - - if (movingDim == 2) - { - typedef itk::Image MaskImageType; - mitk::PixelType maskPixelType = mitk::MakePixelType(); - - result = result && (maskPixelType == targetMask->GetPixelType()); - } - else if (movingDim == 3) - { - typedef itk::Image MaskImageType; - mitk::PixelType maskPixelType = mitk::MakePixelType(); - - result = result && (maskPixelType == targetMask->GetPixelType()); - } } if (movingDim == 2) { typedef ::map::algorithm::facet::MaskedRegistrationAlgorithmInterface<2, 2> MaskedInterface; const MaskedInterface* pMaskedReg = dynamic_cast(m_AlgorithmBase.GetPointer()); result = result && pMaskedReg; } else if (movingDim == 3) { typedef ::map::algorithm::facet::MaskedRegistrationAlgorithmInterface<3, 3> MaskedInterface; const MaskedInterface* pMaskedReg = dynamic_cast(m_AlgorithmBase.GetPointer()); result = result && pMaskedReg; } else { result = false; } return result; }; bool MaskedAlgorithmHelper::SetMasks(const mitk::Image* movingMask, const mitk::Image* targetMask) { if (! m_AlgorithmBase) mapDefaultExceptionStaticMacro(<< "Error, cannot set data. Helper has no algorithm defined."); if (! CheckSupport(movingMask, targetMask)) return false; unsigned int movingDim = m_AlgorithmBase->getMovingDimensions(); unsigned int targetDim = m_AlgorithmBase->getTargetDimensions(); if (movingDim!=targetDim) return false; if (movingDim == 2) { return DoSetMasks<2,2>(movingMask, targetMask); } else if (movingDim == 3) { return DoSetMasks<3,3>(movingMask, targetMask); } return false; }; - template + template bool MaskedAlgorithmHelper::DoSetMasks(const mitk::Image* movingMask, const mitk::Image* targetMask) { - typedef itk::SpatialObject MovingSpatialType; - typedef itk::SpatialObject TargetSpatialType; + typedef itk::SpatialObject MovingSpatialType; + typedef itk::SpatialObject TargetSpatialType; - typedef ::map::algorithm::facet::MaskedRegistrationAlgorithmInterface MaskedRegInterface; + typedef ::map::algorithm::facet::MaskedRegistrationAlgorithmInterface MaskedRegInterface; MaskedRegInterface* pAlg = dynamic_cast(m_AlgorithmBase.GetPointer()); if (!pAlg) return false; if (movingMask) { - AccessFixedTypeByItk(movingMask, DoConvertMask, (MaskPixelType), (VImageDimension1)); + AccessFixedDimensionByItk(movingMask, DoConvertMask, VMovingDimension); typename MovingSpatialType::Pointer movingSpatial = dynamic_cast(m_convertResult.GetPointer()); - if (! movingSpatial) mapDefaultExceptionStaticMacro(<< "Error, cannot convert moving mask."); + if (!movingSpatial) mapDefaultExceptionStaticMacro(<< "Error, cannot convert moving mask."); pAlg->setMovingMask(movingSpatial); } else { pAlg->setMovingMask(nullptr); } if (targetMask) { - AccessFixedTypeByItk(targetMask, DoConvertMask, (MaskPixelType), (VImageDimension2)); + AccessFixedDimensionByItk(targetMask, DoConvertMask, VTargetDimension); typename TargetSpatialType::Pointer targetSpatial = dynamic_cast(m_convertResult.GetPointer()); if (! targetSpatial) mapDefaultExceptionStaticMacro(<< "Error, cannot convert moving mask."); pAlg->setTargetMask(targetSpatial); } else { pAlg->setTargetMask(nullptr); } return true; } - template - void MaskedAlgorithmHelper::DoConvertMask(const itk::Image* mask) + template + typename itk::SpatialObject::Pointer + MaskedAlgorithmHelper::ConvertMaskSO(const itk::Image* mask) const { typedef itk::ImageMaskSpatialObject SpatialType; typename SpatialType::Pointer spatial = SpatialType::New(); spatial->SetImage(mask); - m_convertResult = spatial.GetPointer(); + return spatial.GetPointer(); + } + + template + void MaskedAlgorithmHelper::DoConvertMask(const itk::Image* mask) + { + using InImageType = itk::Image; + using MaskImageType = itk::Image; + + typedef itk::CastImageFilter< InImageType, MaskImageType > CastFilterType; + typename CastFilterType::Pointer imageCaster = CastFilterType::New(); + + imageCaster->SetInput(mask); + + auto castedMask = imageCaster->GetOutput(); + imageCaster->Update(); + m_convertResult = ConvertMaskSO(castedMask); } + template + void MaskedAlgorithmHelper::DoConvertMask(const itk::Image* mask) + { + m_convertResult = ConvertMaskSO(mask); + } } diff --git a/Modules/MatchPointRegistration/Helper/mitkMaskedAlgorithmHelper.h b/Modules/MatchPointRegistration/Helper/mitkMaskedAlgorithmHelper.h index 515ebc90c8..4d12409887 100644 --- a/Modules/MatchPointRegistration/Helper/mitkMaskedAlgorithmHelper.h +++ b/Modules/MatchPointRegistration/Helper/mitkMaskedAlgorithmHelper.h @@ -1,78 +1,86 @@ /*============================================================================ The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center (DKFZ) All rights reserved. Use of this source code is governed by a 3-clause BSD license that can be found in the LICENSE file. ============================================================================*/ #ifndef mitkMaskedAlgorithmHelper_h #define mitkMaskedAlgorithmHelper_h +#include "itkSpatialObject.h" //MatchPoint #include "mapRegistrationAlgorithmBase.h" //MITK #include //MITK #include "MitkMatchPointRegistrationExports.h" namespace mitk { /*! \brief MaskedAlgorithmHelper Helper class as an easy bridge to set mitk images as masks for registration algorithms. It is assumed that the Image indicates the mask by pixel values != 0. \remark Currently only 2D-2D and 3D-3D algorithms are supported. - \remark Currently only masks with pixel type unsigned char (default mitk segmentation images) are supported. \remark Current implementation is not thread-save. Just use one Helper class per registration task. */ class MITKMATCHPOINTREGISTRATION_EXPORT MaskedAlgorithmHelper { public: MaskedAlgorithmHelper(map::algorithm::RegistrationAlgorithmBase* algorithm); /** Set one or both masks to an algorithm. * If the algorithm does not support masks it will be ignored. * @remark Set a mask to nullptr if you don't want to set it. * @return Indicates if the masks could be set/was supported by algorithm.*/ bool SetMasks(const mitk::Image* movingMask, const mitk::Image* targetMask); /** Checks if the algorithm supports masks of the passed type.*/ bool CheckSupport(const mitk::Image* movingMask, const mitk::Image* targetMask) const; static bool HasMaskedRegistrationAlgorithmInterface(const map::algorithm::RegistrationAlgorithmBase* algorithm); ~MaskedAlgorithmHelper() {} private: - typedef unsigned char MaskPixelType; + using MaskPixelType = unsigned char; MaskedAlgorithmHelper& operator = (const MaskedAlgorithmHelper&); MaskedAlgorithmHelper(const MaskedAlgorithmHelper&); /**Internal helper that is used by SetMasks if the data are images to set them properly.*/ template bool DoSetMasks(const mitk::Image* movingMask, const mitk::Image* targetMask); - /**Internal helper that is used by SetData if the data are images to set them properly.*/ + /**Internal helper that is used by SetData if the data are images to cast and set them properly.*/ template void DoConvertMask(const itk::Image* mask); + /**Internal helper that is used by SetData if the data are images to set them properly.*/ + template + void DoConvertMask(const itk::Image* mask); + + /**Internal helper that is used to pack the mask image into a spatial object.*/ + template + typename itk::SpatialObject::Pointer ConvertMaskSO(const itk::Image* mask) const; + /**Helper member that containes the result of the last call of DoConvertMask().*/ itk::DataObject::Pointer m_convertResult; map::algorithm::RegistrationAlgorithmBase::Pointer m_AlgorithmBase; }; } #endif