Skip to content
Snippets Groups Projects
Commit 164ef41b authored by Arnaud Jaen's avatar Arnaud Jaen
Browse files

ENH: Remove otbStreamingVectorizedSegmentation filter. See otbStreamingVectorizedSegmentationOGR

parent 7242220f
No related branches found
No related tags found
No related merge requests found
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
Some parts of this code are derived from ITK. See ITKCopyright.txt
for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef __otbStreamingVectorizedSegmentation_h
#define __otbStreamingVectorizedSegmentation_h
#include <vector>
#include "itkImageRegion.h"
#include "otbVectorData.h"
#include "itkPreOrderTreeIterator.h"
#include "itkDataObject.h"
#include "itkDataObjectDecorator.h"
#include "itkSimpleDataObjectDecorator.h"
#include "otbLabelImageToVectorDataFilter.h"
#include "itkExtractImageFilter.h"
#include "otbPersistentImageFilter.h"
#include "otbPersistentFilterStreamingDecorator.h"
#include "otbPersistentImageToVectorDataFilter.h"
#include "otbMeanShiftImageFilter.h"
#include "otbMeanShiftVectorImageFilter.h"
#include "itkMacro.h"
namespace otb
{
/**
* \class LabeledOutputAccessor
* \brief Accessor to the index of the labeled output image of the Template Filter.
*/
template <class TFilter>
class LabeledOutputAccessor
{
public:
typedef typename TFilter::OutputImageType LabelImageType;
itkStaticConstMacro(LabeledOutputIndex, unsigned int, 0);
};
/**
* \class LabeledOutputAccessor
* \brief Specialized class to get the index of the labeled output image in mean shift filter.
*/
template <class TInputImage, class TOutputImage, class TLabeledImage, class TBufferConverter>
class LabeledOutputAccessor<MeanShiftImageFilter<TInputImage, TOutputImage, TLabeledImage, TBufferConverter> >
{
public:
typedef typename MeanShiftImageFilter<TInputImage, TOutputImage, TLabeledImage, TBufferConverter>::LabeledOutputType LabelImageType;
itkStaticConstMacro(LabeledOutputIndex, unsigned int, 2);
};
/**
* \class LabeledOutputAccessor
* \brief Specialized class to get the index of the labeled output image in mean shift vector image filter.
*/
template <class TInputImage, class TOutputImage, class TLabeledImage>
class LabeledOutputAccessor<MeanShiftVectorImageFilter<TInputImage, TOutputImage, TLabeledImage> >
{
public:
typedef typename MeanShiftImageFilter<TInputImage, TOutputImage, TLabeledImage>::LabeledOutputType LabelImageType;
itkStaticConstMacro(LabeledOutputIndex, unsigned int, 2);
};
/** \class PersistentStreamingLabelImageToVectorDataFilter
* \brief this class uses GDALPolygonize method to transform a Label image into a VectorData.
*
* This filter is a generic PersistentImageFilter, which encapsulate
* the LabelImageToVectorData filter.
*
* \sa PersistentImageToVectorDataFilter
*
*/
template <class TImageType, class TOutputVectorData, class TSegmentationFilter>
class PersistentStreamingLabelImageToVectorDataFilter
: public otb::PersistentImageToVectorDataFilter<TImageType,TOutputVectorData >
{
public:
/** Standard Self typedef */
typedef PersistentStreamingLabelImageToVectorDataFilter Self;
typedef PersistentImageToVectorDataFilter<TImageType, TOutputVectorData > Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
typedef typename Superclass::InputImageType InputImageType;
typedef typename Superclass::InputImagePointer InputImagePointerType;
typedef typename Superclass::OutputVectorDataType OutputVectorDataType;
typedef typename Superclass::OutputVectorDataPointerType OutputVectorDataPointerType;
typedef typename OutputVectorDataType::DataTreeType::TreeNodeType::ChildrenListType ChildrenListType;
typedef typename OutputVectorDataType::DataTreeType::TreeNodeType TreeNodeType;
typedef TSegmentationFilter SegmentationFilterType;
typedef typename LabeledOutputAccessor<SegmentationFilterType>::LabelImageType LabelImageType;
typedef typename LabelImageType::PixelType LabelPixelType;
typedef otb::LabelImageToVectorDataFilter<LabelImageType,
typename OutputVectorDataType::PrecisionType> LabelImageToVectorDataFilterType;
/** Method for creation through the object factory. */
itkNewMacro(Self);
/** Runtime information support. */
itkTypeMacro(PersistentStreamingLabelImageToVectorDataFilter, PersistentImageToVectorDataFilter);
itkGetObjectMacro(SegmentationFilter, SegmentationFilterType);
itkSetMacro(FieldName, std::string);
itkGetMacro(FieldName, std::string);
void SetStartLabel(const LabelPixelType & label)
{
m_StartLabel = label;
m_TileMaxLabel = label;
}
itkGetMacro(StartLabel, LabelPixelType);
protected:
PersistentStreamingLabelImageToVectorDataFilter();
virtual ~PersistentStreamingLabelImageToVectorDataFilter();
void GenerateInputRequestedRegion();
private:
PersistentStreamingLabelImageToVectorDataFilter(const Self &); //purposely not implemented
void operator =(const Self&); //purposely not implemented
virtual OutputVectorDataPointerType ProcessTile();
std::string m_FieldName;
LabelPixelType m_TileMaxLabel;
LabelPixelType m_StartLabel;
typename SegmentationFilterType::Pointer m_SegmentationFilter;
unsigned int m_TileNumber;
};
template <class TImageType, class TOutputVectorData, class TSegmentationFilter>
class ITK_EXPORT StreamingVectorizedSegmentation :
public PersistentFilterStreamingDecorator<PersistentStreamingLabelImageToVectorDataFilter<TImageType, TOutputVectorData, TSegmentationFilter> >
{
public:
/** Standard Self typedef */
typedef StreamingVectorizedSegmentation Self;
typedef PersistentFilterStreamingDecorator
<PersistentStreamingLabelImageToVectorDataFilter<TImageType, TOutputVectorData, TSegmentationFilter> > Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
/** Type macro */
itkNewMacro(Self);
/** Creation through object factory macro */
itkTypeMacro(StreamingVectorizedSegmentation, PersistentFilterStreamingDecorator);
typedef TSegmentationFilter SegmentationFilterType;
typedef TImageType InputImageType;
typedef TOutputVectorData OutputVectorDataType;
typedef typename PersistentStreamingLabelImageToVectorDataFilter<TImageType, TOutputVectorData, TSegmentationFilter>::LabelPixelType LabelPixelType;
void SetInput(InputImageType * input)
{
this->GetFilter()->SetInput(input);
}
const InputImageType * GetInput()
{
return this->GetFilter()->GetInput();
}
SegmentationFilterType * GetSegmentationFilter()
{
return this->GetFilter()->GetSegmentationFilter();
}
const OutputVectorDataType * GetOutputVectorData()
{
return this->GetFilter()->GetOutputVectorData();
}
void SetFieldName(const std::string & field)
{
this->GetFilter()->SetFieldName(field);
}
const std::string & GetFieldName()
{
return this->GetFilter()->GetFieldName();
}
void SetStartLabel(const LabelPixelType & label)
{
this->GetFilter()->SetStartLabel(label);
}
const LabelPixelType & GetStartLabel()
{
return this->GetFilter()->GetStartLabel();
}
void SetFileName(const std::string & fileName)
{
this->GetFilter()->SetFileName(fileName);
}
protected:
/** Constructor */
StreamingVectorizedSegmentation() {}
/** Destructor */
virtual ~StreamingVectorizedSegmentation() {}
private:
StreamingVectorizedSegmentation(const Self &); //purposely not implemented
void operator =(const Self&); //purposely not implemented
};
}
#ifndef OTB_MANUAL_INSTANTIATION
#include "otbStreamingVectorizedSegmentation.txx"
#endif
#endif
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
Some parts of this code are derived from ITK. See ITKCopyright.txt
for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef __otbStreamingVectorizedSegmentation_txx
#define __otbStreamingVectorizedSegmentation_txx
#include "otbStreamingVectorizedSegmentation.h"
#include "otbVectorDataTransformFilter.h"
#include "itkAffineTransform.h"
#include "itkTimeProbe.h"
namespace otb
{
template <class TImageType, class TOutputVectorData, class TSegmentationFilter>
PersistentStreamingLabelImageToVectorDataFilter<TImageType, TOutputVectorData, TSegmentationFilter>
::PersistentStreamingLabelImageToVectorDataFilter() : m_FieldName("DN"), m_TileMaxLabel(0), m_StartLabel(0)
{
m_SegmentationFilter = SegmentationFilterType::New();
m_TileNumber = 1;
}
template <class TImageType, class TOutputVectorData, class TSegmentationFilter>
PersistentStreamingLabelImageToVectorDataFilter<TImageType, TOutputVectorData, TSegmentationFilter>
::~PersistentStreamingLabelImageToVectorDataFilter()
{
}
template <class TImageType, class TOutputVectorData, class TSegmentationFilter>
void
PersistentStreamingLabelImageToVectorDataFilter<TImageType, TOutputVectorData, TSegmentationFilter>
::GenerateInputRequestedRegion()
{
Superclass::GenerateInputRequestedRegion();
if (this->GetInput())
{
InputImagePointerType input = const_cast<InputImageType *> (this->GetInput());
typename InputImageType::RegionType region = this->GetOutput()->GetRequestedRegion();
region.PadByRadius(1);
region.Crop(input->GetLargestPossibleRegion());
input->SetRequestedRegion(region);
}
}
template <class TImageType, class TOutputVectorData, class TSegmentationFilter>
typename PersistentStreamingLabelImageToVectorDataFilter<TImageType, TOutputVectorData, TSegmentationFilter>::OutputVectorDataPointerType
PersistentStreamingLabelImageToVectorDataFilter<TImageType, TOutputVectorData, TSegmentationFilter>
::ProcessTile()
{
std::cout<< "tile number : " << m_TileNumber <<std::endl;
m_TileNumber = m_TileNumber + 1;
itk::TimeProbe tileChrono;
tileChrono.Start();
itk::TimeProbe chrono;
chrono.Start();
// Apply an ExtractImageFilter to avoid problems with filters asking for the LargestPossibleRegion
typedef itk::ExtractImageFilter<InputImageType, InputImageType> ExtractImageFilterType;
typename ExtractImageFilterType::Pointer extract = ExtractImageFilterType::New();
extract->SetInput( this->GetInput() );
extract->SetExtractionRegion( this->GetInput()->GetBufferedRegion() );
extract->Update();
chrono.Stop();
//std::cout<< "extract took " << chrono.GetTotal() << " sec"<<std::endl;
// WARNING: itk::ExtractImageFilter does not copy the MetadataDictionnary
extract->GetOutput()->SetMetaDataDictionary(this->GetInput()->GetMetaDataDictionary());
const unsigned int labelImageIndex = LabeledOutputAccessor<SegmentationFilterType>::LabeledOutputIndex;
typename LabelImageToVectorDataFilterType::Pointer labelImageToVectorDataFilter = LabelImageToVectorDataFilterType::New();
/*typename SegmentationFilterType::Pointer segFilter = SegmentationFilterType::New();
segFilter->SetInput(extract->GetOutput());
segFilter->GetFunctor().SetExpression("distance<40");
segFilter->Update();*/
itk::TimeProbe chrono1;
chrono1.Start();
m_SegmentationFilter->SetInput(extract->GetOutput());
//m_SegmentationFilter->ResetPipeline();
//m_SegmentationFilter->Modified();
m_SegmentationFilter->UpdateLargestPossibleRegion();
m_SegmentationFilter->Update();
chrono1.Stop();
std::cout<< "segmentation took " << chrono1.GetTotal() << " sec"<<std::endl;
itk::TimeProbe chrono2;
chrono2.Start();
//labelImageToVectorDataFilter->SetInput(dynamic_cast<LabelImageType *>(segFilter->GetOutputs().at(labelImageIndex).GetPointer()));
labelImageToVectorDataFilter->SetInput(dynamic_cast<LabelImageType *>(m_SegmentationFilter->GetOutputs().at(labelImageIndex).GetPointer()));
labelImageToVectorDataFilter->SetFieldName(m_FieldName);
labelImageToVectorDataFilter->Update();
chrono2.Stop();
std::cout<< "vectorization took " << chrono2.GetTotal() << " sec"<<std::endl;
//Relabel the vector data
itk::TimeProbe chrono3;
chrono3.Start();
typename TreeNodeType::Pointer rootNode = const_cast<TreeNodeType *>(labelImageToVectorDataFilter->GetOutput()->GetDataTree()->GetRoot());
ChildrenListType childList = rootNode->GetChildrenList()[0]->GetChildrenList();
std::vector<int> fieldValueVector;
for (typename ChildrenListType::iterator it = childList.begin(); it != childList.end(); ++it)
{
typename OutputVectorDataType::DataNodePointerType dataNode = (*it)->Get();
fieldValueVector.push_back(dataNode->GetFieldAsInt(m_FieldName));
}
std::sort(fieldValueVector.begin(), fieldValueVector.end());
std::map<int,int> relabelMap;
unsigned int i = 0;
unsigned int ind = 0;
for(i = 0; i < fieldValueVector.size(); i++)
{
if (relabelMap.find(fieldValueVector.at(i)) == relabelMap.end())
{
relabelMap[fieldValueVector.at(i)] = static_cast<int>(ind);
ind = ind + 1;
}
}
for (typename ChildrenListType::iterator it = childList.begin(); it != childList.end(); ++it)
{
typename OutputVectorDataType::DataNodePointerType dataNode = (*it)->Get();
int newLabel = relabelMap[dataNode->GetFieldAsInt(m_FieldName)] + m_TileMaxLabel;
dataNode->SetFieldAsInt(m_FieldName, newLabel);
}
m_TileMaxLabel = m_TileMaxLabel + fieldValueVector.size();
chrono3.Stop();
//std::cout<< "relabel took " << chrono3.GetTotal() << " sec"<<std::endl;
tileChrono.Stop();
std::cout<< "tile processing took " << tileChrono.GetTotal() << " sec"<<std::endl;
// return the VectorData in image physical coordinates
return labelImageToVectorDataFilter->GetOutput();
}
} // end namespace otb
#endif
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "otbVectorImage.h"
#include "otbStreamingVectorizedSegmentation.h"
#include "otbImageFileReader.h"
#include "otbVectorDataFileWriter.h"
#include "otbVectorData.h"
#include "otbMeanShiftVectorImageFilter.h"
#include "itkConnectedComponentFunctorImageFilter.h"
#include "otbConnectedComponentMuParserFunctor.h"
#include "otbPersistentImageToVectorDataFilter.h"
#include "otbPersistentFilterStreamingDecorator.h"
int otbStreamingVectorizedSegmentationNew(int argc, char * argv[])
{
typedef float InputPixelType;
const unsigned int Dimension = 2;
/** Typedefs */
typedef otb::Image<InputPixelType, Dimension> ImageType;
typedef otb::VectorData<double, 2> VectorDataType;
typedef otb::MeanShiftImageFilter<ImageType, ImageType> MeanShiftImageFilterType;
typedef otb::StreamingVectorizedSegmentation<ImageType, VectorDataType, MeanShiftImageFilterType>::FilterType StreamingVectorizedSegmentationType;
StreamingVectorizedSegmentationType::Pointer filter = StreamingVectorizedSegmentationType::New();
std::cout << filter << std::endl;
return EXIT_SUCCESS;
}
int otbStreamingVectorizedSegmentation(int argc, char * argv[])
{
typedef float InputPixelType;
const unsigned int Dimension = 2;
const std::string fieldName("DN");
// Typedefs
typedef otb::VectorImage<InputPixelType, Dimension> ImageType;
typedef otb::VectorData<double, 2> VectorDataType;
typedef otb::Image<unsigned int, Dimension> LabelImageType;
typedef otb::MeanShiftVectorImageFilter<ImageType, ImageType, LabelImageType> MeanShiftImageFilterType;
typedef otb::Functor::ConnectedComponentMuParserFunctor<ImageType::PixelType> FunctorType;
typedef itk::ConnectedComponentFunctorImageFilter<ImageType, LabelImageType, FunctorType, LabelImageType > SegmentationFilterType;
typedef otb::StreamingVectorizedSegmentation<ImageType, VectorDataType, SegmentationFilterType> StreamingVectorizedSegmentationType;
//typedef otb::StreamingVectorizedSegmentation<ImageType, VectorDataType, MeanShiftImageFilterType> StreamingVectorizedSegmentationType;
typedef otb::ImageFileReader<ImageType> ReaderType;
typedef otb::VectorDataFileWriter<VectorDataType> WriterType;
ReaderType::Pointer reader = ReaderType::New();
StreamingVectorizedSegmentationType::Pointer filter = StreamingVectorizedSegmentationType::New();
WriterType::Pointer writer = WriterType::New();
reader->SetFileName(argv[1]);
reader->GenerateOutputInformation();
filter->SetInput(reader->GetOutput());
//filter->GetStreamer()->SetNumberOfLinesStrippedStreaming(atoi(argv[3]));
filter->GetStreamer()->SetTileDimensionTiledStreaming(atoi(argv[3]));
filter->SetFieldName(fieldName);
filter->SetStartLabel(1);
/*filter->GetSegmentationFilter()->SetSpatialRadius(10);
filter->GetSegmentationFilter()->SetRangeRadius(15);
filter->GetSegmentationFilter()->SetMinimumRegionSize(400);*/
filter->GetSegmentationFilter()->GetFunctor().SetExpression("distance<40");
filter->SetFileName(argv[2]);
filter->Update();
std::cout<< "begin writing ...." <<std::endl;
writer->SetFileName(argv[2]);
writer->SetInput(filter->GetOutputVectorData());
writer->Update();
std::cout<< "end writing ...." <<std::endl;
return EXIT_SUCCESS;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment