Commit 6244e552 authored by Floris Berendsen's avatar Floris Berendsen
Browse files

ENH: added Niftyreg result writer and test

parent ec0be5e1
......@@ -73,10 +73,10 @@ RegistrationControllerComponent< dummy >::RegistrationControllerStart()
{
reconnectTransformInterface->ReconnectTransform();
}
//for (auto && afterRegistrationInterface : this->m_AfterRegistrationInterfaces)
//{
// afterRegistrationInterface->AfterRegistration();
//}
for (auto && afterRegistrationInterface : this->m_AfterRegistrationInterfaces)
{
afterRegistrationInterface->AfterRegistration();
}
}
......
/*=========================================================================
*
* Copyright Leiden University Medical Center, Erasmus University Medical
* Center and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef selxNiftyregWriteImageComponent_h
#define selxNiftyregWriteImageComponent_h
#include "selxSuperElastixComponent.h"
#include "selxInterfaces.h"
#include "selxNiftyregInterfaces.h"
#include <string.h>
//#include "_reg_f3d.h"
#include "_reg_ReadWriteImage.h"
#include "_reg_ReadWriteMatrix.h"
namespace selx
{
template< class TPixel >
class NiftyregWriteImageComponent :
public SuperElastixComponent<
Accepting< NiftyregWarpedImageInterface< TPixel > >,
Providing< AfterRegistrationInterface >
>
{
public:
/** Standard ITK typedefs. */
typedef NiftyregWriteImageComponent< TPixel > Self;
typedef SuperElastixComponent<
Accepting< NiftyregWarpedImageInterface< TPixel > >,
Providing< AfterRegistrationInterface >
> Superclass;
typedef std::shared_ptr< Self > Pointer;
typedef std::shared_ptr< const Self > ConstPointer;
NiftyregWriteImageComponent( const std::string & name );
virtual ~NiftyregWriteImageComponent();
virtual int Set(typename NiftyregWarpedImageInterface< TPixel >::Pointer component) override;
virtual void AfterRegistration() override;
virtual bool MeetsCriterion( const ComponentBase::CriterionType & criterion ) override;
static const char * GetDescription() { return "NiftyregWriteImage Component"; }
private:
std::string m_ImageFileName;
typename NiftyregWarpedImageInterface< TPixel >::Pointer m_WarpedImageInterface;
protected:
// return the class name and the template arguments to uniquely identify this component.
static inline const std::map< std::string, std::string > TemplateProperties()
{
return { { keys::NameOfClass, "NiftyregWriteImageComponent" }, { keys::PixelType, PodString< TPixel >::Get() }, { keys::Dimensionality, "3" } };
}
};
} //end namespace selx
#ifndef ITK_MANUAL_INSTANTIATION
#include "selxNiftyregWriteImageComponent.hxx"
#endif
#endif // #define selxNiftyregWriteImageComponent_h
/*=========================================================================
*
* Copyright Leiden University Medical Center, Erasmus University Medical
* Center and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#include "selxNiftyregWriteImageComponent.h"
#include "selxCheckTemplateProperties.h"
namespace selx
{
template< class TPixel >
NiftyregWriteImageComponent< TPixel >::NiftyregWriteImageComponent( const std::string & name ) : Superclass( name )
{
}
template< class TPixel >
NiftyregWriteImageComponent< TPixel >::~NiftyregWriteImageComponent()
{
}
template< class TPixel >
int NiftyregWriteImageComponent< TPixel >::Set(typename NiftyregWarpedImageInterface< TPixel >::Pointer component)
{
this->m_WarpedImageInterface = component;
return 1;
}
template< class TPixel >
void
NiftyregWriteImageComponent< TPixel >::AfterRegistration()
{
auto warpedImage = this->m_WarpedImageInterface->GetWarpedNiftiImage();
reg_io_WriteImageFile(warpedImage.get(), this->m_ImageFileName.c_str());
}
template< class TPixel >
bool
NiftyregWriteImageComponent< TPixel >::MeetsCriterion( const ComponentBase::CriterionType & criterion )
{
bool hasUndefinedCriteria( false );
bool meetsCriteria( false );
auto status = CheckTemplateProperties( this->TemplateProperties(), criterion );
if( status == CriterionStatus::Satisfied )
{
return true;
}
else if( status == CriterionStatus::Failed )
{
return false;
} // else: CriterionStatus::Unknown
else if( criterion.first == "FileName" ) //Supports this?
{
meetsCriteria = true;
if( criterion.second.size() == 1 )
{
//TODO check if file exists
this->m_ImageFileName = criterion.second[ 0 ];
}
}
return meetsCriteria;
}
} //end namespace selx
......@@ -66,7 +66,8 @@ public:
private:
reg_f3d< TPixel > * m_reg_f3d;
std::shared_ptr<nifti_image> m_reference_image;
std::shared_ptr<nifti_image> m_floating_image;
// m_warped_images is an array of 2 nifti images. Depending on the use case, typically only [0] is a valid image
std::unique_ptr< std::array<std::shared_ptr<nifti_image>,2>> m_warped_images;
......
......@@ -41,9 +41,10 @@ int
Niftyregf3dComponent< TPixel >
::Set( typename NiftyregReferenceImageInterface< TPixel >::Pointer component )
{
auto referenceimage = component->GetReferenceNiftiImage();
// store the shared_ptr to the data, otherwise it gets freed
this->m_reference_image = component->GetReferenceNiftiImage();
// connect the itk pipeline
this->m_reg_f3d->SetReferenceImage( referenceimage.get() );
this->m_reg_f3d->SetReferenceImage(this->m_reference_image.get());
return 0;
}
......@@ -53,9 +54,10 @@ int
Niftyregf3dComponent< TPixel >
::Set( typename NiftyregFloatingImageInterface< TPixel >::Pointer component )
{
auto floatingimage = component->GetFloatingNiftiImage();
// store the shared_ptr to the data, otherwise it gets freed
this->m_floating_image = component->GetFloatingNiftiImage();
// connect the itk pipeline
this->m_reg_f3d->SetFloatingImage( floatingimage.get() );
this->m_reg_f3d->SetFloatingImage(this->m_floating_image.get());
return 0;
}
......@@ -86,6 +88,7 @@ Niftyregf3dComponent< TPixel >
(*(this->m_warped_images.get()))[0] = std::shared_ptr<nifti_image>(outputWarpedImage[0], nifti_image_free);
(*(this->m_warped_images.get()))[1] = std::shared_ptr<nifti_image>(outputWarpedImage[1], nifti_image_free);
// m_reg_f3d->GetWarpedImage() malloc-ed the container which we must free ourselves.
free(outputWarpedImage);
outputWarpedImage = NULL;
......
......@@ -22,6 +22,7 @@
#include "selxRegistrationController.h"
#include "selxNiftyregReadImageComponent.h"
#include "selxNiftyregWriteImageComponent.h"
#include "selxNiftyregf3dComponent.h"
#include "selxDataManager.h"
#include "gtest/gtest.h"
......@@ -41,7 +42,7 @@ public:
/** register all example components */
typedef TypeList< Niftyregf3dComponent< float >, NiftyregReadImageComponent< float >,
RegistrationControllerComponent< >> RegisterComponents;
NiftyregWriteImageComponent< float >, RegistrationControllerComponent< >> RegisterComponents;
virtual void SetUp()
{
......@@ -75,15 +76,15 @@ TEST_F( NiftyregComponentTest, Register2d )
blueprint->SetComponent( "FixedImage", { { "NameOfClass", { "NiftyregReadImageComponent" } }, { "FileName", { this->dataManager->GetInputFile( "BrainProtonDensitySliceBorder20.nii" ) } } } );
blueprint->SetComponent( "MovingImage", { { "NameOfClass", { "NiftyregReadImageComponent" } }, { "FileName", { this->dataManager->GetInputFile( "BrainProtonDensitySliceR10X13Y17.nii" ) } } } );
blueprint->SetComponent( "RegistrationMethod", { { "NameOfClass", { "Niftyregf3dComponent" } } } );
blueprint->SetComponent( "ResultImage", { { "NameOfClass", { "NiftyregWriteImageComponent" } }, { "FileName", { this->dataManager->GetOutputFile("Nifty_warped.nii") } } });
blueprint->SetComponent( "Controller", { { "NameOfClass", { "RegistrationControllerComponent" } } } );
ParameterMapType connection5Parameters;
connection5Parameters[ "NameOfInterface" ] = { "itkMetricv4Interface" };
blueprint->SetConnection( "Metric", "RegistrationMethod", connection5Parameters );
blueprint->SetConnection( "FixedImage", "RegistrationMethod", { {} } ); //{ { "NameOfInterface", { "NiftyregReferenceImageInterface" } } }
blueprint->SetConnection( "MovingImage", "RegistrationMethod", { {} } ); //{ { "NameOfInterface", { "NiftyregFloatingImageInterface" } } }
blueprint->SetConnection("RegistrationMethod", "ResultImage", { {} }); //{ { "NameOfInterface", { "NiftyregWarpedImageInterface" } } }
blueprint->SetConnection( "RegistrationMethod", "Controller", { {} } );
blueprint->SetConnection("ResultImage", "Controller", { {} });
BlueprintITKPointer superElastixFilterBlueprint = BlueprintITKType::New();
superElastixFilterBlueprint->Set( blueprint );
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment