<br>Hi Lars,<br><br>You raise a very good point here.<br><br>We should indeed add a concept check for &quot;SameDimension&quot;,<br>although, the conversion from dimension N to M (with M&gt;N)<br>is still a valid operation.<br>
<br>We may have to create a new Concept Type for this particular case.<br><br>Something like<br><br>             SameDimensionOrLarger&lt;&gt;<br><br><br><br>    Regards,<br><br><br>          Luis<br><br><br>-----------------------------------------------------------------------------------<br>
<div class="gmail_quote">On Sun, Apr 18, 2010 at 12:45 PM, Lars Friedrich Lars <span dir="ltr">&lt;<a href="mailto:lars-friedrich@gmx.net">lars-friedrich@gmx.net</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi again,<br>
<br>
just a further thought regarding the casting-thing:<br>
<br>
Maybe an ITK concept with &quot;SameDimension&quot; should be inserted to prevent users from misusing (maybe that&#39;s the wrong word referring to the previous doc) this class.<br>
<div class="im"><br>
regards,<br>
<br>
lars<br>
<br>
<br>
-------- Original-Nachricht --------<br>
&gt; Datum: Sun, 18 Apr 2010 12:16:02 -0400<br>
&gt; Von: Luis Ibanez &lt;<a href="mailto:luis.ibanez@kitware.com">luis.ibanez@kitware.com</a>&gt;<br>
&gt; An: Lars Friedrich Lars &lt;<a href="mailto:lars-friedrich@gmx.net">lars-friedrich@gmx.net</a>&gt;<br>
&gt; CC: <a href="mailto:Insight-users@itk.org">Insight-users@itk.org</a>, Bradley Lowekamp &lt;<a href="mailto:blowekamp@mail.nih.gov">blowekamp@mail.nih.gov</a>&gt;<br>
&gt; Betreff: Re: [Insight-users] CastImageFilter BUG???<br>
<br>
</div><div><div></div><div class="h5">&gt; Hi Lars,<br>
&gt;<br>
&gt; It looks like this is a case in which:<br>
&gt;<br>
&gt;<br>
&gt; 1) The documentation incorrectly recommended a misuse of the class.<br>
&gt; 2) The misuse was later detected as a buggy behavior<br>
&gt; 3) The buggy behavior has been identified and fixed.<br>
&gt;<br>
&gt;<br>
&gt; The &quot;feature&quot; of reducing 3D to 2D dimensions only<br>
&gt; works in this filter due to a happy coincidence resulting<br>
&gt; from the way in which the image iterators are implemented.<br>
&gt;<br>
&gt; Note for example, that the extracted slice can only be the<br>
&gt; one corresponding to the slowest changing index.<br>
&gt;<br>
&gt;<br>
&gt; Instead of using the CastImageFilter, you should use the<br>
&gt;<br>
&gt;                           ExtractImageFilter<br>
&gt;<br>
&gt; which is explicitly designed for dealing with dimensionality<br>
&gt; reduction between the input image and the output image.<br>
&gt;<br>
&gt; For example, extracting one 2D slice from a 3D image,<br>
&gt; as illustrated in the Example:<br>
&gt;<br>
&gt;     Insight/Examples/IO/ImageReadExtractWrite.cxx<br>
&gt;<br>
&gt; Note that the ExtractImageFilter will do both the pixel<br>
&gt; casting and the dimensionality reduction.<br>
&gt;<br>
&gt; In the meantime we have fixed the documentation<br>
&gt; of the itkCastImageFilter.<br>
&gt; <a href="http://public.kitware.com/cgi-bin/viewcvs.cgi/Code/BasicFilters/itkCastImageFilter.h?root=Insight&amp;view=log" target="_blank">http://public.kitware.com/cgi-bin/viewcvs.cgi/Code/BasicFilters/itkCastImageFilter.h?root=Insight&amp;view=log</a><br>

&gt;<br>
&gt; with the following diff:<br>
&gt; <a href="http://public.kitware.com/cgi-bin/viewcvs.cgi/Code/BasicFilters/itkCastImageFilter.h?root=Insight&amp;r1=1.16&amp;r2=1.17" target="_blank">http://public.kitware.com/cgi-bin/viewcvs.cgi/Code/BasicFilters/itkCastImageFilter.h?root=Insight&amp;r1=1.16&amp;r2=1.17</a><br>

&gt;<br>
&gt;<br>
&gt;<br>
&gt;       Regards,<br>
&gt;<br>
&gt;<br>
&gt;            Luis<br>
&gt;<br>
&gt;<br>
&gt; ----------------------------------------------------------------------------------------------------<br>
&gt; On Sun, Apr 18, 2010 at 10:52 AM, Lars Friedrich Lars &lt;<br>
&gt; <a href="mailto:lars-friedrich@gmx.net">lars-friedrich@gmx.net</a>&gt; wrote:<br>
&gt;<br>
&gt; &gt; Hello,<br>
&gt; &gt;<br>
&gt; &gt; as can be retrieved from itk::CastImageFilter&#39;s Doxygen-entry, this<br>
&gt; filter<br>
&gt; &gt; can obviously be used for casting a 3D-volume to a 2D-slice<br>
&gt; &gt; (dimension-reduction).<br>
&gt; &gt;<br>
&gt; &gt; &quot;... If you attempt to cast an image to a lower dimension, the first<br>
&gt; &gt; &quot;slice&quot; (or line or volume) will be extracted. ...&quot;<br>
&gt; &gt;<br>
&gt; &gt; I&#39;ve been using this functionality for a while in an application.<br>
&gt; However,<br>
&gt; &gt; today I changed something in the code (at completely different<br>
&gt; &gt; code-fragment) and from this point my application started to throw<br>
&gt; segfaults<br>
&gt; &gt; when executing it.<br>
&gt; &gt; I traced the bug (in current ITK-CVS-version) and found out that casting<br>
&gt; &gt; from a 3D-slice to a 2D-image causes the segfault. Inspecting the code<br>
&gt; of<br>
&gt; &gt; itk::CastImageFilter and its superclass itk::UnaryFunctorImageFilter<br>
&gt; showed<br>
&gt; &gt; me that the code lines 97-112 of itkUnaryFunctorImageFilter.txx seem to<br>
&gt; &gt; cause the segfault:<br>
&gt; &gt;<br>
&gt; &gt;  for (i=0; i &lt; Superclass::InputImageDimension; ++i)<br>
&gt; &gt;      {<br>
&gt; &gt;      outputSpacing[i] = inputSpacing[i];<br>
&gt; &gt;      outputOrigin[i] = inputOrigin[i];<br>
&gt; &gt;      for (j=0; j &lt; Superclass::OutputImageDimension; j++)<br>
&gt; &gt;        {<br>
&gt; &gt;        if (j &lt; Superclass::InputImageDimension)<br>
&gt; &gt;          {<br>
&gt; &gt;          outputDirection[j][i] = inputDirection[j][i];<br>
&gt; &gt;          }<br>
&gt; &gt;        else<br>
&gt; &gt;          {<br>
&gt; &gt;          outputDirection[j][i] = 0.0;<br>
&gt; &gt;          }<br>
&gt; &gt;        }<br>
&gt; &gt;      }<br>
&gt; &gt;<br>
&gt; &gt; When casting from 3D to 2D the InputImageDimension=3, but the<br>
&gt; &gt; OutputImageDimension=2. Writing to the 3rd elements of outputSpacing,<br>
&gt; &gt; outputOrigin and outputDirection causes the memory-violation.<br>
&gt; &gt;<br>
&gt; &gt; Please correct me if I am wrong.<br>
&gt; &gt;<br>
&gt; &gt; However, it is scary and funny at the same time that my application has<br>
&gt; &gt; worked without throwing segfaults and without any obvious errors for a<br>
&gt; few<br>
&gt; &gt; weeks ...<br>
&gt; &gt;<br>
&gt; &gt; The example program below shows the bug (if it is a bug).<br>
&gt; &gt;<br>
&gt; &gt; regards,<br>
&gt; &gt;<br>
&gt; &gt; lars<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt; // TestReductionCasting.cxx<br>
&gt; &gt;<br>
&gt; &gt; #include &lt;iostream&gt;<br>
&gt; &gt; #include &lt;stdlib.h&gt;<br>
&gt; &gt; #include &lt;time.h&gt;<br>
&gt; &gt;<br>
&gt; &gt; #include &lt;itkCastImageFilter.h&gt;<br>
&gt; &gt; #include &lt;itkImage.h&gt;<br>
&gt; &gt; #include &lt;itkImageRegionIterator.h&gt;<br>
&gt; &gt;<br>
&gt; &gt; int main(int argc, char *argv[])<br>
&gt; &gt; {<br>
&gt; &gt;  std::cerr &lt;&lt; &quot;Constructing 3D image ...&quot;;<br>
&gt; &gt;  typedef itk::Image&lt;float, 3&gt; Image3DType;<br>
&gt; &gt;  typedef itk::ImageRegionIterator&lt;Image3DType&gt; IteratorType;<br>
&gt; &gt;<br>
&gt; &gt;  Image3DType::RegionType region;<br>
&gt; &gt;  Image3DType::SizeType size;<br>
&gt; &gt;  Image3DType::IndexType start;<br>
&gt; &gt;  Image3DType::SpacingType spacing;<br>
&gt; &gt;  Image3DType::PointType origin;<br>
&gt; &gt;  Image3DType::DirectionType orientation;<br>
&gt; &gt;<br>
&gt; &gt;  size[0] = 100; size[1] = 200; size[2] = 1;<br>
&gt; &gt;  start.Fill(0);<br>
&gt; &gt;  region.SetIndex(start);<br>
&gt; &gt;  region.SetSize(size);<br>
&gt; &gt;  spacing[0] = 0.25; spacing[1] = 0.5; spacing[2] = 1;<br>
&gt; &gt;  origin[0] = 100; origin[1] = 200; origin[2] = -50;<br>
&gt; &gt;  orientation.SetIdentity();<br>
&gt; &gt;  Image3DType::Pointer image = Image3DType::New();<br>
&gt; &gt;  image-&gt;SetRegions(region);<br>
&gt; &gt;  image-&gt;SetSpacing(spacing);<br>
&gt; &gt;  image-&gt;SetOrigin(origin);<br>
&gt; &gt;  image-&gt;SetDirection(orientation);<br>
&gt; &gt;  image-&gt;Allocate();<br>
&gt; &gt;<br>
&gt; &gt;  srand(time(NULL));<br>
&gt; &gt;  IteratorType it(image, region);<br>
&gt; &gt;  for (it.GoToBegin(); !it.IsAtEnd(); ++it)<br>
&gt; &gt;  {<br>
&gt; &gt;    it.Set(static_cast&lt;float&gt;(rand() % 100));<br>
&gt; &gt;  }<br>
&gt; &gt;  std::cerr &lt;&lt; &quot; DONE\n&quot;;<br>
&gt; &gt;<br>
&gt; &gt;  std::cerr &lt;&lt; &quot;Casting to 2D image (cast image filter) ...&quot;;<br>
&gt; &gt;  typedef itk::Image&lt;unsigned char, 2&gt; Image2DType;<br>
&gt; &gt;  typedef itk::CastImageFilter&lt;Image3DType, Image2DType&gt; CasterType;<br>
&gt; &gt;<br>
&gt; &gt;  CasterType::Pointer caster = CasterType::New();<br>
&gt; &gt;  caster-&gt;SetInput(image);<br>
&gt; &gt;  try<br>
&gt; &gt;  {<br>
&gt; &gt;    caster-&gt;Update(); // CAUSES SEGFAULT!!!<br>
&gt; &gt;    std::cerr &lt;&lt; &quot; DONE\n&quot;;<br>
&gt; &gt;  }<br>
&gt; &gt;  catch (itk::ExceptionObject &amp;e)<br>
&gt; &gt;  {<br>
&gt; &gt;    std::cerr &lt;&lt; &quot; ERROR\n&quot;;<br>
&gt; &gt;  }<br>
&gt; &gt;<br>
&gt; &gt;  return EXIT_SUCCESS;<br>
&gt; &gt; }<br>
&gt; &gt;<br>
&gt; &gt; --<br>
&gt; &gt; GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!<br>
&gt; &gt; Jetzt freischalten unter <a href="http://portal.gmx.net/de/go/maxdome01" target="_blank">http://portal.gmx.net/de/go/maxdome01</a><br>
&gt; &gt; _____________________________________<br>
&gt; &gt; Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
&gt; &gt;<br>
&gt; &gt; Visit other Kitware open-source projects at<br>
&gt; &gt; <a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
&gt; &gt;<br>
&gt; &gt; Kitware offers ITK Training Courses, for more information visit:<br>
&gt; &gt; <a href="http://www.kitware.com/products/protraining.html" target="_blank">http://www.kitware.com/products/protraining.html</a><br>
&gt; &gt;<br>
&gt; &gt; Please keep messages on-topic and check the ITK FAQ at:<br>
&gt; &gt; <a href="http://www.itk.org/Wiki/ITK_FAQ" target="_blank">http://www.itk.org/Wiki/ITK_FAQ</a><br>
&gt; &gt;<br>
&gt; &gt; Follow this link to subscribe/unsubscribe:<br>
&gt; &gt; <a href="http://www.itk.org/mailman/listinfo/insight-users" target="_blank">http://www.itk.org/mailman/listinfo/insight-users</a><br>
&gt; &gt;<br>
<br>
</div></div>--<br>
<div><div></div><div class="h5">GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!<br>
Jetzt freischalten unter <a href="http://portal.gmx.net/de/go/maxdome01" target="_blank">http://portal.gmx.net/de/go/maxdome01</a><br>
</div></div></blockquote></div><br>