#include "itksys/SystemTools.hxx"
#include <sstream>
template <typename TImage>
static void CreateImage(TImage* const image);
int main( int argc, char *argv[])
{
const unsigned int Dimension = 2;
typedef unsigned char PixelType;
ImageType::Pointer image;
if( argc < 2 )
{
image = ImageType::New();
CreateImage(image.GetPointer());
}
else
{
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(argv[1]);
reader->Update();
image = reader->GetOutput();
}
ConnectedComponentImageFilterType;
ConnectedComponentImageFilterType::Pointer connected =
ConnectedComponentImageFilterType::New ();
connected->SetInput(image);
connected->Update();
std::cout << "Number of objects: " << connected->GetObjectCount() << std::endl;
RGBFilterType::Pointer rgbFilter = RGBFilterType::New();
rgbFilter->SetInput( connected->GetOutput() );
image.GetPointer(),true,
argc > 1 ? itksys::SystemTools::GetFilenameName(argv[1]) : "Generated image");
std::stringstream desc;
desc << "Connected Components: "
<< connected->GetObjectCount() << " objects";
rgbFilter->GetOutput(),
true,
desc.str());
return EXIT_SUCCESS;
}
template <typename TImage>
void CreateImage(TImage* const image)
{
typename TImage::IndexType start = {{0,0}};
start[0] = 0;
start[1] = 0;
typename TImage::SizeType size;
unsigned int NumRows = 200;
unsigned int NumCols = 300;
size[0] = NumRows;
size[1] = NumCols;
typename TImage::RegionType region(start, size);
image->SetRegions(region);
image->Allocate();
{
{
typename TImage::IndexType pixelIndex = {{r,c}};
image->SetPixel(pixelIndex, 255);
}
}
{
{
typename TImage::IndexType pixelIndex = {{r,c}};
image->SetPixel(pixelIndex, 255);
}
}
}