int main(int argc, char *argv[])
{
const unsigned int Dimension = 2;
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;
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( atoi( argv[3] ) );
BayesianInitializerType::OutputImageType > WriterType;
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] )
{
typedef BayesianInitializerType::OutputImageType MembershipImageType;
typedef itk::Image< MembershipImageType::InternalPixelType,
Dimension > ExtractedComponentImageType;
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 = atoi( argv[4] );
cit.GoToBegin();
it.GoToBegin();
while( !cit.IsAtEnd() )
{
it.Set(cit.Get()[componentToExtract]);
++it;
++cit;
}
ExtractedComponentImageType, OutputImageType > RescalerType;
RescalerType::Pointer rescaler = RescalerType::New();
rescaler->SetInput( extractedComponentImage );
rescaler->SetOutputMinimum( 0 );
rescaler->SetOutputMaximum( 255 );
> ExtractedComponentWriterType;
ExtractedComponentWriterType::Pointer
rescaledImageWriter = ExtractedComponentWriterType::New();
rescaledImageWriter->SetInput( rescaler->GetOutput() );
rescaledImageWriter->SetFileName( argv[5] );
rescaledImageWriter->Update();
}
return EXIT_SUCCESS;
}