<br>Hi Amardeep,<br><br>Thanks for posting the source code and the image that you are using.<br><br>We reformatted your code and were able to reproduce what you are reporting:<br><br>That the directions of the gradient (of the gradient magnitude) <b>seem</b> to be <br>
pointing in the wrong direction.<br><br>Please find attached the two versions that we reformat of your code.<br><br>After a lot of head scratching we found that the apparent problem, is that<br>your image has DIRECTION cosines that are not those of a unit matrix.<br>
<br>The direction cosines of your image actually are:<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -1 0 0 <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 -1 0 <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 0 1<br><br>Which means that the X and Y axis are reflected with respect to<br>what you would usually expect.<br>
<br>Note that ITK gradient filters *DO* take direction into account <br>when they are computing the gradient vectors. since ITK 3.10,<br>we have set the itkImage to consider Direction by default.<br><br>Therefore, at the end, this is simply a case of using inadecuate <br>
Visualization tools. The viewer that you are using doesn&#39;t take<br>the image direction into account.<br><br>For your convenience, we have added the explicit calls for the<br>gradient to take or not to take direction into account. <br>
Please note however, that ignoring the direction will result<br>in inconsistent computations.<br><br><br>Also, you will better understand what is going on, if you <br>use an asymmetric image. The square that you are using<br>
doen&#39;t help to disambiguate between the +Y and -Y directions<br>of the axis.<br><br><br><br>&nbsp; Regards,<br><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Luis<br><br>-----------------------------------------------------<br><div class="gmail_quote">On Thu, Feb 12, 2009 at 7:58 AM, Amardeep Singh <span dir="ltr">&lt;<a href="mailto:amar.singh@gmx.de">amar.singh@gmx.de</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;">Dear ITK Users<br>
<br>
I am still having problems with the gradient vector flow and so I went a step back and looked at the gradients<br>
of my image. At the moment, I am just dealing with a synthetic image of a square (see attachment).<br>
I calculate the gradient magnitude with the itkGradientMagnitudeImageFilter and then the gradient with the<br>
itkGradientImageFilter. I save the output of the gradient filter as a *.vtk image file and visualize it in Paraview.<br>
I find that the vectors are pointing outwards, whereas I would expect them to point inwards, to the middle of<br>
the edge.<br>
Could anyone tell me whats going wrong, please? Or is everything correct and I misunderstood something?<br>
Thank you for any help.<br>
<br>
Best regards<br>
Amar<br>
<br>
P.S. I have attached my code below:<br>
<br>
/*<br>
* gvfOnSyntheticImage.cc<br>
*<br>
* &nbsp;Created on: 11-Feb-2009<br>
* &nbsp; &nbsp;*/<br>
<br>
<br>
<br>
//ITK Headers<br>
#include &quot;itkImage.h&quot;<br>
<br>
#include &quot;itkImageFileReader.h&quot;<br>
#include &quot;itkImageFileWriter.h&quot;<br>
#include &quot;itkCovariantVector.h&quot;<br>
#include &quot;itkGradientVectorFlowImageFilter.h&quot;<br>
//#include &quot;itkLaplacianImageFilter.h&quot;<br>
#include &quot;itkGradientMagnitudeRecursiveGaussianImageFilter.h&quot;<br>
#include &quot;itkGradientMagnitudeImageFilter.h&quot;<br>
#include &quot;itkGradientImageFilter.h&quot;<br>
#include &quot;itkCastImageFilter.h&quot;<br>
<br>
#include &quot;vtkImageWriter.h&quot;<br>
<br>
#include &lt;iostream&gt;<br>
<br>
<br>
int main( int argc, char *argv[] )<br>
{<br>
<br>
 &nbsp; &nbsp; typedef &nbsp; float &nbsp;InternalPixelType;<br>
 &nbsp; &nbsp; typedef float OutputPixelType;<br>
 &nbsp; &nbsp; typedef unsigned char BinaryPixelType;<br>
<br>
 &nbsp; &nbsp; const &nbsp; &nbsp; unsigned int &nbsp; &nbsp;Dimension = 3;<br>
<br>
 &nbsp; &nbsp; typedef itk::Image&lt; InternalPixelType, Dimension &gt; &nbsp;InternalImageType;<br>
 &nbsp; &nbsp; typedef itk::Image&lt; OutputPixelType, Dimension &gt; OutputImageType;<br>
 &nbsp; &nbsp; typedef itk::Image&lt; BinaryPixelType, Dimension &gt; BinaryImageType;<br>
<br>
<br>
 &nbsp; &nbsp; typedef itk::GradientMagnitudeImageFilter&lt; InternalImageType, OutputImageType &gt; GradientMagnitudeFilterType;<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GradientMagnitudeFilterType::Pointer gradientMagnFilter = GradientMagnitudeFilterType::New();<br>
 &nbsp; &nbsp;// typedef itk::GradientMagnitudeRecursiveGaussianImageFilter&lt;InternalImageType, OutputImageType&gt; GradientGaussianFilterType;<br>
<br>
 &nbsp; &nbsp; typedef &nbsp;itk::ImageFileReader&lt; InternalImageType &gt; ReaderType;<br>
 &nbsp; &nbsp; //typedef &nbsp;itk::ImageFileReader&lt; BinaryImageType &gt; BinaryReaderType;<br>
 &nbsp; &nbsp; typedef &nbsp;itk::ImageFileWriter&lt; OutputImageType &gt; WriterType;<br>
 &nbsp; &nbsp;// typedef &nbsp;itk::ImageFileWriter&lt; BinaryImageType &gt; BinaryWriterType;<br>
 &nbsp; &nbsp; ReaderType::Pointer imageReader = ReaderType::New();<br>
<br>
 &nbsp; &nbsp; argv[1] = &quot;/square.nii&quot;;<br>
<br>
 &nbsp; &nbsp; imageReader -&gt; SetFileName( argv[1] );<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp; typedef itk::CovariantVector&lt; float, Dimension &gt; &nbsp;GradientPixelType;<br>
 &nbsp; &nbsp; &nbsp; &nbsp; typedef itk::Image&lt; GradientPixelType, Dimension &gt; GradientImageType;<br>
 &nbsp; &nbsp; &nbsp; &nbsp; //typedef itk::GradientVectorFlowImageFilter&lt; GradientImageType, GradientImageType &gt; GVFFilterType;<br>
 &nbsp; &nbsp; &nbsp; &nbsp; //typedef itk::LaplacianImageFilter&lt;InternalImageType, InternalImageType&gt; LaplacianFilterType;<br>
 &nbsp; &nbsp; &nbsp; &nbsp; typedef itk::GradientImageFilter&lt;InternalImageType, float, float&gt; GradientImageFilterType;<br>
 &nbsp; &nbsp; &nbsp; &nbsp; typedef itk::GradientMagnitudeImageFilter&lt;InternalImageType, InternalImageType&gt; GradientMagnitudeImageFilterType;<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp; GradientMagnitudeImageFilterType::Pointer gmFilter = GradientMagnitudeImageFilterType::New();<br>
 &nbsp; &nbsp; &nbsp; &nbsp; gmFilter-&gt;SetInput(imageReader-&gt;GetOutput());<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp; //GradientGaussianFilterType::Pointer gaussianFilter = GradientGaussianFilterType::New();<br>
 &nbsp; &nbsp; &nbsp; &nbsp; //gaussianFilter-&gt;SetSigma( 0.5 );<br>
 &nbsp; &nbsp; &nbsp; &nbsp; //gaussianFilter-&gt;SetInput( imageReader-&gt;GetOutput() );<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp; GradientImageFilterType::Pointer gFilter = GradientImageFilterType::New();<br>
 &nbsp; &nbsp; &nbsp; &nbsp; gFilter-&gt;SetInput(gmFilter-&gt;GetOutput());<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp; try<br>
 &nbsp; &nbsp; &nbsp; {<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// gmFilter-&gt;Update();<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //gaussianFilter-&gt;Update();<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;gFilter-&gt;Update();<br>
 &nbsp; &nbsp; &nbsp; }<br>
 &nbsp; &nbsp; &nbsp; catch( itk::ExceptionObject &amp; excep )<br>
 &nbsp; &nbsp; &nbsp; {<br>
 &nbsp; &nbsp; &nbsp; &nbsp; std::cerr &lt;&lt; &quot;Exception caught !&quot; &lt;&lt; std::endl;<br>
 &nbsp; &nbsp; &nbsp; &nbsp; std::cerr &lt;&lt; excep &lt;&lt; std::endl;<br>
 &nbsp; &nbsp; &nbsp; }<br>
<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp; WriterType::Pointer gmWriter = WriterType::New();<br>
 &nbsp; &nbsp; &nbsp; &nbsp; gmWriter-&gt;SetFileName(&quot;/gradientMagnitudeSquare.nii&quot;);<br>
 &nbsp; &nbsp; &nbsp; &nbsp; gmWriter-&gt;SetInput(gmFilter-&gt;GetOutput());<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp; //WriterType::Pointer gaussianWriter = WriterType::New();<br>
 &nbsp; &nbsp; &nbsp; &nbsp; //gaussianWriter-&gt;SetFileName(&quot;/gradientMagnitudeGaussianSquare.nii&quot;);<br>
 &nbsp; &nbsp; &nbsp; &nbsp; //gaussianWriter-&gt;SetInput(gaussianFilter-&gt;GetOutput());<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp; typedef itk::ImageFileWriter&lt; GradientImageType &gt; FieldWriterType;<br>
 &nbsp; &nbsp; &nbsp; &nbsp; FieldWriterType::Pointer gradientWriter = FieldWriterType::New();<br>
 &nbsp; &nbsp; &nbsp; &nbsp; gradientWriter-&gt;SetInput(gFilter-&gt;GetOutput());<br>
 &nbsp; &nbsp; &nbsp; &nbsp; gradientWriter-&gt;SetFileName(&quot;/gradientSquare.vtk&quot;);<br>
 &nbsp; &nbsp; &nbsp; &nbsp; try<br>
 &nbsp; &nbsp; &nbsp; {<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gmWriter-&gt;Update();<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gradientWriter-&gt;Update();<br>
 &nbsp; &nbsp; &nbsp; }<br>
 &nbsp; &nbsp; &nbsp; catch( itk::ExceptionObject &amp; excep )<br>
 &nbsp; &nbsp; &nbsp; {<br>
 &nbsp; &nbsp; &nbsp; &nbsp; std::cerr &lt;&lt; &quot;Exception caught !&quot; &lt;&lt; std::endl;<br>
 &nbsp; &nbsp; &nbsp; &nbsp; std::cerr &lt;&lt; excep &lt;&lt; std::endl;<br>
 &nbsp; &nbsp; &nbsp; }<br>
<br>
<br>
}<br>
<br>
<br>_____________________________________<br>
Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
<br>
Visit other Kitware open-source projects at<br>
<a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Please keep messages on-topic and check the ITK FAQ at: <a href="http://www.itk.org/Wiki/ITK_FAQ" target="_blank">http://www.itk.org/Wiki/ITK_FAQ</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://www.itk.org/mailman/listinfo/insight-users" target="_blank">http://www.itk.org/mailman/listinfo/insight-users</a><br>
<br></blockquote></div><br>