otbFillSegmentationHoles.cxx 4.05 KiB
/*
 * Author: Raffaele GAETANO
 * Affiliation: CIRAD, UMR TETIS
 */
#include "otbWrapperApplication.h"
#include "otbWrapperApplicationFactory.h"
#include "otbStreamingStatisticsImageFilter.h"
#include "itkConnectedComponentImageFilter.h"
#include "otbBandMathImageFilter.h"
#include <string>
namespace otb
namespace Wrapper
class FillSegmentationHoles : public Application
public:
  typedef FillSegmentationHoles         Self;
  typedef Application                   Superclass;
  typedef itk::SmartPointer<Self>       Pointer;
  typedef itk::SmartPointer<const Self> ConstPointer;
  typedef UInt32ImageType               LabelImageType;
  typedef itk::ConnectedComponentImageFilter<LabelImageType, LabelImageType> CCLFIlterType;
  typedef otb::BandMathImageFilter<LabelImageType> BandMathFilterType;
  typedef otb::StreamingStatisticsImageFilter<LabelImageType> StatsFilterType;
  itkNewMacro(Self);
  itkTypeMacro(Segmentation, otb::Application);
private:
  void DoInit() override
    SetName("FillSegmentationHoles");
    SetDescription("This application performs ...");
    SetDocLongDescription(
        "..."
        "...");
    SetDocLimitations("This application is proposed as part of the LSGRM Remote Module from Remi Cresson / Pierre Lassalle.");
    SetDocAuthors("Raffaele Gaetano");
    AddDocTag(Tags::Segmentation);
    AddDocTag("LSGRM");
    AddParameter(ParameterType_InputImage, "in", "Input segmentation");
    SetParameterDescription("in", "The input segmentation label image.");
    AddParameter(ParameterType_InputImage, "mask", "Input validity mask");
    SetParameterDescription("mask",
                            "Optional mask to indicate which pixels are valid (any non-zero value).");
    MandatoryOff("mask");
    DisableParameter("mask");
    AddParameter(ParameterType_OutputImage, "out", "Output segmentation");
    SetParameterDescription("out",
                            "The output segmentation with filled holes.");
    AddRAMParameter();
    // Doc example parameter settings
    SetDocExampleParameterValue("in", "seg.tif");
    SetDocExampleParameterValue("out", "seg_filled.tif");
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
} void DoUpdateParameters() override { } void DoExecute() override { LabelImageType::Pointer maskIn; LabelImageType::Pointer maskedIn; LabelImageType::Pointer labelIn = GetParameterUInt32Image("in"); m_MaskFilter = BandMathFilterType::New(); if (HasValue("mask")) { maskIn = GetParameterUInt32Image("mask"); m_MaskFilter->SetNthInput(0, labelIn); m_MaskFilter->SetNthInput(1, maskIn); m_MaskFilter->SetExpression("b2!=0 ? b1 : 0"); maskedIn = m_MaskFilter->GetOutput(); } else maskedIn = labelIn; m_StatsFilter = StatsFilterType::New(); m_StatsFilter->SetInput(maskedIn); m_StatsFilter->Update(); long unsigned M = m_StatsFilter->GetMaximum(); std::cout << "Hole numbering starts at : " << 1 + M << std::endl; m_BandMathFilter = BandMathFilterType::New(); m_BandMathFilter->SetNthInput(0, maskedIn); if (HasValue("mask")) { m_BandMathFilter->SetNthInput(1, maskIn); m_BandMathFilter->SetExpression("b1==0 && b2!=0"); } else m_BandMathFilter->SetExpression("b1==0"); m_CCLFilter = CCLFIlterType::New(); m_CCLFilter->SetInput(m_BandMathFilter->GetOutput()); m_OutBandMathFilter = BandMathFilterType::New(); m_OutBandMathFilter->SetNthInput(0, maskedIn); m_OutBandMathFilter->SetNthInput(1, m_CCLFilter->GetOutput()); std::string expr; if (HasValue("mask")) { m_OutBandMathFilter->SetNthInput(2, maskIn); expr = "b1==0 && b3!=0 ? " + std::to_string(M) + " + b2 : b1"; } else expr = "b1==0 ? " + std::to_string(M) + " + b2 : b1"; m_OutBandMathFilter->SetExpression(expr); SetParameterOutputImage("out", m_OutBandMathFilter->GetOutput()); } BandMathFilterType::Pointer m_MaskFilter; CCLFIlterType::Pointer m_CCLFilter; BandMathFilterType::Pointer m_BandMathFilter; StatsFilterType::Pointer m_StatsFilter; BandMathFilterType::Pointer m_OutBandMathFilter; }; } } OTB_APPLICATION_EXPORT(otb::Wrapper::FillSegmentationHoles)