#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 EXIT_FAILURE;
}
typedef unsigned char PixelType;
ImageType
> ShapedNeighborhoodIteratorType;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName( argv[1] );
try
{
reader->Update();
}
{
std::cerr << "ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
return EXIT_FAILURE;
}
ImageType::Pointer output = ImageType::New();
output->SetRegions(reader->GetOutput()->GetRequestedRegion());
output->Allocate();
unsigned int element_radius = ::atoi( argv[3] );
ShapedNeighborhoodIteratorType::RadiusType radius;
radius.Fill(element_radius);
ImageType > FaceCalculatorType;
FaceCalculatorType faceCalculator;
FaceCalculatorType::FaceListType faceList;
FaceCalculatorType::FaceListType::iterator fit;
faceList = faceCalculator( reader->GetOutput(),
output->GetRequestedRegion(),
radius );
IteratorType out;
const PixelType background_value = 0;
const PixelType foreground_value = 255;
const float rad = static_cast<float>(element_radius);
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 = true;
for (ci = it.Begin(); ci != it.End(); ci++)
{
if (ci.Get() == background_value)
{
flag = false;
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::cerr << "ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}