#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 =
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(argv[1]);
unsigned int element_radius = ::std::stoi(argv[3]);
try
{
reader->Update();
}
catch (const itk::ExceptionObject & err)
{
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();
using FaceCalculatorType =
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 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;
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();
}
catch (const itk::ExceptionObject & err)
{
std::cerr << "ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}