[Insight-users] Gradients of binary image pointing in wrong direction?

Luis Ibanez luis.ibanez at kitware.com
Sun Feb 15 18:03:38 EST 2009


Hi Amardeep,

Thanks for posting the source code and the image that you are using.

We reformatted your code and were able to reproduce what you are reporting:

That the directions of the gradient (of the gradient magnitude) *seem* to be

pointing in the wrong direction.

Please find attached the two versions that we reformat of your code.

After a lot of head scratching we found that the apparent problem, is that
your image has DIRECTION cosines that are not those of a unit matrix.

The direction cosines of your image actually are:

                  -1 0 0
                  0 -1 0
                  0 0 1

Which means that the X and Y axis are reflected with respect to
what you would usually expect.

Note that ITK gradient filters *DO* take direction into account
when they are computing the gradient vectors. since ITK 3.10,
we have set the itkImage to consider Direction by default.

Therefore, at the end, this is simply a case of using inadecuate
Visualization tools. The viewer that you are using doesn't take
the image direction into account.

For your convenience, we have added the explicit calls for the
gradient to take or not to take direction into account.
Please note however, that ignoring the direction will result
in inconsistent computations.


Also, you will better understand what is going on, if you
use an asymmetric image. The square that you are using
doen't help to disambiguate between the +Y and -Y directions
of the axis.



  Regards,


        Luis

-----------------------------------------------------
On Thu, Feb 12, 2009 at 7:58 AM, Amardeep Singh <amar.singh at gmx.de> wrote:

> Dear ITK Users
>
> I am still having problems with the gradient vector flow and so I went a
> step back and looked at the gradients
> of my image. At the moment, I am just dealing with a synthetic image of a
> square (see attachment).
> I calculate the gradient magnitude with the itkGradientMagnitudeImageFilter
> and then the gradient with the
> itkGradientImageFilter. I save the output of the gradient filter as a *.vtk
> image file and visualize it in Paraview.
> I find that the vectors are pointing outwards, whereas I would expect them
> to point inwards, to the middle of
> the edge.
> Could anyone tell me whats going wrong, please? Or is everything correct
> and I misunderstood something?
> Thank you for any help.
>
> Best regards
> Amar
>
> P.S. I have attached my code below:
>
> /*
> * gvfOnSyntheticImage.cc
> *
> *  Created on: 11-Feb-2009
> *    */
>
>
>
> //ITK Headers
> #include "itkImage.h"
>
> #include "itkImageFileReader.h"
> #include "itkImageFileWriter.h"
> #include "itkCovariantVector.h"
> #include "itkGradientVectorFlowImageFilter.h"
> //#include "itkLaplacianImageFilter.h"
> #include "itkGradientMagnitudeRecursiveGaussianImageFilter.h"
> #include "itkGradientMagnitudeImageFilter.h"
> #include "itkGradientImageFilter.h"
> #include "itkCastImageFilter.h"
>
> #include "vtkImageWriter.h"
>
> #include <iostream>
>
>
> int main( int argc, char *argv[] )
> {
>
>     typedef   float  InternalPixelType;
>     typedef float OutputPixelType;
>     typedef unsigned char BinaryPixelType;
>
>     const     unsigned int    Dimension = 3;
>
>     typedef itk::Image< InternalPixelType, Dimension >  InternalImageType;
>     typedef itk::Image< OutputPixelType, Dimension > OutputImageType;
>     typedef itk::Image< BinaryPixelType, Dimension > BinaryImageType;
>
>
>     typedef itk::GradientMagnitudeImageFilter< InternalImageType,
> OutputImageType > GradientMagnitudeFilterType;
>           GradientMagnitudeFilterType::Pointer gradientMagnFilter =
> GradientMagnitudeFilterType::New();
>    // typedef
> itk::GradientMagnitudeRecursiveGaussianImageFilter<InternalImageType,
> OutputImageType> GradientGaussianFilterType;
>
>     typedef  itk::ImageFileReader< InternalImageType > ReaderType;
>     //typedef  itk::ImageFileReader< BinaryImageType > BinaryReaderType;
>     typedef  itk::ImageFileWriter< OutputImageType > WriterType;
>    // typedef  itk::ImageFileWriter< BinaryImageType > BinaryWriterType;
>     ReaderType::Pointer imageReader = ReaderType::New();
>
>     argv[1] = "/square.nii";
>
>     imageReader -> SetFileName( argv[1] );
>
>         typedef itk::CovariantVector< float, Dimension >
>  GradientPixelType;
>         typedef itk::Image< GradientPixelType, Dimension >
> GradientImageType;
>         //typedef itk::GradientVectorFlowImageFilter< GradientImageType,
> GradientImageType > GVFFilterType;
>         //typedef itk::LaplacianImageFilter<InternalImageType,
> InternalImageType> LaplacianFilterType;
>         typedef itk::GradientImageFilter<InternalImageType, float, float>
> GradientImageFilterType;
>         typedef itk::GradientMagnitudeImageFilter<InternalImageType,
> InternalImageType> GradientMagnitudeImageFilterType;
>
>         GradientMagnitudeImageFilterType::Pointer gmFilter =
> GradientMagnitudeImageFilterType::New();
>         gmFilter->SetInput(imageReader->GetOutput());
>
>         //GradientGaussianFilterType::Pointer gaussianFilter =
> GradientGaussianFilterType::New();
>         //gaussianFilter->SetSigma( 0.5 );
>         //gaussianFilter->SetInput( imageReader->GetOutput() );
>
>         GradientImageFilterType::Pointer gFilter =
> GradientImageFilterType::New();
>         gFilter->SetInput(gmFilter->GetOutput());
>
>         try
>       {
>            // gmFilter->Update();
>             //gaussianFilter->Update();
>            gFilter->Update();
>       }
>       catch( itk::ExceptionObject & excep )
>       {
>         std::cerr << "Exception caught !" << std::endl;
>         std::cerr << excep << std::endl;
>       }
>
>
>         WriterType::Pointer gmWriter = WriterType::New();
>         gmWriter->SetFileName("/gradientMagnitudeSquare.nii");
>         gmWriter->SetInput(gmFilter->GetOutput());
>
>         //WriterType::Pointer gaussianWriter = WriterType::New();
>
> //gaussianWriter->SetFileName("/gradientMagnitudeGaussianSquare.nii");
>         //gaussianWriter->SetInput(gaussianFilter->GetOutput());
>
>         typedef itk::ImageFileWriter< GradientImageType > FieldWriterType;
>         FieldWriterType::Pointer gradientWriter = FieldWriterType::New();
>         gradientWriter->SetInput(gFilter->GetOutput());
>         gradientWriter->SetFileName("/gradientSquare.vtk");
>         try
>       {
>             gmWriter->Update();
>           gradientWriter->Update();
>       }
>       catch( itk::ExceptionObject & excep )
>       {
>         std::cerr << "Exception caught !" << std::endl;
>         std::cerr << excep << std::endl;
>       }
>
>
> }
>
>
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20090215/adc8c8d9/attachment-0001.htm>
-------------- next part --------------
CMAKE_MINIMUM_REQUIRED(VERSION 2.4)
IF(COMMAND CMAKE_POLICY)
  CMAKE_POLICY(SET CMP0003 NEW)
ENDIF(COMMAND CMAKE_POLICY)

PROJECT(GradientVectorFlow)

FIND_PACKAGE(ITK REQUIRED)
INCLUDE(${ITK_USE_FILE})

ADD_EXECUTABLE(GradientVectorFlow GradientVectorFlow.cxx )
TARGET_LINK_LIBRARIES(GradientVectorFlow ITKIO ITKCommon)

ADD_EXECUTABLE(GradientVectorFlow2 GradientVectorFlow2.cxx )
TARGET_LINK_LIBRARIES(GradientVectorFlow2 ITKIO ITKCommon)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: GradientVectorFlow.cxx
Type: text/x-c++src
Size: 2601 bytes
Desc: not available
URL: <http://www.itk.org/pipermail/insight-users/attachments/20090215/adc8c8d9/attachment-0002.cxx>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: GradientVectorFlow2.cxx
Type: text/x-c++src
Size: 2714 bytes
Desc: not available
URL: <http://www.itk.org/pipermail/insight-users/attachments/20090215/adc8c8d9/attachment-0003.cxx>


More information about the Insight-users mailing list