int main( int argc, char *argv[] )
{
if( argc < 4 )
{
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 << " numberOfLines " << 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;
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();
CastingFilterType;
CastingFilterType::Pointer caster = CastingFilterType::New();
std::cout << "Applying gradient magnitude filter" << std::endl;
AccumulatorImageType > GradientFilterType;
GradientFilterType::Pointer gradFilter = GradientFilterType::New();
caster->SetInput(localImage);
gradFilter->SetInput(caster->GetOutput());
gradFilter->Update();
std::cout << "Thresholding" << std::endl;
ThresholdFilterType::Pointer threshFilter = ThresholdFilterType::New();
threshFilter->SetInput( gradFilter->GetOutput());
threshFilter->SetOutsideValue(0);
unsigned char threshBelow = 0;
unsigned char threshAbove = 255;
threshFilter->ThresholdOutside(threshBelow,threshAbove);
threshFilter->Update();
std::cout << "Computing Hough Map" << std::endl;
AccumulatorPixelType> HoughTransformFilterType;
HoughTransformFilterType::Pointer houghFilter
= HoughTransformFilterType::New();
houghFilter->SetInput(threshFilter->GetOutput());
houghFilter->SetNumberOfLines(atoi(argv[3]));
if(argc > 4 )
{
houghFilter->SetVariance(atof(argv[4]));
}
if(argc > 5 )
{
houghFilter->SetDiscRadius(atof(argv[5]));
}
houghFilter->Update();
AccumulatorImageType::Pointer localAccumulator = houghFilter->GetOutput();
HoughTransformFilterType::LinesListType lines;
lines = houghFilter->GetLines(atoi(argv[3]));
std::cout << "Found " << lines.size() << " line(s)." << std::endl;
typedef unsigned char OutputPixelType;
OutputImageType::Pointer localOutputImage = OutputImageType::New();
OutputImageType::RegionType region(localImage->GetLargestPossibleRegion());
localOutputImage->SetRegions(region);
localOutputImage->CopyInformation(localImage);
localOutputImage->Allocate(true);
typedef HoughTransformFilterType::LinesListType::const_iterator LineIterator;
LineIterator itLines = lines.begin();
while( itLines != lines.end() )
{
typedef HoughTransformFilterType::LineType::PointListType PointListType;
PointListType pointsList = (*itLines)->GetPoints();
PointListType::const_iterator itPoints = pointsList.begin();
double u[2];
u[0] = (*itPoints).GetPosition()[0];
u[1] = (*itPoints).GetPosition()[1];
itPoints++;
double v[2];
v[0] = u[0]-(*itPoints).GetPosition()[0];
v[1] = u[1]-(*itPoints).GetPosition()[1];
double norm = std::sqrt(v[0]*v[0]+v[1]*v[1]);
v[0] /= norm;
v[1] /= norm;
ImageType::IndexType localIndex;
float diag = std::sqrt((float)( size[0]*size[0] + size[1]*size[1] ));
for(int i=static_cast<int>(-diag); i<static_cast<int>(diag); i++)
{
localIndex[0]=(long int)(u[0]+i*v[0]);
localIndex[1]=(long int)(u[1]+i*v[1]);
OutputImageType::RegionType outputRegion =
localOutputImage->GetLargestPossibleRegion();
if( outputRegion.IsInside( localIndex ) )
{
localOutputImage->SetPixel( localIndex, 255 );
}
}
itLines++;
}
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 EXIT_FAILURE;
}
return EXIT_SUCCESS;
}