Skip to content

Instantly share code, notes, and snippets.

@daviddoria
Created February 18, 2011 15:46
Show Gist options
  • Save daviddoria/833843 to your computer and use it in GitHub Desktop.
Save daviddoria/833843 to your computer and use it in GitHub Desktop.
An example of the level of simplicity that should be available.
#include "itkImage.h"
#include "itkImageFileWriter.h"
#include "itkPolyLineParametricPath.h"
#include "itkSpeedFunctionToPathFilter.h"
#include "itkPathIterator.h"
#include "itkGradientDescentOptimizer.h"
#include "itkSigmoidImageFilter.h"
typedef itk::Image< unsigned char, 2> ImageType;
typedef itk::PolyLineParametricPath< 2 > PathType;
typedef itk::SpeedFunctionToPathFilter< ImageType, PathType > PathFilterType;
void CreateCircleImage(ImageType::Pointer);
void ComputePath(ImageType::Pointer image, PathFilterType::PointType startPoint, PathFilterType::PointType endPoint, ImageType::Pointer pathImage);
int main( int argc, char *argv[] )
{
ImageType::Pointer image = ImageType::New();
CreateCircleImage(image);
PathFilterType::PointType startPoint;
PathFilterType::PointType endPoint;
ImageType::Pointer pathImage;
/*
startPoint[0] = 30; startPoint[1] = 49;
endPoint[0] = 69; endPoint[1] = 50;
*/
startPoint[0] = 49; startPoint[1] = 30;
endPoint[0] = 50; endPoint[1] = 69;
ComputePath(image, startPoint, endPoint, pathImage);
/*
typedef itk::ImageFileWriter< ImageType > WriterType;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName("output.png");
writer->SetInput(pathImage);
writer->Update();
*/
return EXIT_SUCCESS;
}
void ComputePath(ImageType::Pointer image, PathFilterType::PointType startPoint, PathFilterType::PointType endPoint, ImageType::Pointer pathImage)
{
// Compute speed
typedef itk::SigmoidImageFilter<
ImageType,
ImageType > SigmoidFilterType;
SigmoidFilterType::Pointer sigmoid = SigmoidFilterType::New();
sigmoid->SetOutputMinimum( 0.0 );
sigmoid->SetOutputMaximum( 1.0 );
sigmoid->SetInput(image);
sigmoid->Update();
ImageType::Pointer speed = sigmoid->GetOutput();
typedef PathFilterType::CostFunctionType::CoordRepType CoordRepType;
typedef itk::PathIterator< ImageType, PathType > PathIteratorType;
// Create interpolator
typedef itk::LinearInterpolateImageFunction<ImageType, CoordRepType>
InterpolatorType;
InterpolatorType::Pointer interp = InterpolatorType::New();
// Create cost function
PathFilterType::CostFunctionType::Pointer cost =
PathFilterType::CostFunctionType::New();
cost->SetInterpolator( interp );
// Create optimizer
typedef itk::GradientDescentOptimizer OptimizerType;
OptimizerType::Pointer optimizer = OptimizerType::New();
optimizer->SetNumberOfIterations( 1000 );
// Create path filter
PathFilterType::Pointer pathFilter = PathFilterType::New();
pathFilter->SetInput( speed );
pathFilter->SetCostFunction( cost );
pathFilter->SetOptimizer( optimizer );
pathFilter->SetTerminationValue( 2.0 );
// Add path information
PathFilterType::PathInfo info;
info.SetStartPoint( startPoint );
info.SetEndPoint( endPoint );
pathFilter->AddPathInfo( info );
// Compute the path
pathFilter->Update( );
// Allocate output image
ImageType::Pointer output = ImageType::New();
output->SetRegions( speed->GetLargestPossibleRegion() );
output->SetSpacing( speed->GetSpacing() );
output->SetOrigin( speed->GetOrigin() );
output->Allocate( );
output->FillBuffer(0);
// Rasterize path
for (unsigned int i=0; i<pathFilter->GetNumberOfOutputs(); i++)
{
// Get the path
PathType::Pointer path = pathFilter->GetOutput( i );
// Check path is valid
if ( path->GetVertexList()->Size() == 0 )
{
std::cout << "WARNING: Path " << (i+1) << " contains no points!" << std::endl;
continue;
}
// Iterate path and convert to image
PathIteratorType it( output, path );
for (it.GoToBegin(); !it.IsAtEnd(); ++it)
{
it.Set( itk::NumericTraits<ImageType::PixelType>::max() );
}
}
pathImage = output;
}
void CreateCircleImage(ImageType::Pointer image)
{
itk::Size<2> size;
size.Fill(100);
itk::Index<2> start;
start.Fill(0);
itk::ImageRegion<2> region(start, size);
image->SetRegions(region);
image->Allocate();
image->FillBuffer(0);
for(unsigned int i = 0; i < 1000; i++)
{
int x = 20 * cos(i) + 50;
int y = 20 * sin(i) + 50;
itk::Index<2> pixel;
pixel[0] = x;
pixel[1] = y;
image->SetPixel(pixel, 255);
}
typedef itk::ImageFileWriter< ImageType > WriterType;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName("circle.png");
writer->SetInput(image);
writer->Update();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment