[Insight-users] My filters do not release their output data correctly

Cyril cyril.mory at philips.com
Fri Apr 19 09:00:01 EDT 2013


Ok, quick update : One of the filters I've written, rtkDivergenceImageFilter,
does release its data correctly. A more complex filter utilizing it,
rtkMotionWeightedDivergenceImageFilter, does not. Here is the code of this
second filter :

---------------------------rtkMotionWeightedDivergenceImageFilter.h------------------------
#ifndef __rtkMotionWeightedDivergenceImageFilter_h
#define __rtkMotionWeightedDivergenceImageFilter_h

#include "itkImageToImageFilter.h"
#include "itkCovariantVector.h"
#include "rtkDivergenceImageFilter.h"

namespace rtk
{

template< typename TInputImage,
         typename TRealType = float,
         typename TOutputImage = itk::Image< TRealType,
                                            TInputImage::ImageDimension >,
          typename TMotionWeightsMap = itk::Image< TRealType,
TInputImage::ImageDimension - 1>
         >
class ITK_EXPORT MotionWeightedDivergenceImageFilter:
        public itk::ImageToImageFilter< TInputImage, TOutputImage >
{
public:
    /** Standard class typedefs. */
    typedef MotionWeightedDivergenceImageFilter             Self;
    typedef itk::ImageToImageFilter<TInputImage, TOutputImage> Superclass;
    typedef itk::SmartPointer< Self >        Pointer;

    /** Method for creation through the object factory. */
    itkNewMacro(Self)

    /** Run-time type information (and related methods). */
    itkTypeMacro(MotionWeightedDivergenceImageFilter,
itk::ImageToImageFilter)

    /** The 3D volume by which the last component of the 4D input image will
be multiplied  */
    void SetMotionWeightsMap(const TMotionWeightsMap* Map);

    typedef rtk::DivergenceImageFilter<TInputImage> DivergenceFilterType;

protected:
    MotionWeightedDivergenceImageFilter();
    ~MotionWeightedDivergenceImageFilter(){}

    typename TMotionWeightsMap::Pointer GetMotionWeightsMap();

    /** Does the real work. */
    virtual void GenerateData();

    /** Member pointers to the filters used internally (for convenience)*/
    typename DivergenceFilterType::Pointer                      
m_DivergenceFilter;

private:
    MotionWeightedDivergenceImageFilter(const Self &); //purposely not
implemented
    void operator=(const Self &);  //purposely not implemented

};
} //namespace RTK


#ifndef ITK_MANUAL_INSTANTIATION
#include "rtkMotionWeightedDivergenceImageFilter.txx"
#endif

#endif



---------------------------rtkMotionWeightedDivergenceImageFilter.txx------------------------
#ifndef __rtkMotionWeightedDivergenceImageFilter_hxx
#define __rtkMotionWeightedDivergenceImageFilter_hxx

#include "rtkMotionWeightedDivergenceImageFilter.h"
#include "itkImageRegionIterator.h"

namespace rtk
{
//
// Constructor
//
template< typename TInputImage, typename TRealType, typename TOutputImage,
typename  TMotionWeightsMap>
MotionWeightedDivergenceImageFilter< TInputImage, TRealType, TOutputImage,
TMotionWeightsMap >
::MotionWeightedDivergenceImageFilter()
{
    this->SetNumberOfRequiredInputs(2);
    m_DivergenceFilter = DivergenceFilterType::New();
}

template< typename TInputImage, typename TRealType, typename TOutputImage,
typename  TMotionWeightsMap>
void
MotionWeightedDivergenceImageFilter< TInputImage, TRealType, TOutputImage,
TMotionWeightsMap >
::SetMotionWeightsMap(const TMotionWeightsMap* Map)
{
    this->SetNthInput(1, const_cast<TMotionWeightsMap*>(Map));
}

template< typename TInputImage, typename TRealType, typename TOutputImage,
typename  TMotionWeightsMap>
typename TMotionWeightsMap::Pointer
MotionWeightedDivergenceImageFilter< TInputImage, TRealType, TOutputImage,
TMotionWeightsMap >
::GetMotionWeightsMap()
{
    return static_cast< TMotionWeightsMap * >
            ( this->itk::ProcessObject::GetInput(1) );
}

template< typename TInputImage, typename TRealType, typename TOutputImage,
typename  TMotionWeightsMap>
void
MotionWeightedDivergenceImageFilter< TInputImage, TRealType, TOutputImage,
TMotionWeightsMap >
::GenerateData()
{
    typename TInputImage::RegionType Largest =
this->GetInput()->GetLargestPossibleRegion();
    typename TMotionWeightsMap::RegionType MWMLargest =
this->GetMotionWeightsMap()->GetLargestPossibleRegion();
    typename TInputImage::Pointer intermediatePointer = TInputImage::New();

    intermediatePointer->SetRegions(Largest);
    intermediatePointer->Allocate();
    intermediatePointer->SetSpacing(this->GetInput(0)->GetSpacing());
    intermediatePointer->SetOrigin(this->GetInput(0)->GetOrigin());

    itk::ImageRegionIterator<TInputImage>
intermediateIterator(intermediatePointer, Largest);
    itk::ImageRegionConstIterator<TInputImage>
inputIterator(this->GetInput(), Largest);
    itk::ImageRegionIterator<TMotionWeightsMap>
weightsMapIterator(this->GetMotionWeightsMap(), MWMLargest);

    int Dimension = this->GetInput()->GetImageDimension();

    itk::CovariantVector< TRealType, TInputImage::ImageDimension > temp;
    for (int PhaseNumber=0; PhaseNumber<Largest.GetSize()[Dimension-1];
PhaseNumber++)
    {
        weightsMapIterator.GoToBegin();
        while(!weightsMapIterator.IsAtEnd())
        {
            temp = inputIterator.Get();
            temp[Dimension-1] = temp[Dimension-1] *
weightsMapIterator.Get();
            intermediateIterator.Set(temp);
            ++inputIterator;
            ++intermediateIterator;
            ++weightsMapIterator;
        }
    }
    m_DivergenceFilter->SetInput(intermediatePointer);
    m_DivergenceFilter->Update();
    this->GraftOutput(m_DivergenceFilter->GetOutput());
    intermediatePointer->ReleaseData(); //Seems to have no effect
}

} // end namespace rtk

#endif




--
View this message in context: http://itk-users.7.n7.nabble.com/My-filters-do-not-release-their-output-data-correctly-tp31213p31219.html
Sent from the ITK - Users mailing list archive at Nabble.com.


More information about the Insight-users mailing list