[Insight-users] ImageToImageFilter: producing two different types of output

Mathieu Malaterre mathieu.malaterre at gmail.com
Wed Aug 22 10:44:49 EDT 2007


Sweet !!

Thanks that was it. I was reading itk::ProcessObject, and miss all of
the ImageSource documentation.

Thanks
-Mathieu

On 8/22/07, Karthik Krishnan <karthik.krishnan at kitware.com> wrote:
> Hi Mathieu :
>
> You need to override the method AllocateOutputs() and manually allocate each
> of the outputs.
>
> The implementation of ImageToImageFilter is such that if the filter is not
> threaded, (as in it overrides GenerateData()), the implementation is
> responsible for allocating the outputs. If the filter is threaded (provides
> ThreadedGenerateData()), the superclass automatically allocates the outputs
> for you and ends up allocating all outputs of the type specified as the
> output image template parameter. In your case, the outputs are of different
> types and hence a memory access crash.
>
> The following illustrates the pipeline for threaded filters :
>    *      1) Allocate the output buffer  --- (You must override this if the
> output types are different")
>    *      2) Call BeforeThreadedGenerateData()
>    *      3) Spawn threads, calling ThreadedGenerateData() in each thread.
>    *      4) Call AfterThreadedGenerateData()
>
>
> For details, please see itkImageSource.h
>
> --
> Karthik Krishnan
> R&D Engineer,
> Kitware Inc.
>
>
> On 8/22/07, Mathieu Malaterre <mathieu.malaterre at gmail.com> wrote:
> >
> > Hello,
> >
> >
> >   I am trying to write an ImagetoImageFilter that would produce two
> > types of output, two output are the same type as the input and the
> > last one (3rd) is floating point type. For some reason the filter is
> > crashing. I suspect that the allocation did not go correctly for the
> > third output (second type).
> >
> > Prototype is:
> >
> > template <class TImage>
> > class ITK_EXPORT MyImageFilter:
> >     public ImageToImageFilter<TImage, TImage>
> > {
> > ...
> >   /** Get the error image output of this process object.  */
> >   typedef Image<float,3> ErrorImageType;
> >   ErrorImageType::Pointer GetErrorOutput()
> >   { return static_cast<ErrorImageType
> *>(this->ProcessObject::GetOutput(2)); }
> >
> >   /** Set the error image output of this process object.  */
> >   void SetErrorOutput(ErrorImageType *output)
> >   { this->SetNthOutput(2, output); }
> > ...
> > }
> >
> > And in the ThreadedGenerateData :
> >
> > ThreadedGenerateData(const ImageRegionType& outputRegionForThread,
> >                        int threadId)
> > {
> >   ErrorImageType::Pointer outputErrorPtr = this->GetErrorOutput();
> >   OutputIterator outIt(outputPtr, outputRegionForThread);
> >   ...
> >   //outIt.Set( residual ); // everything works if I comment out the 'Set'
> >   ...
> > }
> >
> > Finally here is the cstor:
> >
> > ::MyImageFilter()
> > {
> >   this->ProcessObject::SetNumberOfRequiredOutputs(3);
> >   typename TImage::Pointer output = TImage::New();
> >   this->ProcessObject::SetNthOutput(1,
> output.GetPointer());
> >   ErrorImageType::Pointer e1 = ErrorImageType::New();
> >   this->ProcessObject::SetNthOutput(2, e1.GetPointer());
> > }
> >
> > Comments welcome,
> >
> >
> > --
> > Mathieu
> > _______________________________________________
> > Insight-users mailing list
> > Insight-users at itk.org
> > http://www.itk.org/mailman/listinfo/insight-users
> >
>


-- 
Mathieu


More information about the Insight-users mailing list