#include <math.h>
int main( int argc, char ** argv )
{
if ( argc < 4 )
{
std::cerr << "Missing parameters. " << std::endl;
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0]
<< " inputImageFile outputImageFile element_radius"
<< std::endl;
return -1;
}
typedef unsigned char PixelType;
ShapedNeighborhoodIteratorType;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName( argv[1] );
unsigned int element_radius = ::atoi( argv[3] );
try
{
reader->Update();
}
{
std::cout << "ExceptionObject caught !" << std::endl;
std::cout << err << std::endl;
return -1;
}
ImageType::Pointer output = ImageType::New();
output->SetRegions(reader->GetOutput()->GetRequestedRegion());
output->Allocate();
FaceCalculatorType faceCalculator;
FaceCalculatorType::FaceListType faceList;
FaceCalculatorType::FaceListType::iterator fit;
ShapedNeighborhoodIteratorType::RadiusType radius;
radius.Fill(element_radius);
faceList = faceCalculator(reader->GetOutput(), output->GetRequestedRegion(), radius);
IteratorType out;
const float rad = static_cast<float>(element_radius);
const PixelType background_value = 0;
const PixelType foreground_value = 255;
for ( fit=faceList.begin(); fit != faceList.end(); ++fit)
{
ShapedNeighborhoodIteratorType it( radius, reader->GetOutput(), *fit );
out = IteratorType( output, *fit );
for (float y = -rad; y <= rad; y++)
{
for (float x = -rad; x <= rad; x++)
{
ShapedNeighborhoodIteratorType::OffsetType off;
float dis = std::sqrt( x*x + y*y );
if (dis <= rad)
{
off[0] = static_cast<int>(x);
off[1] = static_cast<int>(y);
it.ActivateOffset(off);
}
}
}
for (it.GoToBegin(), out.GoToBegin(); !it.IsAtEnd(); ++it, ++out)
{
ShapedNeighborhoodIteratorType::ConstIterator ci;
bool flag = false;
for (ci = it.Begin(); ci != it.End(); ci++)
{
if (ci.Get() != background_value)
{
flag = true;
break;
}
}
if (flag == true)
{
out.Set(foreground_value);
}
else
{
out.Set(background_value);
}
}
}
WriterType::Pointer writer = WriterType::New();
writer->SetFileName( argv[2] );
writer->SetInput( output );
try
{
writer->Update();
}
{
std::cout << "ExceptionObject caught !" << std::endl;
std::cout << err << std::endl;
return -1;
}
return 0;
}