[Insight-users] Gradients of binary image pointing in wrong direction?
Amardeep Singh
amar.singh at gmx.de
Thu Feb 12 07:58:18 EST 2009
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;
}
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: square.nii.gz
Type: application/x-gzip
Size: 1655 bytes
Desc: not available
URL: <http://www.itk.org/pipermail/insight-users/attachments/20090212/80dd2c8e/attachment.bin>
More information about the Insight-users
mailing list