[Insight-developers] own ImageToImageFilter doesn't work

Michael Siegesmund TheSmashingPumpkin at web.de
Thu Aug 13 05:48:56 EDT 2009


Hi itk developers,

I tried to write my own filter class (inherited from ImageToImageFilter), which encapsulates the functionality of ConnectedThresholdImageFilter.cxx from examples/segmentation.
So I took CompositeFilterExample.cxx (from examples/filters) as a template and replaced the core code with the one of ConnectedThresholdImageFilter.cxx. 
Afterwards I figured out, that I can use my new class for filtering a png file (input=imageFileReader) but if I want to process a CT-image from a dicom reader (which works fine with other filters!) I always get a black screen. 
And it doesn't matter whether I use an imageviewer or a filewriter for output.
The problem sounds like I forgot an important thing to do, for example initialize something. I tried hard, but couldn't find the bug.

Do someone has an idea or a working example?

Thanks in advance 



/////////////////////////////the header///////////////////////////////////
#ifndef __ConnectedThresholdFilter_h
#define __ConnectedThresholdFilter_h

#ifdef _USE_ITK


#include "itkImageToImageFilter.h"
#include "itkCurvatureFlowImageFilter.h"
#include "itkConnectedThresholdImageFilter.h"
#include "itkCastImageFilter.h"
#include "itkNumericTraits.h"


namespace itk {

template <class TImageType, class TOutputType>
class ITK_EXPORT ConnectedThresholdFilter : public ImageToImageFilter<TImageType, TOutputType>
{
public:

    typedef ConnectedThresholdFilter                 Self;
    typedef ImageToImageFilter<TImageType,TOutputType>  Superclass;
    typedef SmartPointer<Self>                          Pointer;
    typedef SmartPointer<const Self>                    ConstPointer;

    itkNewMacro(Self);
    itkTypeMacro(ConnectedThresholdFilter, ImageToImageFilter);

    void PrintSelf( std::ostream& os, Indent indent ) const;

protected:

    ConnectedThresholdFilter();

protected:

    typedef itk::Image< float, 3> FloatType;

    typedef itk::CurvatureFlowImageFilter< TImageType, FloatType >      CurvatureFlowImageFilterType;
    typedef itk::ConnectedThresholdImageFilter< FloatType, FloatType >  ConnectedFilterType;
    typedef itk::CastImageFilter< FloatType, TOutputType >              CastingFilterType;

    void GenerateData();

private:

  ConnectedThresholdFilter(Self&);   // intentionally not implemented
  void operator=(const Self&);          // intentionally not implemented

  typename CurvatureFlowImageFilterType::Pointer smoothing;
  typename ConnectedFilterType::Pointer connectedThreshold;
  typename CastingFilterType::Pointer caster;

};

} /* namespace itk */

#ifndef ITK_MANUAL_INSTANTIATION
#include "ConnectedThresholdFilter.cpp"
#endif

#endif _USE_ITK

#endif



//////////the class/////////////////////////////////////////
#ifndef __ConnectedThresholdFilter_cpp
#define __ConnectedThresholdFilter_cpp

#ifdef _USE_ITK

#include "ConnectedThresholdFilter.h"


namespace itk 
{

template <class TImageType, class TOutputType>
ConnectedThresholdFilter<TImageType, TOutputType>::ConnectedThresholdFilter()
{
    smoothing = CurvatureFlowImageFilterType::New();
    connectedThreshold = ConnectedFilterType::New();
    caster = CastingFilterType::New();

    connectedThreshold->SetInput( smoothing->GetOutput() );
    caster->SetInput( connectedThreshold->GetOutput() );

    smoothing->SetNumberOfIterations( 5 );
    smoothing->SetTimeStep( 0.125 );

    connectedThreshold->SetLower(  0  );
    connectedThreshold->SetUpper(  800  );
    connectedThreshold->SetReplaceValue( 255 );

    TImageType::IndexType  index;
    index[0] = 250;
    index[1] = 250;
    connectedThreshold->SetSeed( index );
}

template <class TImageType, class TOutputType>
void ConnectedThresholdFilter<TImageType, TOutputType>::GenerateData()
{
    smoothing->SetInput(this->GetInput());
    caster->GraftOutput( this->GetOutput() );
    caster->Update();
    this->GraftOutput( caster->GetOutput() );
}


template <class TImageType, class TOutputType>
void ConnectedThresholdFilter<TImageType, TOutputType>::PrintSelf( std::ostream& os, Indent indent ) const
{
  Superclass::PrintSelf(os,indent);
  //not implemented yet
}

} /* end namespace itk */


#endif _USE_ITK

#endif



///////this is how i call it //////////////////////////
    typedef itk::Image<short, 3>                        ImageType;
    typedef itk::Image<unsigned char, 3>                OutputType;
    typedef itk::ImageFileWriter<OutputType>             WriterType;
    typedef itk::ConnectedThresholdFilter<ImageType,OutputType> FilterType1;
    
    WriterType::Pointer writer = WriterType::New();
    FilterType1::Pointer filter = FilterType1::New();

    //the input works!
    filter->SetInput(m_Dicomreader->GetITKOutput() ); 
    writer->SetInput( filter->GetOutput() );
    writer->SetFileName( "c://example.png" );

    try
    {
        writer->Update(); // so here I got a black image
    }
    catch ( itk::ExceptionObject e )
    {
        std::cerr << "Error: " << e << std::endl;
    }

______________________________________________________
GRATIS für alle WEB.DE-Nutzer: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://movieflat.web.de



More information about the Insight-developers mailing list