<div dir="ltr"><div><div>Hello all,<br><br></div>Would anyone have a WORKING c++/source file that successfully generates/acquires the inverse displacement field from a Demon&#39;s non-rigid registration field?<br><br></div>
Many users have been helping me in achieving this, though I&#39;m worried that I&#39;m losing some of the finer points in my code implementation.. this is what I have so far, and it compiles, but the program always crashes at the same point, after a negative index is created for my new field...<br>
<br>any help is much appreciated.. my c++ code is below:<br><br>#include &quot;itkInverseDisplacementFieldImageFilter.h&quot;<br>#include &quot;itkImageFileWriter.h&quot;<br>#include &quot;itkImageFileReader.h&quot;<br>#include &quot;itkFilterWatcher.h&quot;<br>
#include &quot;itkVectorScaleImageFilter.h&quot;<br>#include &quot;itkImage.h&quot;<br>#include &quot;itkArray.h&quot;<br><br>int main( int argc, char * argv[] )<br>{<br><br>  if( argc &lt; 4 )<br>    {<br>    std::cerr &lt;&lt; &quot;Missing Parameters &quot; &lt;&lt; std::endl;<br>
    std::cerr &lt;&lt; &quot;Usage: &quot; &lt;&lt; argv[0];<br>    std::cerr &lt;&lt; &quot;inputDemonsField subSampFactor outputInvertedField&quot; &lt;&lt; std::endl;<br>    return EXIT_FAILURE;<br>    }<br><br>  const     unsigned int Dimension = 3;<br>
  typedef   float VectorComponentType;<br><br>  typedef   itk::Vector&lt; VectorComponentType, Dimension &gt; VectorType;<br><br>  typedef itk::Image&lt; VectorType,  Dimension &gt; DisplacementFieldType;<br><br>  typedef itk::Array&lt; VectorType &gt; ArrayType;<br>
<br>  typedef itk::InverseDisplacementFieldImageFilter&lt;<br>    DisplacementFieldType,<br>    DisplacementFieldType<br>    &gt;  FilterType;<br><br>  FilterType::Pointer filter = FilterType::New();<br><br>  FilterWatcher watcher(filter);<br>
<br>  // Read a DisplacementField<br>  typedef itk::ImageFileReader&lt;  DisplacementFieldType  &gt; ReaderType;<br><br>  ReaderType::Pointer reader = ReaderType::New();<br><br>  reader-&gt;SetFileName( argv[1] );<br><br>
  reader-&gt;Update();<br><br><br>  // Obtaining an input displacement field<br>  DisplacementFieldType::Pointer field = reader-&gt;GetOutput();<br><br>  DisplacementFieldType::RegionType inputRegion;<br>  DisplacementFieldType::SizeType inputSize;<br>
  DisplacementFieldType::DirectionType inputDirection;<br>  DisplacementFieldType::PointType inputOrigin;<br>  DisplacementFieldType::SpacingType inputSpacing;<br><br>  inputRegion = field-&gt;GetLargestPossibleRegion();<br>
  inputSize = field-&gt;GetLargestPossibleRegion().GetSize();<br>  inputDirection =  field-&gt;GetDirection();<br>  inputOrigin =  field-&gt;GetOrigin();<br>  inputSpacing =  field-&gt;GetSpacing();<br>  inputRegion.SetIndex(field-&gt;GetLargestPossibleRegion().GetIndex());<br>
<br>  DisplacementFieldType::Pointer outField = DisplacementFieldType::New();<br><br> <br><br><br>   itk::ImageRegionIteratorWithIndex&lt; DisplacementFieldType &gt; it( field, inputRegion );<br><br>   filter-&gt;SetOutputSpacing( field-&gt;GetSpacing() );<br>
   filter-&gt;SetOutputOrigin( field-&gt;GetOrigin() );<br>   filter-&gt;SetSize( inputSize );<br><br>  filter-&gt;SetInput( field );<br>  filter-&gt;SetSubsamplingFactor( atof(argv[2]) );<br><br>  try<br>    {<br>    filter-&gt;UpdateLargestPossibleRegion();<br>
    }<br>  catch( itk::ExceptionObject &amp; excp )<br>    {<br>    std::cerr &lt;&lt; &quot;Exception thrown &quot; &lt;&lt; std::endl;<br>    std::cerr &lt;&lt; excp &lt;&lt; std::endl;<br>    }<br><br>  // Make template to ensure proper indexing of new def field<br>
<br>  DisplacementFieldType::Pointer outField = filter-&gt;GetOutput();<br>  DisplacementFieldType::RegionType outputRegion;<br><br>   outputRegion.SetIndex( inputRegion.GetIndex() );<br><br>  // Write an image for regression testing<br>
  typedef itk::ImageFileWriter&lt;  DisplacementFieldType  &gt; WriterType;<br><br>  WriterType::Pointer writer = WriterType::New();<br><br>  outField-&gt;Update();<br>  writer-&gt;SetInput (outField );<br>  writer-&gt;SetFileName( argv[3] );<br>
<br>  try<br>    {<br>    writer-&gt;Update();<br>    }<br>  catch( itk::ExceptionObject &amp; excp )<br>    {<br>    std::cerr &lt;&lt; &quot;Exception thrown by writer&quot; &lt;&lt; std::endl;<br>    std::cerr &lt;&lt; excp &lt;&lt; std::endl;<br>
    return EXIT_FAILURE;<br>    }<br><br><br><br><br>  // Now, test for loop invariant (acts as filter validation)<br>  // f^-1(f(p1) + p1 ) - f(p1)  = 0<br>  it.GoToBegin();<br>  while( !it.IsAtEnd() )<br>    {<br><br>    DisplacementFieldType::PointType p1;<br>
    field-&gt;TransformIndexToPhysicalPoint(it.GetIndex(), p1);<br><br>    DisplacementFieldType::PixelType fp1 = it.Get();<br><br>    DisplacementFieldType::PointType p2;<br>    p2[0] = p1[0] + fp1[0];<br>    p2[1] = p1[1] + fp1[1];<br>
    p2[2] = p1[2] + fp1[2];<br><br>    DisplacementFieldType::IndexType id2;<br>    outField-&gt;TransformPhysicalPointToIndex(p2,id2);<br>    //filter-&gt;GetOutput()-&gt;TransformPhysicalPointToIndex(p2,id2);<br>    std::cerr &lt;&lt; &quot;hurrah&quot; &lt;&lt; id2 &lt;&lt; std::endl;<br>
    DisplacementFieldType::PixelType fp2 = outField-&gt;GetPixel(id2);<br>    //DisplacementFieldType::PixelType fp2 = filter-&gt;GetOutput()-&gt;GetPixel(id2);<br>    std::cerr &lt;&lt; &quot;hurrah2&quot; &lt;&lt; std::endl;<br>
    <br><br>    if(vcl_abs(fp2[0] + fp1[0]) &gt; 100.000<br>       || vcl_abs(fp2[1] + fp1[1]) &gt; 100.000<br>       || vcl_abs(fp2[2] + fp1[2]) &gt; 100.000)<br>      {<br>      std::cerr&lt;&lt;&quot;Loop invariant not satisfied for index &quot;&lt;&lt;it.GetIndex()&lt;&lt;&quot; : f^-1(f(p1) + p1 ) + f(p1)  = 0&quot;&lt;&lt;   std::endl;<br>
      std::cerr&lt;&lt;&quot;f(p1) = &quot;&lt;&lt;fp1&lt;&lt;std::endl;<br>      std::cerr&lt;&lt;&quot;f^-1(f(p1) + p1 ) = &quot;&lt;&lt;fp2&lt;&lt;std::endl;<br>      std::cerr&lt;&lt;&quot;diff: &quot;&lt;&lt;fp1[0]+fp2[0]&lt;&lt;&quot;, &quot;&lt;&lt;fp1[1]+fp2[1]&lt;&lt;&quot;, &quot;&lt;&lt;fp1[2]+fp2[2]&lt;&lt;std::endl;<br>
    //  return EXIT_FAILURE;<br>      }<br>    ++it;<br>    }<br><br>  return EXIT_SUCCESS;<br><br>}<br clear="all"><div><div><div><br>-- <br>Tim Bhatnagar<br>PhD Candidate<br>Orthopaedic Injury Biomechanics Group<br>Department of Mechanical Engineering<br>
University of British Columbia<br><br>Rm 5000 - 818 West 10th Ave.<br>Vancouver, BC<br>Canada<br>V5Z 1M9<br><br>Ph: (604) 675-8845<br>Fax: (604) 675-8820<br>Web: <a href="http://oibg.mech.ubc.ca" target="_blank">oibg.mech.ubc.ca</a><br>

</div></div></div></div>