int
main(int argc, char * argv[])
{
if (argc < 4)
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0] << " inputImageFile ";
std::cerr << " outputImageFile radius " << std::endl;
return EXIT_FAILURE;
}
using PixelType = unsigned char;
using WritePixelType = unsigned char;
using StructuringElementType =
using OpeningFilterType =
ImageType,
StructuringElementType>;
using ClosingFilterType =
ImageType,
StructuringElementType>;
using AdditionFilterType =
using SubtractionFilterType = itk::
ConstrainedValueDifferenceImageFilter<ImageType, ImageType, ImageType>;
using RescaleFilterType =
StructuringElementType structuringElement;
structuringElement.
SetRadius(std::stoi(argv[3]));
structuringElement.CreateStructuringElement();
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(argv[1]);
WriterType::Pointer writer = WriterType::New();
writer->SetFileName(argv[2]);
try
{
reader->Update();
}
catch (const itk::ExceptionObject & err)
{
std::cout << "Problems reading input image" << std::endl;
std::cerr << "ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
return EXIT_FAILURE;
}
OpeningFilterType::Pointer opening = OpeningFilterType::New();
ClosingFilterType::Pointer closing = ClosingFilterType::New();
opening->SetKernel(structuringElement);
closing->SetKernel(structuringElement);
RescaleFilterType::Pointer rescaleFilter = RescaleFilterType::New();
rescaleFilter->SetOutputMinimum(0);
rescaleFilter->SetOutputMaximum(255);
opening->SetInput(reader->GetOutput());
closing->SetInput(reader->GetOutput());
SubtractionFilterType::Pointer topHat = SubtractionFilterType::New();
topHat->SetInput1(reader->GetOutput());
topHat->SetInput2(opening->GetOutput());
SubtractionFilterType::Pointer bottomHat = SubtractionFilterType::New();
bottomHat->SetInput1(closing->GetOutput());
bottomHat->SetInput2(reader->GetOutput());
AdditionFilterType::Pointer internalAddition = AdditionFilterType::New();
internalAddition->SetInput1(reader->GetOutput());
internalAddition->SetInput2(topHat->GetOutput());
SubtractionFilterType::Pointer imageEnhancement =
SubtractionFilterType::New();
imageEnhancement->SetInput1(internalAddition->GetOutput());
imageEnhancement->SetInput2(bottomHat->GetOutput());
rescaleFilter->SetInput(imageEnhancement->GetOutput());
writer->SetInput(rescaleFilter->GetOutput());
try
{
writer->Update();
}
catch (const itk::ExceptionObject & err)
{
std::cout << "ExceptionObject caught !" << std::endl;
std::cout << err << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}