#include "itksys/SystemTools.hxx"
#include <sstream>
typedef unsigned char PixelType;
static void CreateImage(ImageType::Pointer image);
int main(int argc, char *argv[])
{
unsigned int numberOfThresholds = 1;
ImageType::Pointer image;
if( argc < 2 )
{
image = ImageType::New();
CreateImage(image.GetPointer());
}
else
{
ReaderType::Pointer reader =
ReaderType::New();
reader->SetFileName(argv[1]);
reader->Update();
if (argc > 2)
{
numberOfThresholds = atoi(argv[2]);
}
image = reader->GetOutput();
}
FilterType;
FilterType::Pointer otsuFilter
= FilterType::New();
otsuFilter->SetInput(image);
otsuFilter->SetNumberOfThresholds(numberOfThresholds);
otsuFilter->Update();
RGBFilterType::Pointer colormapImageFilter = RGBFilterType::New();
colormapImageFilter->SetInput(otsuFilter->GetOutput());
image.GetPointer(),true,
argc > 1 ? itksys::SystemTools::GetFilenameName(argv[1]) : "Generated image");
std::stringstream desc;
desc << "Otsu Threshold\n"
<< otsuFilter->GetNumberOfThresholds()
<< " thresholds: ";
FilterType::ThresholdVectorType thresholds = otsuFilter->GetThresholds();
for (unsigned int i = 0; i < thresholds.size(); i++)
{
desc << itk::NumericTraits<FilterType::InputPixelType>::PrintType(thresholds[i]) << " ";
}
colormapImageFilter->GetOutput(),
true,
desc.str());
return EXIT_SUCCESS;
}
void CreateImage(ImageType::Pointer image)
{
ImageType::IndexType start;
start.Fill(0);
ImageType::SizeType size;
size.Fill(100);
ImageType::RegionType region;
region.SetSize(size);
region.SetIndex(start);
image->SetRegions(region);
image->Allocate();
}