[Insight-users] Multi Resolution
Luis Ibanez
luis . ibanez at kitware . com
Tue, 28 Oct 2003 19:07:39 -0500
Hi Robert,
The itkNewMacro() is a very fundamental macro in ITK. If your
application produces compilation errors on this code, it certainly
means that your have a configuration problem.
Could you please post the CMakeLists.txt file that you are using for
your project ?
The original error message will also be helpful.
Thanks
Luis
-------------------------------------
Robert-Paul Buitendijk wrote:
> 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
>
>
> ------------------------------------------------------------------------
>
> // 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[] = __FILE__;
> #endif
>
> ///////////////////////////////////////////////////////
> template <typename TRegistration>
> class RegistrationInterfaceCommand : public itk::Command
> {
>
> 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 ) != typeid( itk::IterationEvent ) )
> {
> return;
> }
>
> RegistrationPointer registration =
> dynamic_cast<RegistrationPointer>( object );
>
> OptimizerPointer optimizer = dynamic_cast< OptimizerPointer >(
> registration->GetOptimizer() );
>
> if ( registration->GetCurrentLevel() == 0 )
> {
> optimizer->SetMaximumStepLength( 16.00 );
> optimizer->SetMinimumStepLength( 2.5 );
> }
> else
> {
> optimizer->SetMaximumStepLength(
> 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
> {
> 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 =
> dynamic_cast< OptimizerPointer >( object );
> if( typeid( event ) != 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 = 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 /*=NULL*/)
> : CDialog(CReg_TemplateDlg::IDD, pParent)
> {
> //{{AFX_DATA_INIT(CReg_TemplateDlg)
> m_dNumIt = 0.0;
> m_sRef = _T("");
> m_sTar = _T("");
> m_sTekst = _T("Laad de plaatjes");
> m_dOpt = 0.0;
> m_dRot = 0.0;
> m_dTransX = 0.0;
> m_dTransY = 0.0;
> //}}AFX_DATA_INIT
> // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
> m_hIcon = 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) == IDM_ABOUTBOX);
> ASSERT(IDM_ABOUTBOX < 0xF000);
>
> CMenu* pSysMenu = GetSystemMenu(FALSE);
> if (pSysMenu != 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
>
> // TODO: Add extra initialization here
>
> return TRUE; // return TRUE unless you set the focus to a control
> }
>
> void CReg_TemplateDlg::OnSysCommand(UINT nID, LPARAM lParam)
> {
> if ((nID & 0xFFF0) == 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()
> {
> if (IsIconic())
> {
> CPaintDC dc(this); // device context for painting
>
> SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
>
> // Center icon in client rectangle
> int cxIcon = GetSystemMetrics(SM_CXICON);
> int cyIcon = GetSystemMetrics(SM_CYICON);
> CRect rect;
> GetClientRect(&rect);
> int x = (rect.Width() - cxIcon + 1) / 2;
> int y = (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()
> {
>
>
> CFileDialog m_OnRef(TRUE,NULL,NULL,NULL,
> "JPG Image Files (*.jpg)|*.jpg|All Files (*.*)|*.*||");
> try
> {
> if (m_OnRef.DoModal() == IDOK)
> {
>
> m_sRef = m_OnRef.GetFileName();
> m_sTekst = m_OnRef.GetFileName() + " " + "is goed geladen";
> }
> }
> catch(...)
> {m_sTekst = "Fout bij het laden van het referentieplaatje";}
>
> UpdateData(FALSE);
> //update the dialog
> }
>
> void CReg_TemplateDlg::OnTar()
> {
>
> CFileDialog m_OnTar(TRUE,NULL,NULL,NULL,
> "JPG Image Files (*.jpg)|*.jpg|All Files (*.*)|*.*||");
> try
> {
> if (m_OnTar.DoModal() == IDOK)
> {
>
> m_sTar = m_OnTar.GetFileName();
> m_sTekst = m_OnTar.GetFileName() + " " + "is goed geladen";
> }
> }
> catch(...)
> {m_sTekst = "Fout bij het laden van het referentieplaatje";}
>
> UpdateData(FALSE);
> //update the dialog
> }
>
> void CReg_TemplateDlg::OnBereken()
> {
>
> if (m_sRef.GetLength()>0)
> {
> if (m_sTar.GetLength()>0)
> {
> if (m_sRef==m_sTar)
> {
> m_sTekst= "PAS OP: Beide Plaatjes zijn het zelfde!";
>
> UpdateData(FALSE);
> }
> else
> {
> m_sTekst= "Beide Plaatjes zijn geladen!";
>
> UpdateData(FALSE);
>
> //itk code
>
> const unsigned int Dimension = 2;
> typedef unsigned short PixelType;
>
> 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<
> InternalImageType,
> double > InterpolatorType;
> typedef itk::MattesMutualInformationImageToImageMetric<
> InternalImageType,
> InternalImageType > MetricType;
> typedef itk::MultiResolutionImageRegistrationMethod<
> InternalImageType,
> InternalImageType > RegistrationType;
>
> typedef itk::MultiResolutionPyramidImageFilter<
> InternalImageType,
> InternalImageType > FixedImagePyramidType;
> typedef itk::MultiResolutionPyramidImageFilter<
> InternalImageType,
> InternalImageType > MovingImagePyramidType;
>
> TransformType::Pointer transform = TransformType::New();
> OptimizerType::Pointer optimizer = OptimizerType::New();
> InterpolatorType::Pointer interpolator = InterpolatorType::New();
> RegistrationType::Pointer registration = RegistrationType::New();
> MetricType::Pointer metric = MetricType::New();
>
> FixedImagePyramidType::Pointer fixedImagePyramid =
> FixedImagePyramidType::New();
> MovingImagePyramidType::Pointer movingImagePyramid =
> MovingImagePyramidType::New();
>
> registration->SetOptimizer( optimizer );
> registration->SetTransform( transform );
> registration->SetInterpolator( interpolator );
> registration->SetMetric( metric );
> registration->SetFixedImagePyramid( fixedImagePyramid );
> registration->SetMovingImagePyramid( movingImagePyramid );
>
>
> typedef itk::ImageFileReader< FixedImageType > FixedImageReaderType;
> typedef itk::ImageFileReader< MovingImageType > MovingImageReaderType;
>
> FixedImageReaderType::Pointer fixedImageReader = FixedImageReaderType::New();
> MovingImageReaderType::Pointer movingImageReader = MovingImageReaderType::New();
>
> fixedImageReader->SetFileName( m_sRef );
> movingImageReader->SetFileName( m_sTar );
>
>
> typedef itk::CastImageFilter<
> FixedImageType, InternalImageType > FixedCastFilterType;
> typedef itk::CastImageFilter<
> MovingImageType, InternalImageType > MovingCastFilterType;
>
> FixedCastFilterType::Pointer fixedCaster = FixedCastFilterType::New();
> MovingCastFilterType::Pointer movingCaster = MovingCastFilterType::New();
>
> fixedCaster->SetInput( fixedImageReader->GetOutput() );
> movingCaster->SetInput( movingImageReader->GetOutput() );
>
> registration->SetFixedImage( fixedCaster->GetOutput() );
> registration->SetMovingImage( movingCaster->GetOutput() );
>
> fixedCaster->Update();
>
> registration->SetFixedImageRegion(
> fixedCaster->GetOutput()->GetBufferedRegion() );
>
>
> typedef RegistrationType::ParametersType ParametersType;
> ParametersType initialParameters( transform->GetNumberOfParameters() );
>
> initialParameters[0] = 0.0; // Initial offset in mm along X
> initialParameters[1] = 0.0; // Initial offset in mm along Y
>
> registration->SetInitialTransformParameters( initialParameters );
>
> metric->SetNumberOfHistogramBins( 20 );
> metric->SetNumberOfSpatialSamples( 10000 );
>
> optimizer->SetNumberOfIterations( 200 );
>
> CommandIterationUpdate:: observer = CommandIterationUpdate::New();
> optimizer->AddObserver( itk::IterationEvent(), observer );
>
> typedef RegistrationInterfaceCommand<RegistrationType> CommandType;
> CommandType::Pointer command = CommandType::New();
> registration->AddObserver( itk::IterationEvent(), command );
>
> registration->SetNumberOfLevels( 3 );
>
> try
> {
> registration->StartRegistration();
> }
> catch( itk::ExceptionObject & err )
> {
> std::cout << "ExceptionObject caught !" << std::endl;
> std::cout << err << std::endl;
>
> }
>
> ParametersType finalParameters = registration->GetLastTransformParameters();
>
> double TranslationAlongX = finalParameters[0];
> double TranslationAlongY = finalParameters[1];
>
> unsigned int numberOfIterations = optimizer->GetCurrentIteration();
>
> double bestValue = optimizer->GetValue();
>
>
> // Print out results
> std::cout << "Result = " << std::endl;
> std::cout << " Translation X = " << TranslationAlongX << std::endl;
> std::cout << " Translation Y = " << TranslationAlongY << std::endl;
> std::cout << " Iterations = " << numberOfIterations << std::endl;
> std::cout << " Metric value = " << bestValue << std::endl;
>
> typedef itk::ResampleImageFilter<
> MovingImageType,
> FixedImageType > ResampleFilterType;
>
> TransformType::Pointer finalTransform = TransformType::New();
>
> finalTransform->SetParameters( finalParameters );
>
> ResampleFilterType::Pointer resample = ResampleFilterType::New();
>
> resample->SetTransform( finalTransform );
> resample->SetInput( movingImageReader->GetOutput() );
>
> FixedImageType::Pointer fixedImage = 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;
>
> typedef itk::CastImageFilter<
> FixedImageType,
> OutputImageType > CastFilterType;
>
> typedef itk::ImageFileWriter< OutputImageType > WriterType;
>
> WriterType::Pointer writer = WriterType::New();
> CastFilterType::Pointer caster = CastFilterType::New();
>
> writer->SetFileName( m_sTar + "altered" );
>
> caster->SetInput( resample->GetOutput() );
> writer->SetInput( caster->GetOutput() );
> writer->Update();
>
>
>
> //einde itk code
>
>
>
> }
> }
> else
> {
> m_sTekst= "Eén van de plaatjes is nog niet geladen";
>
> UpdateData(FALSE);
> }
> }
> else
> {
> m_sTekst= "Eén van de plaatjes is nog niet geladen";
>
> UpdateData(FALSE);
> }
>
>
> }