[Insight-users] Urgenthelp with Observer class to store image sequence

Luis Ibanez luis.ibanez@kitware.com
Fri May 14 01:05:09 EDT 2004


Hi Jayant,

The datapipeline will not execute in the order that
your organized your code. What matters is the topology
of the pipeline.

If you have

    FilterB->SetInput(   FilterA->GetOutput()   );

the FilterA will always execute before Filter B.

What you should do is to draw your pipeline in a piece
of paper, like:


FilterA -->  Filter B ---+
                          |
                       FilterQ -----> Output
                          |
FilterK -->  Filter M ---+


Then, simply follow the flow of data from the sources
to the consumers.


Note that if you are running the pipeline several
times, only the filter that have any change in parameters
will be executed again, and will force the filters
downstream to execute too.

---

When you say that

    "I'm not able to get the Observer to work"

what do you mean ?


   A) it seg.faults ?
   B) it throws exceptions ?
   C) its Execute() method is not invoked ?
   D) it prints always zero progress ?
   C) it prints garbage .... ?



Please be more precise,


   Thanks


    Luis



-----------------------
Jayant Chauhan wrote:

> Hey Fellas,
>  
> Seems I am a lil bit confused about the sequential flow of control in ITK.
>  
> When I include all my image reading and filter executions in the main(), 
> it works, but when I create a function which does all this as a member 
> of my class, then I am not able to get the Observer to work. My code is 
> as follows, I am using the GeodesicActiveContourLevelSetImageFilter to 
> segment my image, and want to store all the contours, ie, the 
> zerocrossing, so I add the observer to my zerocrossing filter,ie, m_contour.
>  
> ALso it seems that only when I add my geodesicfilter to the observer are 
> the contours stored and not when I add m_contour. ie.
>   m_observer->SetCallbackFunction( this,
>       & firstFilter::GetContourAfterIteration );
>  *m_geodesic*->AddObserver(itk::IterationEvent(), m_observer); //works
>  *m_contour*->AddObserver(itk::IterationEvent(), m_observer); //doesnt work
>  
>  my code:
>  
> *_firstFilter.h_*
> *__* 
> #ifndef _FIRST_FILTER_H
> #define _FIRST_FILTER_H
> #include "itkImage.h"
> #include "itkImageFileReader.h"
> #include "itkImageFileWriter.h"
> #include "itkGeodesicActiveContourLevelSetImageFilter.h"
> #include "itkCurvatureAnisotropicDiffusionImageFilter.h"
> #include "itkGradientMagnitudeRecursiveGaussianImageFilter.h"
> #include "itkSigmoidImageFilter.h"
> #include "itkBinaryThresholdImageFilter.h"
> #include "itkGeodesicActiveContourLevelSetFunction.h"
> #include "itkSegmentationLevelSetImageFilter.h"
> #include "itkCastImageFilter.h"
> #include "itkRescaleIntensityImageFilter.h"
> #include "itkZeroCrossingImageFilter.h"
> #include "itkCommand.h"
> #include "itkWeightedAddImageFilter.h"
> 
> template <class TInputImage, class TInternalImage, class TOutputImage>
> class firstFilter{
>  
>  public:
>   typedef itk::CurvatureAnisotropicDiffusionImageFilter<TInputImage,
>      TInternalImage> SmoothingFilterType;
>   typedef itk::GradientMagnitudeRecursiveGaussianImageFilter<TInternalImage,
>      TInternalImage> GradientFilterType;
>   typedef itk::SigmoidImageFilter<TInternalImage,
>      TInternalImage> SigmoidFilterType;
>   typedef itk::GeodesicActiveContourLevelSetImageFilter<TInternalImage,
>         TInternalImage> GACFilterType;
>  
>   typedef itk::BinaryThresholdImageFilter<TInternalImage,
>      TOutputImage> TFilterType;
>   
>   typedef itk::ZeroCrossingImageFilter<TOutputImage,
>      TInternalImage> ZeroFilterType;
>   
>   typedef itk::WeightedAddImageFilter<TInternalImage,
>      TInternalImage, TInternalImage> WAddFilterType;
>   
>   typedef itk::SimpleMemberCommand< firstFilter >  ObserverType;
> 
>     SmoothingFilterType::Pointer m_smoothing;
>   GradientFilterType::Pointer m_gradient;
>   SigmoidFilterType::Pointer m_sigmoid;
>   SmoothingFilterType::Pointer m_framesmoothing;
>   GradientFilterType::Pointer m_framegradient;
>   SigmoidFilterType::Pointer m_framesigmoid;
>   GACFilterType::Pointer m_geodesic;
>   TFilterType::Pointer m_threshold;
>   ZeroFilterType::Pointer m_contour;
>   WAddFilterType::Pointer m_wadd;
>   ObserverType::Pointer m_observer;
>   
>   firstFilter();
>   void ExecuteGAC(char* , char* , char* , char* , char* , char* , char*,
>     int , float , float , float ,
>     float , float , float );
>   void GetContourAfterIteration();
> };
> template <class TInputImage, class TInternalImage, class TOutputImage>
> void firstFilter<TInputImage, TInternalImage, TOutputImage>
> ::ExecuteGAC(char* featureimage, char* frameimage, char* initlevelset,
>     char* sigmoidresult, char* framesigmoidresult, char* gacresult, 
> char* threshresult,
>     int no_iter, float sigma, float alpha, float beta,
>     float ascale, float cscale, float pscale){
>  typedef itk::ImageFileReader<TInputImage> InputImageReader;
>  typedef itk::ImageFileReader<TInternalImage> InternalImageReader;
>  
>  typedef itk::ImageFileWriter<TInternalImage> InternalImageWriter;
>  typedef itk::ImageFileWriter<TOutputImage> OutputImageWriter;
>  std::cout<<"I am inside the function "<<std::endl;
>  InputImageReader::Pointer reader1 = InputImageReader::New();
>  InputImageReader::Pointer reader2 = InputImageReader::New();
>  
>  InternalImageReader::Pointer internalreader1 = InternalImageReader::New();
>  InternalImageWriter::Pointer internalwriter1 = InternalImageWriter::New();
>  
>  OutputImageWriter::Pointer sigmoidwriter = OutputImageWriter::New();
>  OutputImageWriter::Pointer framesigmoidwriter = OutputImageWriter::New();
>  OutputImageWriter::Pointer gacwriter = OutputImageWriter::New();
>  OutputImageWriter::Pointer twriter = OutputImageWriter::New();
>  
>  reader1->SetFileName(featureimage);
>  reader2->SetFileName(frameimage);
>  internalreader1->SetFileName(initlevelset);
>  sigmoidwriter->SetFileName(sigmoidresult);
>  framesigmoidwriter->SetFileName(framesigmoidresult);
>   
>  m_smoothing->SetInput(reader1->GetOutput());
>  m_framesmoothing->SetInput(reader2->GetOutput());
>  m_gradient->SetSigma(sigma);
>  m_gradient->SetInput(m_smoothing->GetOutput());
>  m_framegradient->SetSigma(sigma);
>  m_framegradient->SetInput(m_framesmoothing->GetOutput());
>  m_sigmoid->SetInput(m_gradient->GetOutput());
>  m_sigmoid->SetAlpha( alpha );
>     m_sigmoid->SetBeta( beta );
>  m_framesigmoid->SetInput(m_framegradient->GetOutput());
>  m_framesigmoid->SetAlpha( alpha );
>     m_framesigmoid->SetBeta( alpha );
>  m_wadd->SetInput1(m_sigmoid->GetOutput());
>  m_wadd->SetInput2(m_framesigmoid->GetOutput());
>  m_geodesic->SetPropagationScaling( pscale );
>  m_geodesic->SetCurvatureScaling( cscale );
>     m_geodesic->SetAdvectionScaling( ascale );
>  
>  m_geodesic->SetMaximumRMSError(0.02);
>  m_geodesic->SetNumberOfIterations( no_iter );
>  m_geodesic->SetReverseExpansionDirection(false);
>  m_geodesic->SetInput(internalreader1->GetOutput());
>  m_geodesic->SetFeatureImage(m_wadd->GetOutput());
>  gacwriter->SetFileName(gacresult);
>  
>  twriter->SetFileName(threshresult);
>  m_threshold->SetInput(m_geodesic->GetOutput());
>  typedef itk::RescaleIntensityImageFilter<TInternalImage,
>     TOutputImage> CastFilterType;
>  CastFilterType::Pointer sigmoidcastfilter = CastFilterType::New();
>  CastFilterType::Pointer gaccastfilter = CastFilterType::New();
>  
>  sigmoidcastfilter->SetInput(m_sigmoid->GetOutput());
>  gaccastfilter->SetInput(m_geodesic->GetOutput());
>  
>  sigmoidcastfilter->SetOutputMinimum(0);
>  sigmoidcastfilter->SetOutputMaximum(255);
>  
>  gaccastfilter->SetOutputMinimum(0);
>  gaccastfilter->SetOutputMaximum(255);
>  sigmoidwriter->SetInput(sigmoidcastfilter->GetOutput());
>  gacwriter->SetInput(gaccastfilter->GetOutput());
>  twriter->SetInput(m_threshold->GetOutput());
>  CastFilterType::Pointer zerocastfilter = CastFilterType::New();
>  zerocastfilter->SetOutputMinimum(0);
>  zerocastfilter->SetOutputMaximum(255);
>  OutputImageWriter::Pointer zcwriter = OutputImageWriter::New();
>  m_contour->SetInput(m_threshold->GetOutput());
>  zerocastfilter->SetInput(m_contour->GetOutput());
>  zcwriter->SetFileName("zerocrossing.png");
>  zcwriter->SetInput(zerocastfilter->GetOutput());
>  
>  try{
>   gacwriter->Update();
>   twriter->Update();
>   sigmoidwriter->Update();
>   zcwriter->Update();
>  }
>  catch (itk::ExceptionObject & err)
>  {
>   std::cerr<<"Exception caught "<<std::endl;
>   std::cerr<<err<<std::endl;
>  }
> 
> }
> 
> template <class TInputImage, class TInternalImage, class TOutputImage>
> void firstFilter<TInputImage, TInternalImage, TOutputImage>
> ::GetContourAfterIteration(){
>  
>  static unsigned int iterationCounter = 0;
>  std::cout<<"Hey do I get a change "<<std::endl;
>  typedef itk::ImageFileWriter<TOutputImage> ImageWriter;
>  typedef itk::RescaleIntensityImageFilter<TInternalImage,
>     TOutputImage> CastFilterType;
>  ImageWriter::Pointer contour_writer = ImageWriter::New();
>  CastFilterType::Pointer zccast = CastFilterType::New();
>  zccast->SetOutputMinimum(0);
>  zccast->SetOutputMaximum(255);
> 
>  char* contourfile;
>  contourfile = (char*)malloc(sizeof(char)*50);
>  
>  if ( (iterationCounter != 0) && (iterationCounter%25 == 0) ){
>   
>   sprintf(contourfile,"%d%s",iterationCounter,".png");
>   std::cout << "No. elpased iterations: " << 
> m_geodesic->GetElapsedIterations() << std::endl;
>   std::cout << "RMS change: " << m_geodesic->GetRMSChange() << std::endl;
>   contour_writer->SetFileName(contourfile);
>   zccast->SetInput(m_contour->GetOutput());
>   contour_writer->SetInput(zccast->GetOutput());
>   try{
>    contour_writer->Update();
>   }
>   catch (itk::ExceptionObject & err){
>    std::cerr<<"ITK Exception caught :"<<err<<std::endl;
>   }
>   
>  }
>  else{
>   std::cout<<"Iteration counter :"<<iterationCounter<<std::endl;
>  }
>  iterationCounter++;
> }
> 
> template <class TInputImage, class TInternalImage, class TOutputImage>
> firstFilter<TInputImage, TInternalImage, TOutputImage>
> ::firstFilter(){
>  m_smoothing = SmoothingFilterType::New();
>  m_framesmoothing = SmoothingFilterType::New();
>  m_smoothing->SetTimeStep(0.125);
>  m_smoothing->SetNumberOfIterations(5);
>     m_smoothing->SetConductanceParameter(3.0);
>  m_framesmoothing->SetTimeStep(0.125);
>  m_framesmoothing->SetNumberOfIterations(5);
>     m_framesmoothing->SetConductanceParameter(3.0);
>  m_gradient = GradientFilterType::New();
>  m_framegradient = GradientFilterType::New();
>     m_sigmoid = SigmoidFilterType::New();
>  m_framesigmoid = SigmoidFilterType::New();
>     m_sigmoid->SetOutputMinimum(0.0);
>     m_sigmoid->SetOutputMaximum(1.0);
>  m_framesigmoid->SetOutputMinimum(0.0);
>     m_framesigmoid->SetOutputMaximum(1.0);
>  m_wadd = WAddFilterType::New();
>  m_wadd->SetAlpha(0.5);
>  
>  m_geodesic = GACFilterType::New();
>  m_geodesic->SetIsoSurfaceValue(127.0);
>  
>  m_threshold = TFilterType::New();
>  
>  m_threshold->SetLowerThreshold(-1000.0);
>  m_threshold->SetUpperThreshold(0.0);
>  
>  m_threshold->SetOutsideValue(0);
>  m_threshold->SetInsideValue(255);
>  m_contour =  ZeroFilterType::New();
>  m_observer = ObserverType::New();
>  m_observer->SetCallbackFunction( this,
>       & firstFilter::GetContourAfterIteration );
>  m_geodesic->AddObserver(itk::IterationEvent(), m_observer);
> }
>  
> #endif
>  
> --------------------------------------------------------------------------------------------------------------
>  
> *_firstFilter.cxx_*
> #include "firstFilter.h"
> int main(int argc, char** argv){
>  typedef unsigned char InputPixelType;
>  typedef float InternalPixelType;
>  typedef unsigned char OutputPixelType;
>  typedef itk::Image<InputPixelType, 2> InputImageType;
>  typedef itk::Image<InternalPixelType, 2> InternalImageType;
>  typedef itk::Image<OutputPixelType, 2> OutputImageType;
>  typedef itk::ImageFileReader<InputImageType> InputImageReader;
>  typedef itk::ImageFileReader<InternalImageType> InternalImageReader;
>  
>  typedef itk::ImageFileWriter<InternalImageType> InternalImageWriter;
>  typedef itk::ImageFileWriter<OutputImageType> OutputImageWriter;
>  if (argc < 15){
>   std::cerr<<"Incorrect usage, Correct usage being : "<<std::endl;
>   std::cerr<<"firstFilter.exe FeatureImage FrameImage 
> InitialLevelSetImage ";
>   std::cerr<<"sigmoidresult framesigmoidresult geodesicresult 
> thresholdresult ";
>   std::cerr<<"NoOfIterations  sigma alpha beta pscale cscale ascale ";
>   std::cerr<<"--------------"<<std::endl;
>   exit(1);
>   
>  }
>  firstFilter<InputImageType, InternalImageType, OutputImageType>  ff;
>  ff.ExecuteGAC(argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], 
> argv[7], atoi(argv[8]),
>      atof(argv[9]), atof(argv[10]), atof(argv[11]), atof(argv[12]), 
> atof(argv[13]),
>      atof(argv[14])) ;
> 
>     return 1;
> }
> ----------------------------------------------------------------------------------------------------------
>  
> Any suggestions please, have to finish this asap
>  
> with regards
>  
> Jayant
> 
> ------------------------------------------------------------------------
> Send flowers in 24 hours! At MSN Shopping. 
> <http://g.msn.com/8HMAENIN/2743??PS=47575> 
> _______________________________________________ Insight-users mailing 
> list Insight-users@itk.org 
> http://www.itk.org/mailman/listinfo/insight-users






More information about the Insight-users mailing list