[Insight-users] problem: my own filter doesn't return!!!!

Renaud Isabelle renauisa at yahoo.fr
Thu Nov 3 09:52:58 EST 2005


Hi guys, 
 
I am trying to write my own filter taking 2 inputs of the same type. For the moment, to test my filter, I am just trying to compute the mean and variance of each input in the GenerateData() method. However, at execution, my filter doesn't return!!!!
 
Could you tell me what I am missing? 
 
Isabelle
 
/*=========================================================================
  Module:    itkCorrelationCoefficientsImageFilter.txx
  Language:  C++
  Date:      
=========================================================================*/
#ifndef __itkCorrelationCoefficientsImageFilter_txx
#define __itkCorrelationCoefficientsImageFilter_txx
 
#include "itkCorrelationCoefficientsImageFilter.h"
#include "itkProgressReporter.h"
#include "itkImageRegionIterator.h"
#include "itkImageRegionConstIterator.h"
#include "stdafx.h" //AfxMessageBox, CString
 
namespace itk {
 
template <class TInputImage, class TOutputImage>
CorrelationCoefficientsImageFilter<TInputImage, TOutputImage>
::CorrelationCoefficientsImageFilter()
{
  this->SetNumberOfRequiredInputs(2);
}

/* Connect the 1st input */
template <class TInputImage, class TOutputImage>
void
CorrelationCoefficientsImageFilter<TInputImage, TOutputImage>
::SetInput1( const TInputImage * image1 ) 
{
  // Process object is not const-correct so the const casting is required.
  SetNthInput(0, const_cast<TInputImage *>( image1 ));
}
template <class TInputImage, class TOutputImage>
const TInputImage *
CorrelationCoefficientsImageFilter<TInputImage, TOutputImage>
::GetInput1()
{
  return this->GetInput(0);
}
/* Connect the 2nd input */
template <class TInputImage, class TOutputImage>
void
CorrelationCoefficientsImageFilter<TInputImage, TOutputImage>
::SetInput2( const TInputImage * image2 ) 
{
  // Process object is not const-correct so the const casting is required.
  SetNthInput(1, const_cast<TInputImage *>( image2 ));
}
template <class TInputImage, class TOutputImage>
const TInputImage *
CorrelationCoefficientsImageFilter<TInputImage, TOutputImage>
::GetInput2()
{
  return this->GetInput(1);
}
 
template <class TInputImage, class TOutputImage>
void 
CorrelationCoefficientsImageFilter<TInputImage, TOutputImage>
::GenerateInputRequestedRegion()
{
  // call the superclass' implementation of this method
  Superclass::GenerateInputRequestedRegion();//ImageToImageFilter
  // get pointers to the inputs
  Input1ImagePointer inputPtr1
    = const_cast< TInputImage*>( this->GetInput(0) );
  Input2ImagePointer inputPtr2
    = const_cast< TInputImage*>( this->GetInput(1) );
  if ( !inputPtr1 || !inputPtr2 ) return;
  // We need to configure the inputs such that all the data is available.
  inputPtr1->SetRequestedRegion(inputPtr1->GetLargestPossibleRegion());
  inputPtr2->SetRequestedRegion(inputPtr2->GetLargestPossibleRegion());
}

template <class TInputImage, class TOutputImage>
void 
CorrelationCoefficientsImageFilter<TInputImage, TOutputImage>
::EnlargeOutputRequestedRegion(DataObject *)
{
  this->GetOutput()
    ->SetRequestedRegion( this->GetOutput()->GetLargestPossibleRegion() );
}
 
template <class TInputImage, class TOutputImage>
float
CorrelationCoefficientsImageFilter<TInputImage, TOutputImage>
::Mean(TInputImage* input) const
{
    ImageRegionConstIteratorType it(input, input->GetRequestedRegion());
    it.GoToBegin();
 unsigned int NumberOfPixels = 0;
 float mean = 0.0;
    while( !it.IsAtEnd() ) 
    {
    mean += ((float)it.Get());
 NumberOfPixels++;
    ++it;
    }
    mean /= (float)NumberOfPixels;
    return mean;
}
 
template <class TInputImage, class TOutputImage>
float
CorrelationCoefficientsImageFilter<TInputImage, TOutputImage>
::Variance(TInputImage* input, float mean) const
{
    ImageRegionConstIteratorType it(input, input->GetRequestedRegion());
 
    it.GoToBegin();
 float sum = 0.0;
 float tmp; 
    while( !it.IsAtEnd() ) 
 {
     tmp = ((float)it.Get()) - mean;
    sum += (tmp)*(tmp);
 }
 return sum;
}
 
template <class TInputImage, class TOutputImage>
void
CorrelationCoefficientsImageFilter<TInputImage, TOutputImage>
::GenerateData()
{
  this->AllocateOutputs();
  // get pointers to the inputs
  Input1ImagePointer inputPtr1
    = const_cast< TInputImage*>( this->GetInput(0) );
  Input2ImagePointer inputPtr2
    = const_cast< TInputImage*>( this->GetInput(1) );
  OutputImagePointer outputPtr = this->GetOutput(0);  
  
  // the inputs must have the same size
  if ( this->GetInput1()->GetRequestedRegion().GetSize() != this->GetInput2()->GetRequestedRegion().GetSize() )
    {
    AfxMessageBox("The two input images must have the same size.");
    }
  float meanX = this->Mean(inputPtr1);
  float meanY = this->Mean(inputPtr2);
  TRACE("means:%f, %f\n", meanX,meanY); //this is ok
 
  float varX = this->Variance(inputPtr1, meanX);
  float varY = this->Variance(inputPtr2, meanY);
 
   // iterator for the first input
  ImageRegionConstIteratorType inputIt1(this->GetInput1(), this->GetInput1()->GetRequestedRegion());
  
  // iterator for the 2nd input
  ImageRegionConstIteratorType inputIt2(this->GetInput2(), this->GetInput2()->GetRequestedRegion());
  
  // iterator for output image
  typedef ImageRegionIterator< TOutputImage > ImageRegionIteratorType;
  ImageRegionIteratorType outputIt( this->GetOutput(), this->GetOutput()->GetRequestedRegion() );
 
  inputIt1.GoToBegin();
  inputIt2.GoToBegin();
  outputIt.GoToBegin();

  InputImagePixelType denom = sqrt( varX * varY );
  if( denom == 0.0) 
   AfxMessageBox("Coefficient de Correlation non defini, variance nulle!\n");
  //else
  //{
   // Set up the progress reporter 
   ProgressReporter progress(this, 0, this->GetInput1()->GetRequestedRegion().GetNumberOfPixels());
   while( !inputIt1.IsAtEnd() ) 
   {
  ++inputIt1;
  progress.CompletedPixel(); // potential exception thrown here
   }  
  //}
}
  
}// end namespace itk
#endif

		
---------------------------------
 Appel audio GRATUIT partout dans le monde avec le nouveau Yahoo! Messenger
 Téléchargez le ici !  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://public.kitware.com/pipermail/insight-users/attachments/20051103/3ccb38ad/attachment.html


More information about the Insight-users mailing list