int main(int argc, char *argv[])
{
if( argc < 4 )
{
std::cerr << "Usage arguments: InputImage MembershipImage numberOfClasses [componentToExtract ExtractedImage]" << std::endl;
std::cerr << " The MembershipImage image written is a VectorImage, ( an image with multiple components ) ";
std::cerr << "Given that most viewers can't see vector images, we will optionally extract a component and ";
std::cerr << "write it out as a scalar image as well." << std::endl;
return EXIT_FAILURE;
}
BayesianInitializerType::Pointer bayesianInitializer
= BayesianInitializerType::New();
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName( argv[1] );
try
{
reader->Update();
}
{
std::cerr << "Exception thrown " << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
bayesianInitializer->SetInput( reader->GetOutput() );
bayesianInitializer->SetNumberOfClasses( std::stoi( argv[3] ) );
WriterType::Pointer writer = WriterType::New();
writer->SetInput( bayesianInitializer->GetOutput() );
writer->SetFileName( argv[2] );
try
{
bayesianInitializer->Update();
}
{
std::cerr << "Exception thrown " << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
try
{
writer->Update();
}
{
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 >;
ExtractedComponentImageType::Pointer extractedComponentImage =
ExtractedComponentImageType::New();
extractedComponentImage->CopyInformation(
bayesianInitializer->GetOutput() );
extractedComponentImage->SetBufferedRegion( bayesianInitializer->GetOutput()->GetBufferedRegion() );
extractedComponentImage->SetRequestedRegion( bayesianInitializer->GetOutput()->GetRequestedRegion() );
extractedComponentImage->Allocate();
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() )
{
it.Set(cit.Get()[componentToExtract]);
++it;
++cit;
}
ExtractedComponentImageType, OutputImageType >;
RescalerType::Pointer rescaler = RescalerType::New();
rescaler->SetInput( extractedComponentImage );
rescaler->SetOutputMinimum( 0 );
rescaler->SetOutputMaximum( 255 );
ExtractedComponentWriterType::Pointer
rescaledImageWriter = ExtractedComponentWriterType::New();
rescaledImageWriter->SetInput( rescaler->GetOutput() );
rescaledImageWriter->SetFileName( argv[5] );
rescaledImageWriter->Update();
}
return EXIT_SUCCESS;
}