Hi Mathieu :<br><br>You need to override the method AllocateOutputs() and manually allocate each of the outputs.<br><br>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.
<br><br>The following illustrates the pipeline for threaded filters : <br>&nbsp;&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1) Allocate the output buffer&nbsp; --- (You must override this if the output types are different&quot;)<br>&nbsp;&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2) Call BeforeThreadedGenerateData()
<br>&nbsp;&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3) Spawn threads, calling ThreadedGenerateData() in each thread.<br>&nbsp;&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4) Call AfterThreadedGenerateData()<br><br><br>For details, please see itkImageSource.h <br><br>-- <br>Karthik Krishnan<br>R&amp;D Engineer,
<br>Kitware Inc.
<br><br><div><span class="gmail_quote">On 8/22/07, <b class="gmail_sendername">Mathieu Malaterre</b> &lt;<a href="mailto:mathieu.malaterre@gmail.com">mathieu.malaterre@gmail.com</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hello,<br><br><br>&nbsp;&nbsp;I am trying to write an ImagetoImageFilter that would produce two<br>types of output, two output are the same type as the input and the<br>last one (3rd) is floating point type. For some reason the filter is
<br>crashing. I suspect that the allocation did not go correctly for the<br>third output (second type).<br><br>Prototype is:<br><br>template &lt;class TImage&gt;<br>class ITK_EXPORT MyImageFilter:<br>&nbsp;&nbsp;&nbsp;&nbsp;public ImageToImageFilter&lt;TImage, TImage&gt;
<br>{<br>...<br>&nbsp;&nbsp;/** Get the error image output of this process object.&nbsp;&nbsp;*/<br>&nbsp;&nbsp;typedef Image&lt;float,3&gt; ErrorImageType;<br>&nbsp;&nbsp;ErrorImageType::Pointer GetErrorOutput()<br>&nbsp;&nbsp;{ return static_cast&lt;ErrorImageType *&gt;(this-&gt;ProcessObject::GetOutput(2)); }
<br><br>&nbsp;&nbsp;/** Set the error image output of this process object.&nbsp;&nbsp;*/<br>&nbsp;&nbsp;void SetErrorOutput(ErrorImageType *output)<br>&nbsp;&nbsp;{ this-&gt;SetNthOutput(2, output); }<br>...<br>}<br><br>And in the ThreadedGenerateData :<br><br>
ThreadedGenerateData(const ImageRegionType&amp; outputRegionForThread,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int threadId)<br>{<br>&nbsp;&nbsp;ErrorImageType::Pointer outputErrorPtr = this-&gt;GetErrorOutput();<br>&nbsp;&nbsp;OutputIterator outIt(outputPtr, outputRegionForThread);
<br>&nbsp;&nbsp;...<br>&nbsp;&nbsp;//outIt.Set( residual ); // everything works if I comment out the &#39;Set&#39;<br>&nbsp;&nbsp;...<br>}<br><br>Finally here is the cstor:<br><br>::MyImageFilter()<br>{<br>&nbsp;&nbsp;this-&gt;ProcessObject::SetNumberOfRequiredOutputs(3);
<br>&nbsp;&nbsp;typename TImage::Pointer output = TImage::New();<br>&nbsp;&nbsp;this-&gt;ProcessObject::SetNthOutput(1, output.GetPointer());<br>&nbsp;&nbsp;ErrorImageType::Pointer e1 = ErrorImageType::New();<br>&nbsp;&nbsp;this-&gt;ProcessObject::SetNthOutput(2, 
e1.GetPointer());<br>}<br><br>Comments welcome,<br><br><br>--<br>Mathieu<br>_______________________________________________<br>Insight-users mailing list<br><a href="mailto:Insight-users@itk.org">Insight-users@itk.org</a>
<br><a href="http://www.itk.org/mailman/listinfo/insight-users">http://www.itk.org/mailman/listinfo/insight-users</a><br></blockquote></div>