From d0ff194b5a6971111ccc82743dd8b31de768a452 Mon Sep 17 00:00:00 2001 From: Jonathan Guinet <jonathan.guinet@c-s.fr> Date: Fri, 11 May 2012 16:06:58 +0200 Subject: [PATCH] ENH: LargeScaleSegmentation application, only EDISONBased meanshift choice is implemented (WIP). --- .../otbLargeScaleSegmentation.cxx | 166 +++++++++++++++++- 1 file changed, 159 insertions(+), 7 deletions(-) diff --git a/Applications/Segmentation/otbLargeScaleSegmentation.cxx b/Applications/Segmentation/otbLargeScaleSegmentation.cxx index 0711e858da..7707cf30a6 100644 --- a/Applications/Segmentation/otbLargeScaleSegmentation.cxx +++ b/Applications/Segmentation/otbLargeScaleSegmentation.cxx @@ -18,8 +18,26 @@ #include "otbWrapperApplication.h" #include "otbWrapperApplicationFactory.h" +// Segmentation filters includes #include "otbMeanShiftVectorImageFilter.h" + +#include "otbStreamingVectorizedSegmentationOGR.h" + +//#include "otbPersistentImageToOGRDataFilter.h" +//#include "otbPersistentFilterStreamingDecorator.h" + +#include "otbConnectedComponentMuParserFunctor.h" +#include <itkConnectedComponentFunctorImageFilter.h> + +#include "otbParser.h" + +#include "otbOGRDataSourceWrapper.h" + +#include "otbMultiToMonoChannelExtractROI.h" +#include "otbObjectList.h" + + namespace otb { namespace Wrapper @@ -35,7 +53,27 @@ public: typedef itk::SmartPointer<Self> Pointer; typedef itk::SmartPointer<const Self> ConstPointer; - typedef otb::MeanShiftVectorImageFilter<FloatVectorImageType, FloatVectorImageType> MSFilterType; + typedef UInt32ImageType LabelImageType; + typedef UInt32ImageType MaskImageType; + + + typedef MultiToMonoChannelExtractROI<FloatVectorImageType::InternalPixelType, + MaskImageType::PixelType> ExtractROIFilterType; + typedef ObjectList<ExtractROIFilterType> ExtractROIFilterListType; + + // typedef itk::ImageToImageFilter<FloatVectorImageType,UInt32ImageType> SegmentationFilterType; + typedef otb::MeanShiftVectorImageFilter<FloatVectorImageType,FloatVectorImageType,LabelImageType> MSEDISONSegmentationFilterType; + + + // TODO replace by new meanshift segmentation scheme + //typedef otb::MeanShiftConnectedComponentSegmentationFilter< FloatVectorImageType, MaskImageType,LabelImageType > MeanShiftConnectedComponentsegmentationFilterType; + // typedef otb::MeanShiftImageFilter2<FloatVectorImageType, LabelImageType> MeanShiftFilterType; + + + + + typedef otb::StreamingVectorizedSegmentationOGR<FloatVectorImageType, MSEDISONSegmentationFilterType> MSEDIONStreamingVectorizedSegmentationOGRType; + /** Standard macro */ itkNewMacro(Self); @@ -64,12 +102,12 @@ private: SetParameterDescription( "inmask", "Mask image. (Pixel with 0 will not be processed)." ); MandatoryOff("inmask"); - AddParameter(ParameterType_OutputVectorData,"outvd", "Output VectorData"); + AddParameter(ParameterType_OutputFilename,"outvd", "Output VectorData"); SetParameterDescription( "outvd", "The name of output Vector Data." ); - //AddParameter(ParameterType_OutputImage, "lout", "Labeled output"); - //SetParameterDescription( "lout", "The labeled output image." ); - //MandatoryOff("lout"); + AddParameter(ParameterType_OutputImage, "lout", "Labeled output"); + SetParameterDescription( "lout", "The labeled output image." ); + MandatoryOff("lout"); // AddParameter(ParameterType_Choice, "filter", "Segmentation Filter"); @@ -78,14 +116,33 @@ private: AddChoice("filter.meanshiftedison", "EDISON MeanShift"); SetParameterDescription("filter.meanshiftedison", "EDISON based MeanShift filter. (is going to be replaced by newframework)."); // EDISON Meanshift Parameters + AddParameter(ParameterType_Int, "filter.meanshiftedison.spatialr", "Spatial radius"); + SetParameterDescription( "filter.meanshiftedison.spatialr", "Spatial radius defining neighborhood." ); + AddParameter(ParameterType_Float, "filter.meanshiftedison.ranger", "Range radius"); + SetParameterDescription( "filter.meanshiftedison.ranger", "Range radius defining the interval in the color space." ); + AddParameter(ParameterType_Int, "filter.meanshiftedison.minsize", "Min region size"); + SetParameterDescription( "filter.meanshiftedison.minsize", "Minimun size of a region to be kept after clustering." ); + AddParameter(ParameterType_Float, "filter.meanshiftedison.scale", "Scale"); + SetParameterDescription( "filter.meanshiftedison.scale", "Scale to stretch the image before processing." ); + SetDefaultParameterInt("filter.meanshiftedison.spatialr", 5); + SetDefaultParameterFloat("filter.meanshiftedison.ranger", 15.0); + SetDefaultParameterInt("filter.meanshiftedison.minsize", 100); + SetDefaultParameterFloat("filter.meanshiftedison.scale", 100000.); AddChoice("filter.meanshift", "MeanShift"); - SetParameterDescription("filter.meanshiftedison", "MeanShift filter."); + SetParameterDescription("filter.meanshift", "MeanShift filter."); // MeanShift Parameters AddChoice("filter.connectedcomponent", "ConnectedComponentMuParser"); SetParameterDescription("filter.connectedcomponent", "connectedComponant muparser filter."); // CC parameters + AddParameter(ParameterType_String, "filter.connectedcomponent.mask", "Mask expression"); + SetParameterDescription("filter.connectedcomponent.mask", "Mask mathematical expression (only if support image is given)"); + MandatoryOff("filter.connectedcomponent.mask"); + DisableParameter("filter.connectedcomponent.mask"); + + AddParameter(ParameterType_String, "filter.connectedcomponent.expr", "Connected Component Expression"); + SetParameterDescription("filter.connectedcomponent.expr", "Formula used for connected component segmentation"); AddParameter(ParameterType_Group, "relabel", "Relabel Output."); @@ -148,9 +205,104 @@ private: void DoExecute() { - } + // + std::string dataSourceName =GetParameterString("outvd"); + otb::ogr::DataSource::Pointer ogrDS = otb::ogr::DataSource::New(dataSourceName, otb::ogr::DataSource::Modes::write); + + //mask image transform vector image to LabelImage + if(HasValue("inmask")) + { + m_MaskImage=this->GetParameterUInt32Image("inmask"); + m_MaskImage->UpdateOutputInformation(); + } + + const unsigned int tileSize = static_cast<unsigned int>(this->GetParameterInt("tilesize")); + std::string layerName =this->GetParameterString("layername"); + std::string fieldName =this->GetParameterString("fieldname"); + const unsigned int startLabel=this->GetParameterInt("startlabel"); + bool use8connected=!HasValue("neighbor"); + + + switch (GetParameterInt("filter")) + { + // MeanShiftEDISON basedfiltering + case 0: + { + MSEDIONStreamingVectorizedSegmentationOGRType::Pointer EDISONVectorizationFilter=MSEDIONStreamingVectorizedSegmentationOGRType::New(); + m_VectorizationFilter=EDISONVectorizationFilter; + + + EDISONVectorizationFilter->SetInput(GetParameterFloatVectorImage("in")); + if(HasValue("inmask")) + { + EDISONVectorizationFilter->SetInputMask(this->GetParameterUInt32Image("inmask")); + } + EDISONVectorizationFilter->SetOGRDataSource(ogrDS); + + //filter->GetStreamer()->SetNumberOfLinesStrippedStreaming(atoi(argv[3])); + if(tileSize!=0) + EDISONVectorizationFilter->GetStreamer()->SetTileDimensionTiledStreaming(tileSize); + else + EDISONVectorizationFilter->GetStreamer()->SetAutomaticTiledStreaming(); + + EDISONVectorizationFilter->SetLayerName(layerName); + EDISONVectorizationFilter->SetFieldName(fieldName); + EDISONVectorizationFilter->SetStartLabel(startLabel); + EDISONVectorizationFilter->SetUse8Connected(use8connected); + + //segmentation paramters + const unsigned int spatialRadius=static_cast<unsigned int>(this->GetParameterInt("filter.meanshiftedison.spatialr")); + const unsigned int rangeRadius=static_cast<unsigned int>(this->GetParameterInt("filter.meanshiftedison.ranger")); + const unsigned int minimumObjectSize= static_cast<unsigned int>(this->GetParameterInt("filter.meanshiftedison.minsize")); + const float scale=this->GetParameterFloat("filter.meanshiftedison.scale"); + + EDISONVectorizationFilter->GetSegmentationFilter()->SetSpatialRadius(spatialRadius); + EDISONVectorizationFilter->GetSegmentationFilter()->SetRangeRadius(rangeRadius); + EDISONVectorizationFilter->GetSegmentationFilter()->SetMinimumRegionSize(minimumObjectSize); + EDISONVectorizationFilter->GetSegmentationFilter()->SetScale(scale); + + EDISONVectorizationFilter->Initialize(); //must be called ! + m_VectorizationFilter->Update(); + m_LabelImage=EDISONVectorizationFilter->GetSegmentationFilter()->GetLabeledClusteredOutput(); + + break; + + } + //MeanShift filter + case 1: + { + + break; + } + // CC filter + case 2: + { + + break; + } + + + + default: + { + otbAppLogFATAL(<<"non defined filtering method "<<GetParameterInt("filter")<<std::endl); + break; + + } + return; + } + + + //m_VectorizationFilter->Update(); + SetParameterOutputImage<LabelImageType>("lout", m_LabelImage); + + } + itk::ProcessObject::Pointer m_SegmentationFilter; + itk::ProcessObject::Pointer m_VectorizationFilter; + MaskImageType::Pointer m_MaskImage; + LabelImageType::Pointer m_LabelImage; }; -- GitLab