#include <cmath>
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;
}
using PixelType = unsigned char;
using ShapedNeighborhoodIteratorType =
reader->SetFileName(argv[1]);
try
{
reader->Update();
}
catch (const itk::ExceptionObject & err)
{
std::cerr << "ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
return EXIT_FAILURE;
}
output->SetRegions(reader->GetOutput()->GetRequestedRegion());
output->Allocate();
unsigned int element_radius = std::stoi(argv[3]);
ShapedNeighborhoodIteratorType::RadiusType radius;
radius.Fill(element_radius);
using FaceCalculatorType =
FaceCalculatorType faceCalculator;
FaceCalculatorType::FaceListType faceList;
FaceCalculatorType::FaceListType::iterator fit;
faceList =
faceCalculator(reader->GetOutput(), output->GetRequestedRegion(), radius);
IteratorType out;
constexpr PixelType background_value = 0;
constexpr PixelType foreground_value = 255;
const auto 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);
}
}
}
writer->SetFileName(argv[2]);
writer->SetInput(output);
try
{
writer->Update();
}
catch (const itk::ExceptionObject & err)
{
std::cerr << "ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}