Skip to content

Commit

Permalink
STYLE: Enclose the majority of the main function in a try-catch block
Browse files Browse the repository at this point in the history
  • Loading branch information
dzenanz committed Feb 7, 2025
1 parent d233827 commit 3f13571
Show file tree
Hide file tree
Showing 4 changed files with 555 additions and 627 deletions.
133 changes: 57 additions & 76 deletions Examples/Statistics/BayesianClassifier.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -67,89 +67,70 @@ main(int argc, char * argv[])
return EXIT_FAILURE;
}


// setup reader
constexpr unsigned int Dimension = 2;
using InputPixelType = float;
using InputImageType = itk::VectorImage<InputPixelType, Dimension>;
InputImageType::Pointer input;
try
{
input = itk::ReadImage<InputImageType>(argv[1]);
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}

using LabelType = unsigned char;
using PriorType = float;
using PosteriorType = float;


using ClassifierFilterType =
itk::BayesianClassifierImageFilter<InputImageType,
LabelType,
PosteriorType,
PriorType>;

auto filter = ClassifierFilterType::New();


filter->SetInput(input);

if (argv[3])
{
filter->SetNumberOfSmoothingIterations(std::stoi(argv[3]));
using ExtractedComponentImageType =
ClassifierFilterType::ExtractedComponentImageType;
using SmoothingFilterType = itk::GradientAnisotropicDiffusionImageFilter<
ExtractedComponentImageType,
ExtractedComponentImageType>;
auto smoother = SmoothingFilterType::New();
smoother->SetNumberOfIterations(1);
smoother->SetTimeStep(0.125);
smoother->SetConductanceParameter(3);
filter->SetSmoothingFilter(smoother);
}


// SET FILTER'S PRIOR PARAMETERS
// do nothing here to default to uniform priors
// otherwise set the priors to some user provided values

//
// Setup writer.. Rescale the label map to the dynamic range of the
// datatype and write it
//
using ClassifierOutputImageType = ClassifierFilterType::OutputImageType;
using OutputImageType = itk::Image<unsigned char, Dimension>;
using RescalerType =
itk::RescaleIntensityImageFilter<ClassifierOutputImageType,
OutputImageType>;
auto rescaler = RescalerType::New();
rescaler->SetInput(filter->GetOutput());
rescaler->SetOutputMinimum(0);
rescaler->SetOutputMaximum(255);

//
// Write labelmap to file
//
try
{
constexpr unsigned int Dimension = 2;
using InputPixelType = float;
using InputImageType = itk::VectorImage<InputPixelType, Dimension>;

auto input = itk::ReadImage<InputImageType>(argv[1]);

using LabelType = unsigned char;
using PriorType = float;
using PosteriorType = float;

using ClassifierFilterType =
itk::BayesianClassifierImageFilter<InputImageType,
LabelType,
PosteriorType,
PriorType>;

auto filter = ClassifierFilterType::New();

filter->SetInput(input);

if (argc > 3)
{
filter->SetNumberOfSmoothingIterations(std::stoi(argv[3]));
using ExtractedComponentImageType =
ClassifierFilterType::ExtractedComponentImageType;
using SmoothingFilterType =
itk::GradientAnisotropicDiffusionImageFilter<
ExtractedComponentImageType,
ExtractedComponentImageType>;
auto smoother = SmoothingFilterType::New();
smoother->SetNumberOfIterations(1);
smoother->SetTimeStep(0.125);
smoother->SetConductanceParameter(3);
filter->SetSmoothingFilter(smoother);
}

// SET FILTER'S PRIOR PARAMETERS
// do nothing here to default to uniform priors
// otherwise set the priors to some user provided values

// Rescale the label map to the dynamic range of the datatype and write it
using ClassifierOutputImageType = ClassifierFilterType::OutputImageType;
using OutputImageType = itk::Image<unsigned char, Dimension>;
using RescalerType =
itk::RescaleIntensityImageFilter<ClassifierOutputImageType,
OutputImageType>;
auto rescaler = RescalerType::New();
rescaler->SetInput(filter->GetOutput());
rescaler->SetOutputMinimum(0);
rescaler->SetOutputMaximum(255);

// Write labelmap to file
itk::WriteImage(rescaler->GetOutput(), argv[2]);

// Testing print
filter->Print(std::cout);
std::cout << "Test passed." << std::endl;
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << "Exception caught: " << std::endl;
std::cerr << excp << std::endl;
std::cerr << "ITK exception caught:\n" << excp << std::endl;
return EXIT_FAILURE;
}

// Testing print
filter->Print(std::cout);
std::cout << "Test passed." << std::endl;

return EXIT_SUCCESS;
}
136 changes: 54 additions & 82 deletions Examples/Statistics/BayesianClassifierInitializer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -80,101 +80,73 @@ main(int argc, char * argv[])
return EXIT_FAILURE;
}

using ImageType = itk::Image<unsigned char, Dimension>;
using BayesianInitializerType =
itk::BayesianClassifierInitializationImageFilter<ImageType>;
auto bayesianInitializer = BayesianInitializerType::New();

ImageType::Pointer input;
try
{
input = itk::ReadImage<ImageType>(argv[1]);
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << "Exception thrown " << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
using ImageType = itk::Image<unsigned char, Dimension>;
using BayesianInitializerType =
itk::BayesianClassifierInitializationImageFilter<ImageType>;
auto bayesianInitializer = BayesianInitializerType::New();

bayesianInitializer->SetInput(input);
bayesianInitializer->SetNumberOfClasses(std::stoi(argv[3]));
auto input = itk::ReadImage<ImageType>(argv[1]);

// TODO add test where we specify membership functions
bayesianInitializer->SetInput(input);
bayesianInitializer->SetNumberOfClasses(std::stoi(argv[3]));

// TODO add test where we specify membership functions

try
{
bayesianInitializer->Update();
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << "Exception thrown " << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}

try
{
itk::WriteImage(bayesianInitializer->GetOutput(), argv[2]);
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << "Exception thrown " << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}

if (argv[4] && argv[5])
{
using MembershipImageType = BayesianInitializerType::OutputImageType;
using ExtractedComponentImageType =
itk::Image<MembershipImageType::InternalPixelType, Dimension>;
auto extractedComponentImage = ExtractedComponentImageType::New();
extractedComponentImage->CopyInformation(
bayesianInitializer->GetOutput());
extractedComponentImage->SetBufferedRegion(
bayesianInitializer->GetOutput()->GetBufferedRegion());
extractedComponentImage->SetRequestedRegion(
bayesianInitializer->GetOutput()->GetRequestedRegion());
extractedComponentImage->Allocate();
using ConstIteratorType =
itk::ImageRegionConstIterator<MembershipImageType>;
using IteratorType =
itk::ImageRegionIterator<ExtractedComponentImageType>;
ConstIteratorType cit(
bayesianInitializer->GetOutput(),
bayesianInitializer->GetOutput()->GetBufferedRegion());
IteratorType it(extractedComponentImage,
extractedComponentImage->GetLargestPossibleRegion());

const unsigned int componentToExtract = std::stoi(argv[4]);
cit.GoToBegin();
it.GoToBegin();
while (!cit.IsAtEnd())
if (argv[4] && argv[5])
{
it.Set(cit.Get()[componentToExtract]);
++it;
++cit;
}
using MembershipImageType = BayesianInitializerType::OutputImageType;
using ExtractedComponentImageType =
itk::Image<MembershipImageType::InternalPixelType, Dimension>;
auto extractedComponentImage = ExtractedComponentImageType::New();
extractedComponentImage->CopyInformation(
bayesianInitializer->GetOutput());
extractedComponentImage->SetBufferedRegion(
bayesianInitializer->GetOutput()->GetBufferedRegion());
extractedComponentImage->SetRequestedRegion(
bayesianInitializer->GetOutput()->GetRequestedRegion());
extractedComponentImage->Allocate();
using ConstIteratorType =
itk::ImageRegionConstIterator<MembershipImageType>;
using IteratorType =
itk::ImageRegionIterator<ExtractedComponentImageType>;
ConstIteratorType cit(
bayesianInitializer->GetOutput(),
bayesianInitializer->GetOutput()->GetBufferedRegion());
IteratorType it(extractedComponentImage,
extractedComponentImage->GetLargestPossibleRegion());

// Write out the rescaled extracted component
using OutputImageType = itk::Image<unsigned char, Dimension>;
using RescalerType =
itk::RescaleIntensityImageFilter<ExtractedComponentImageType,
OutputImageType>;
auto rescaler = RescalerType::New();
rescaler->SetInput(extractedComponentImage);
rescaler->SetOutputMinimum(0);
rescaler->SetOutputMaximum(255);
try
{
const unsigned int componentToExtract = std::stoi(argv[4]);
cit.GoToBegin();
it.GoToBegin();
while (!cit.IsAtEnd())
{
it.Set(cit.Get()[componentToExtract]);
++it;
++cit;
}

// Write out the rescaled extracted component
using OutputImageType = itk::Image<unsigned char, Dimension>;
using RescalerType =
itk::RescaleIntensityImageFilter<ExtractedComponentImageType,
OutputImageType>;
auto rescaler = RescalerType::New();
rescaler->SetInput(extractedComponentImage);
rescaler->SetOutputMinimum(0);
rescaler->SetOutputMaximum(255);
itk::WriteImage(rescaler->GetOutput(), argv[5]);
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << "ITK exception caught:\n" << excp << std::endl;
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
Expand Down
Loading

0 comments on commit 3f13571

Please sign in to comment.