#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]);
const unsigned int element_radius = std::stoi(argv[3]);
try
{
reader->Update();
}
{
std::cerr << "ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
return EXIT_FAILURE;
}
output->SetRegions(reader->GetOutput()->GetRequestedRegion());
output->Allocate();
using FaceCalculatorType =
FaceCalculatorType faceCalculator;
FaceCalculatorType::FaceListType faceList;
FaceCalculatorType::FaceListType::iterator fit;
auto radius = itk::MakeFilled<ShapedNeighborhoodIteratorType::RadiusType>(
element_radius);
faceList =
faceCalculator(reader->GetOutput(), output->GetRequestedRegion(), radius);
IteratorType out;
const auto rad = static_cast<float>(element_radius);
constexpr PixelType background_value = 0;
constexpr 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;
const 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);
}
}
}
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;
}