[Insight-users] Multi Resolution

Robert-Paul Buitendijk r . p . buitendijk at student . utwente . nl
Tue, 28 Oct 2003 20:41:55 +0100


This is a multi-part message in MIME format.

------=_NextPart_000_0003_01C39D93.ECB2CCC0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello

I tried to make a multi resolution registration but i get an error.
The code in the example which i copied gives some errors.
The file is attached below.

itkNewMacro( Self ); allready gets an error. did i miss a directory which i
should have linked.

Tnx Robert

------=_NextPart_000_0003_01C39D93.ECB2CCC0
Content-Type: text/plain;
	name="MultiRes.txt"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="MultiRes.txt"

// Reg_TemplateDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Reg_Template.h"
#include "Reg_TemplateDlg.h"

//Itk files
#include "itkMultiResolutionImageRegistrationMethod.h"
#include "itkTranslationTransform.h"
#include "itkMattesMutualInformationImageToImageMetric.h"
#include "itkLinearInterpolateImageFunction.h"
#include "itkRegularStepGradientDescentOptimizer.h"
#include "itkMultiResolutionPyramidImageFilter.h"
#include "itkImage.h"

#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"

#include "itkResampleImageFilter.h"
#include "itkCastImageFilter.h"

//#include "itkCommand.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] =3D __FILE__;
#endif

///////////////////////////////////////////////////////
template <typename TRegistration>
class RegistrationInterfaceCommand : public itk::Command=20
{

public:
  typedef  RegistrationInterfaceCommand   Self;
  typedef  itk::Command                   Superclass;
  typedef  itk::SmartPointer<Self>        Pointer;
  itkNewMacro( Self );
protected:
  RegistrationInterfaceCommand() {};

public:
  typedef   TRegistration                              RegistrationType;
  typedef   RegistrationType *                         =
RegistrationPointer;
  typedef   itk::RegularStepGradientDescentOptimizer   OptimizerType;
  typedef   OptimizerType *                            OptimizerPointer;

  void Execute(itk::Object * object, const itk::EventObject & event)
  {

    if( typeid( event ) !=3D typeid( itk::IterationEvent ) )
      {
      return;
      }

    RegistrationPointer registration =3D
                            dynamic_cast<RegistrationPointer>( object );

    OptimizerPointer optimizer =3D dynamic_cast< OptimizerPointer >(=20
                       registration->GetOptimizer() );

    if ( registration->GetCurrentLevel() =3D=3D 0 )
      {
      optimizer->SetMaximumStepLength( 16.00 ); =20
      optimizer->SetMinimumStepLength( 2.5 );
      }
    else
      {
      optimizer->SetMaximumStepLength(=20
                optimizer->GetCurrentStepLength() );
      optimizer->SetMinimumStepLength(
                optimizer->GetMinimumStepLength() / 10.0 );
      }
  }

  void Execute(const itk::Object * , const itk::EventObject & )
    { return; }
};

#include "itkCommand.h"
class CommandIterationUpdate : public itk::Command=20
{
public:
  typedef  CommandIterationUpdate   Self;
  typedef  itk::Command             Superclass;
  typedef  itk::SmartPointer<Self>  Pointer;
//  itkNewMacro( Self );
protected:
  CommandIterationUpdate() {};
public:
  typedef   itk::RegularStepGradientDescentOptimizer     OptimizerType;
  typedef   const OptimizerType   *           OptimizerPointer;

  void Execute(itk::Object *caller, const itk::EventObject & event)
    {
      Execute( (const itk::Object *)caller, event);
    }

  void Execute(const itk::Object * object, const itk::EventObject & =
event)
    {
      OptimizerPointer optimizer =3D=20
        dynamic_cast< OptimizerPointer >( object );
      if( typeid( event ) !=3D typeid( itk::IterationEvent ) )
        {
        return;
        }
      std::cout << optimizer->GetCurrentIteration() << "   ";
      std::cout << optimizer->GetValue() << "   ";
      std::cout << optimizer->GetCurrentPosition() << std::endl;
    }
};

/////////////////////////////////////////////////////////////////////////=
////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD =3D IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////=
////
// CReg_TemplateDlg dialog

CReg_TemplateDlg::CReg_TemplateDlg(CWnd* pParent /*=3DNULL*/)
	: CDialog(CReg_TemplateDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CReg_TemplateDlg)
	m_dNumIt =3D 0.0;
	m_sRef =3D _T("");
	m_sTar =3D _T("");
	m_sTekst =3D _T("Laad de plaatjes");
	m_dOpt =3D 0.0;
	m_dRot =3D 0.0;
	m_dTransX =3D 0.0;
	m_dTransY =3D 0.0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in =
Win32
	m_hIcon =3D AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CReg_TemplateDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CReg_TemplateDlg)
	DDX_Text(pDX, IDC_NumIt, m_dNumIt);
	DDX_Text(pDX, IDC_EDIT1, m_sRef);
	DDX_Text(pDX, IDC_EDIT2, m_sTar);
	DDX_Text(pDX, IDC_EDIT3, m_sTekst);
	DDX_Text(pDX, IDC_Opt, m_dOpt);
	DDX_Text(pDX, IDC_Rot, m_dRot);
	DDX_Text(pDX, IDC_TransX, m_dTransX);
	DDX_Text(pDX, IDC_TransY, m_dTransY);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CReg_TemplateDlg, CDialog)
	//{{AFX_MSG_MAP(CReg_TemplateDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_Ref, OnRef)
	ON_BN_CLICKED(IDC_Tar, OnTar)
	ON_BN_CLICKED(IDC_Bereken, OnBereken)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////=
////
// CReg_TemplateDlg message handlers

BOOL CReg_TemplateDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) =3D=3D IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu =3D GetSystemMenu(FALSE);
	if (pSysMenu !=3D NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
=09
	// TODO: Add extra initialization here
=09
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CReg_TemplateDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) =3D=3D IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code =
below
//  to draw the icon.  For MFC applications using the document/view =
model,
//  this is automatically done for you by the framework.

void CReg_TemplateDlg::OnPaint()=20
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon =3D GetSystemMetrics(SM_CXICON);
		int cyIcon =3D GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x =3D (rect.Width() - cxIcon + 1) / 2;
		int y =3D (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user =
drags
//  the minimized window.
HCURSOR CReg_TemplateDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CReg_TemplateDlg::OnRef()=20
{
=09
=09
	CFileDialog m_OnRef(TRUE,NULL,NULL,NULL,
		"JPG Image Files (*.jpg)|*.jpg|All Files (*.*)|*.*||");
	try
	{
		if (m_OnRef.DoModal() =3D=3D IDOK)
		{
	=09
		m_sRef =3D m_OnRef.GetFileName();
		m_sTekst =3D m_OnRef.GetFileName() + " " + "is goed geladen";
		}	=09
	}
	catch(...)
		{m_sTekst =3D "Fout bij het laden van het referentieplaatje";}=20
		=09
	UpdateData(FALSE);
		//update the dialog
}

void CReg_TemplateDlg::OnTar()=20
{
	=09
	CFileDialog m_OnTar(TRUE,NULL,NULL,NULL,
		"JPG Image Files (*.jpg)|*.jpg|All Files (*.*)|*.*||");
	try
	{
		if (m_OnTar.DoModal() =3D=3D IDOK)
		{
=09
		m_sTar =3D m_OnTar.GetFileName();
		m_sTekst =3D m_OnTar.GetFileName() + " " + "is goed geladen";
		}	=09
	}
	catch(...)
		{m_sTekst =3D "Fout bij het laden van het referentieplaatje";}=20
		=09
	UpdateData(FALSE);
		//update the dialog=09
}

void CReg_TemplateDlg::OnBereken()=20
{

if (m_sRef.GetLength()>0)	=09
{
	if (m_sTar.GetLength()>0)
	{
		if (m_sRef=3D=3Dm_sTar)	=09
				{
				m_sTekst=3D "PAS OP: Beide Plaatjes zijn het zelfde!";

				UpdateData(FALSE);
				}
			else		=09
				{
				m_sTekst=3D "Beide Plaatjes zijn geladen!";

				UpdateData(FALSE);

//itk code

  const    unsigned int    Dimension =3D 2;
  typedef  unsigned short  PixelType;
 =20
  typedef itk::Image< PixelType, Dimension >  FixedImageType;
  typedef itk::Image< PixelType, Dimension >  MovingImageType;


  typedef   float     InternalPixelType;
  typedef itk::Image< InternalPixelType, Dimension > InternalImageType;

  typedef itk::TranslationTransform< double, Dimension > TransformType;
  typedef itk::RegularStepGradientDescentOptimizer       OptimizerType;
  typedef itk::LinearInterpolateImageFunction<=20
                                    InternalImageType,
                                    double             > =
InterpolatorType;
  typedef itk::MattesMutualInformationImageToImageMetric<=20
                                    InternalImageType,=20
                                    InternalImageType >   MetricType;
  typedef itk::MultiResolutionImageRegistrationMethod<=20
                                    InternalImageType,=20
                                    InternalImageType >   =
RegistrationType;

  typedef itk::MultiResolutionPyramidImageFilter<
                                    InternalImageType,
                                    InternalImageType >   =
FixedImagePyramidType;
  typedef itk::MultiResolutionPyramidImageFilter<
                                    InternalImageType,
                                    InternalImageType >   =
MovingImagePyramidType;

  TransformType::Pointer      transform     =3D TransformType::New();
  OptimizerType::Pointer      optimizer     =3D OptimizerType::New();
  InterpolatorType::Pointer   interpolator  =3D InterpolatorType::New();
  RegistrationType::Pointer   registration  =3D RegistrationType::New();
  MetricType::Pointer         metric        =3D MetricType::New();

  FixedImagePyramidType::Pointer fixedImagePyramid =3D=20
      FixedImagePyramidType::New();
  MovingImagePyramidType::Pointer movingImagePyramid =3D
      MovingImagePyramidType::New();

  registration->SetOptimizer(     optimizer     );
  registration->SetTransform(     transform     );
  registration->SetInterpolator(  interpolator  );
  registration->SetMetric( metric  );
  registration->SetFixedImagePyramid( fixedImagePyramid );
  registration->SetMovingImagePyramid( movingImagePyramid );
 =20

  typedef itk::ImageFileReader< FixedImageType  > FixedImageReaderType;
  typedef itk::ImageFileReader< MovingImageType > MovingImageReaderType;

  FixedImageReaderType::Pointer  fixedImageReader  =3D =
FixedImageReaderType::New();
  MovingImageReaderType::Pointer movingImageReader =3D =
MovingImageReaderType::New();

  fixedImageReader->SetFileName(  m_sRef );
  movingImageReader->SetFileName( m_sTar );


  typedef itk::CastImageFilter<=20
                        FixedImageType, InternalImageType > =
FixedCastFilterType;
  typedef itk::CastImageFilter<=20
                        MovingImageType, InternalImageType > =
MovingCastFilterType;

  FixedCastFilterType::Pointer fixedCaster   =3D =
FixedCastFilterType::New();
  MovingCastFilterType::Pointer movingCaster =3D =
MovingCastFilterType::New();

  fixedCaster->SetInput(  fixedImageReader->GetOutput() );
  movingCaster->SetInput( movingImageReader->GetOutput() );

  registration->SetFixedImage(    fixedCaster->GetOutput()    );
  registration->SetMovingImage(   movingCaster->GetOutput()   );

  fixedCaster->Update();

  registration->SetFixedImageRegion(=20
       fixedCaster->GetOutput()->GetBufferedRegion() );
  =20

  typedef RegistrationType::ParametersType ParametersType;
  ParametersType initialParameters( transform->GetNumberOfParameters() =
);

  initialParameters[0] =3D 0.0;  // Initial offset in mm along X
  initialParameters[1] =3D 0.0;  // Initial offset in mm along Y
 =20
  registration->SetInitialTransformParameters( initialParameters );

  metric->SetNumberOfHistogramBins( 20 );
  metric->SetNumberOfSpatialSamples( 10000 );

  optimizer->SetNumberOfIterations( 200 );

  CommandIterationUpdate:: observer =3D CommandIterationUpdate::New();
  optimizer->AddObserver( itk::IterationEvent(), observer );

  typedef RegistrationInterfaceCommand<RegistrationType> CommandType;
  CommandType::Pointer command =3D CommandType::New();
  registration->AddObserver( itk::IterationEvent(), command );

  registration->SetNumberOfLevels( 3 );

  try=20
    {=20
    registration->StartRegistration();=20
    }=20
  catch( itk::ExceptionObject & err )=20
    {=20
    std::cout << "ExceptionObject caught !" << std::endl;=20
    std::cout << err << std::endl;=20
   =20
    }=20

  ParametersType finalParameters =3D =
registration->GetLastTransformParameters();
 =20
  double TranslationAlongX =3D finalParameters[0];
  double TranslationAlongY =3D finalParameters[1];
 =20
  unsigned int numberOfIterations =3D optimizer->GetCurrentIteration();
 =20
  double bestValue =3D optimizer->GetValue();


  // Print out results
  std::cout << "Result =3D " << std::endl;
  std::cout << " Translation X =3D " << TranslationAlongX  << std::endl;
  std::cout << " Translation Y =3D " << TranslationAlongY  << std::endl;
  std::cout << " Iterations    =3D " << numberOfIterations << std::endl;
  std::cout << " Metric value  =3D " << bestValue          << std::endl;

  typedef itk::ResampleImageFilter<=20
                            MovingImageType,=20
                            FixedImageType >    ResampleFilterType;

  TransformType::Pointer finalTransform =3D TransformType::New();

  finalTransform->SetParameters( finalParameters );

  ResampleFilterType::Pointer resample =3D ResampleFilterType::New();

  resample->SetTransform( finalTransform );
  resample->SetInput( movingImageReader->GetOutput() );

  FixedImageType::Pointer fixedImage =3D fixedImageReader->GetOutput();

  resample->SetSize(    fixedImage->GetLargestPossibleRegion().GetSize() =
);
  resample->SetOutputOrigin(  fixedImage->GetOrigin() );
  resample->SetOutputSpacing( fixedImage->GetSpacing() );
  resample->SetDefaultPixelValue( 100 );

  typedef  unsigned char  OutputPixelType;

  typedef itk::Image< OutputPixelType, Dimension > OutputImageType;
 =20
  typedef itk::CastImageFilter<=20
                        FixedImageType,
                        OutputImageType > CastFilterType;
                   =20
  typedef itk::ImageFileWriter< OutputImageType >  WriterType;

  WriterType::Pointer      writer =3D  WriterType::New();
  CastFilterType::Pointer  caster =3D  CastFilterType::New();

  writer->SetFileName( m_sTar + "altered" );
 =20
  caster->SetInput( resample->GetOutput() );
  writer->SetInput( caster->GetOutput()   );
  writer->Update();

 =20

//einde itk code



				}
	}
	else					=09
	{
		m_sTekst=3D "E=E9n van de plaatjes is nog niet geladen";

		UpdateData(FALSE);
	}
}
else						=09
{
	m_sTekst=3D "E=E9n van de plaatjes is nog niet geladen";

	UpdateData(FALSE);
}


} 
------=_NextPart_000_0003_01C39D93.ECB2CCC0--