ITK  4.9.0
Insight Segmentation and Registration Toolkit
WikiExamples/ImageProcessing/BinaryThinningImageFilter.cxx
#include "itkImage.h"
typedef itk::Image<unsigned char, 2> ImageType;
static void WriteImage(const ImageType::Pointer image, const std::string& fileName);
static void CreateImage(ImageType::Pointer image);
int main(int argc, char *argv[])
{
ImageType::Pointer image = ImageType::New();
if( argc == 2)
{
typedef itk::ImageFileReader<ImageType> ImageReader;
ImageReader::Pointer reader = ImageReader::New();
std::string fileName = argv[1];
reader->SetFileName(fileName);
reader->Update();
image = reader->GetOutput();
}
else
{
CreateImage(image);
WriteImage(image, "input.png");
}
typedef itk::BinaryThinningImageFilter <ImageType, ImageType> BinaryThinningImageFilterType;
BinaryThinningImageFilterType::Pointer binaryThinningImageFilter = BinaryThinningImageFilterType::New();
binaryThinningImageFilter->SetInput(image);
binaryThinningImageFilter->Update();
// Rescale the image so that it can be seen (the output is 0 and 1, we want 0 and 255)
RescaleType::Pointer rescaler = RescaleType::New();
rescaler->SetInput( binaryThinningImageFilter->GetOutput() );
rescaler->SetOutputMinimum(0);
rescaler->SetOutputMaximum(255);
rescaler->Update();
WriteImage(rescaler->GetOutput(), "output.png");
return EXIT_SUCCESS;
}
void CreateImage(ImageType::Pointer image)
{
// Create an image
ImageType::IndexType start;
start.Fill(0);
ImageType::SizeType size;
size.Fill(100);
ImageType::RegionType region(start,size);
image->SetRegions(region);
image->Allocate();
image->FillBuffer(0);
// Draw a 5 pixel wide line
for(unsigned int i = 20; i < 80; ++i)
{
for(unsigned int j = 50; j < 55; ++j)
{
index[0] = i;
index[1] = j;
image->SetPixel(index, 255);
}
}
}
void WriteImage(const ImageType::Pointer image, const std::string& fileName)
{
WriterType::Pointer writer = WriterType::New();
writer->SetFileName(fileName);
writer->SetInput(image);
writer->Update();
}