#include "itksys/SystemTools.hxx"
#include <sstream>
#ifdef ENABLE_QUICKVIEW
#endif
template <typename TImage>
static void
CreateImage(TImage * const image);
template <typename TImage, typename TLabelImage>
static void
SummarizeLabelStatistics(TImage * image, TLabelImage * labelImage);
int
main(int argc, char * argv[])
{
using PixelType = short;
using LabelPixelType = unsigned int;
PixelType distanceThreshold = 4;
if (argc < 2)
{
CreateImage(image.GetPointer());
}
else
{
if (argc > 2)
{
distanceThreshold = static_cast<PixelType>(atoi(argv[2]));
}
image = itk::ReadImage<ImageType>(argv[1]);
}
connected->SetInput(image);
connected->SetDistanceThreshold(distanceThreshold);
RelabelFilterType::ObjectSizeType minSize = 20;
if (argc > 3)
{
minSize = std::stoi(argv[3]);
}
relabel->SetInput(connected->GetOutput());
relabel->SetMinimumObjectSize(minSize);
relabel->Update();
SummarizeLabelStatistics(image.GetPointer(), relabel->GetOutput());
rgbFilter->SetInput(relabel->GetOutput());
#ifdef ENABLE_QUICKVIEW
image.GetPointer(), true, argc > 1 ? itksys::SystemTools::GetFilenameName(argv[1]) : "Generated image");
std::stringstream desc;
desc << "Scalar Connected Components:\n# of Objects: " << relabel->GetNumberOfObjects() << " Threshold: "
connected->GetDistanceThreshold())
<< " Min Size: " << relabel->GetMinimumObjectSize();
viewer.
AddRGBImage(rgbFilter->GetOutput(),
true, desc.str());
#endif
return EXIT_SUCCESS;
}
template <typename TImage>
void
CreateImage(TImage * const image)
{
start[0] = 0;
start[1] = 0;
unsigned int NumRows = 200;
unsigned int NumCols = 300;
size[0] = NumRows;
size[1] = NumCols;
image->SetRegions(region);
image->Allocate();
{
{
image->SetPixel(pixelIndex, 255);
}
}
{
{
image->SetPixel(pixelIndex, 255);
}
}
}
template <typename TImage, typename TLabelImage>
void
SummarizeLabelStatistics(TImage * image, TLabelImage * labelImage)
{
labelStatisticsImageFilter->SetLabelInput(labelImage);
labelStatisticsImageFilter->SetInput(image);
labelStatisticsImageFilter->UseHistogramsOn();
labelStatisticsImageFilter->Update();
std::cout << "Number of labels: " << labelStatisticsImageFilter->GetNumberOfLabels() << std::endl;
std::cout << std::endl;
using LabelPixelType = typename LabelStatisticsImageFilterType::LabelPixelType;
for (auto vIt = labelStatisticsImageFilter->GetValidLabelValues().begin();
vIt != labelStatisticsImageFilter->GetValidLabelValues().end();
++vIt)
{
if (labelStatisticsImageFilter->HasLabel(*vIt))
{
LabelPixelType labelValue = *vIt;
std::cout << "Label: " << *vIt << std::endl;
std::cout << "\tmin: " << labelStatisticsImageFilter->GetMinimum(labelValue) << std::endl;
std::cout << "\tmax: " << labelStatisticsImageFilter->GetMaximum(labelValue) << std::endl;
std::cout << "\tmedian: " << labelStatisticsImageFilter->GetMedian(labelValue) << std::endl;
std::cout << "\tmean: " << labelStatisticsImageFilter->GetMean(labelValue) << std::endl;
std::cout << "\tsigma: " << labelStatisticsImageFilter->GetSigma(labelValue) << std::endl;
std::cout << "\tvariance: " << labelStatisticsImageFilter->GetVariance(labelValue) << std::endl;
std::cout << "\tsum: " << labelStatisticsImageFilter->GetSum(labelValue) << std::endl;
std::cout << "\tcount: " << labelStatisticsImageFilter->GetCount(labelValue) << std::endl;
std::cout << "\tregion: " << labelStatisticsImageFilter->GetRegion(labelValue) << std::endl;
}
}
}