#include <list>
#include "vnl/vnl_math.h"
int main( int argc, char *argv[] )
{
if( argc < 6 )
{
std::cerr << "Missing Parameters " << std::endl;
std::cerr << "Usage: " << argv[0] << std::endl;
std::cerr << " inputImage " << std::endl;
std::cerr << " outputImage" << std::endl;
std::cerr << " numberOfCircles " << std::endl;
std::cerr << " radius Min " << std::endl;
std::cerr << " radius Max " << std::endl;
std::cerr << " sweep Angle (default = 0)" << std::endl;
std::cerr << " SigmaGradient (default = 1) " << std::endl;
std::cerr << " variance of the accumulator blurring (default = 5) " << std::endl;
std::cerr << " radius of the disk to remove from the accumulator (default = 10) "<< std::endl;
return 1;
}
typedef unsigned char PixelType;
typedef float AccumulatorPixelType;
const unsigned int Dimension = 2;
ImageType::IndexType localIndex;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName( argv[1] );
try
{
reader->Update();
}
{
std::cerr << "Exception caught !" << std::endl;
std::cerr << excep << std::endl;
}
ImageType::Pointer localImage = reader->GetOutput();
std::cout << "Computing Hough Map" << std::endl;
AccumulatorPixelType> HoughTransformFilterType;
HoughTransformFilterType::Pointer houghFilter
= HoughTransformFilterType::New();
houghFilter->SetInput( reader->GetOutput() );
houghFilter->SetNumberOfCircles( atoi(argv[3]) );
houghFilter->SetMinimumRadius( atof(argv[4]) );
houghFilter->SetMaximumRadius( atof(argv[5]) );
if( argc > 6 )
{
houghFilter->SetSweepAngle( atof(argv[6]) );
}
if( argc > 7 )
{
houghFilter->SetSigmaGradient( atoi(argv[7]) );
}
if( argc > 8 )
{
houghFilter->SetVariance( atof(argv[8]) );
}
if( argc > 9 )
{
houghFilter->SetDiscRadiusRatio( atof(argv[9]) );
}
houghFilter->Update();
AccumulatorImageType::Pointer localAccumulator = houghFilter->GetOutput();
HoughTransformFilterType::CirclesListType circles;
circles = houghFilter->GetCircles( atoi(argv[3]) );
std::cout << "Found " << circles.size() << " circle(s)." << std::endl;
typedef unsigned char OutputPixelType;
OutputImageType::Pointer localOutputImage = OutputImageType::New();
OutputImageType::RegionType region;
region.SetSize(localImage->GetLargestPossibleRegion().GetSize());
region.SetIndex(localImage->GetLargestPossibleRegion().GetIndex());
localOutputImage->SetRegions( region );
localOutputImage->SetOrigin(localImage->GetOrigin());
localOutputImage->SetSpacing(localImage->GetSpacing());
localOutputImage->Allocate(true);
typedef HoughTransformFilterType::CirclesListType CirclesListType;
CirclesListType::const_iterator itCircles = circles.begin();
while( itCircles != circles.end() )
{
std::cout << "Center: ";
std::cout << (*itCircles)->GetObjectToParentTransform()->GetOffset()
<< std::endl;
std::cout << "Radius: " << (*itCircles)->GetRadius()[0] << std::endl;
{
localIndex[0] =
(long int)((*itCircles)->GetObjectToParentTransform()->GetOffset()[0]
+ (*itCircles)->GetRadius()[0]*std::cos(angle));
localIndex[1] =
(long int)((*itCircles)->GetObjectToParentTransform()->GetOffset()[1]
+ (*itCircles)->GetRadius()[0]*std::sin(angle));
OutputImageType::RegionType outputRegion =
localOutputImage->GetLargestPossibleRegion();
if( outputRegion.IsInside( localIndex ) )
{
localOutputImage->SetPixel( localIndex, 255 );
}
}
itCircles++;
}
WriterType::Pointer writer = WriterType::New();
writer->SetFileName( argv[2] );
writer->SetInput(localOutputImage );
try
{
writer->Update();
}
{
std::cerr << "Exception caught !" << std::endl;
std::cerr << excep << std::endl;
}
return 0;
}