https://vtk.org/Wiki/ITK/Examples/Segmentation/WatershedImageFilter
https://itk.org/Doxygen/html/Examples_2Segmentation_2WatershedSegmentation1_8cxx-example.html
这两个都可以参考,结果最后是转化为RGB彩图的,大致效果是这样的,里面参数threshold、level可以调试。
主要的代码是这些:
typedef float PixelType;
const unsigned int Dim = 2;
typedef itk::Image< PixelType, Dim > floatImageType;
typedef itk::RGBPixel<unsigned char> RGBPixelType;
typedef itk::Image<RGBPixelType, Dim> RGBImageType;
typedef itk::Image<itk::IdentifierType, Dim> LabeledImageType;
typedef itk::MedianImageFilter<floatImageType,floatImageType> MedianFilterType;
MedianFilterType::Pointer medianFilter = MedianFilterType::New();
medianFilter->SetRadius(3);
medianFilter->SetInput(intensityFilter->GetOutput());
medianFilter->Update();
typedef itk::GradientMagnitudeRecursiveGaussianImageFilter<floatImageType,floatImageType > GradientFilterType;
GradientFilterType::Pointer gradientMagnitude = GradientFilterType::New();
gradientMagnitude->SetInput( medianFilter->GetOutput() );
gradientMagnitude->SetSigma( 1 );
gradientMagnitude->Update();
// Custom parameters
typedef itk::WatershedImageFilter<floatImageType> WatershedFilterType;
WatershedFilterType::Pointer watershed = WatershedFilterType::New();
watershed->SetThreshold(0.003);
watershed->SetLevel(0.25);
watershed->SetInput(gradientMagnitude->GetOutput());
watershed->Update();
typedef itk::ScalarToRGBColormapImageFilter<LabeledImageType, RGBImageType> RGBFilterType;
RGBFilterType::Pointer colormapImageFilter = RGBFilterType::New();
colormapImageFilter->SetInput(watershed->GetOutput());
colormapImageFilter->SetColormap( RGBFilterType::Jet );
colormapImageFilter->Update();