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

ENH: Add option 4Connected/8Connected to GDALPolygonize in...

ENH: Add option 4Connected/8Connected to GDALPolygonize in LabelImageToVectorDataFilter and LabelImageToOGRDataSourceFilter
parent a0bb7648
Branches
Tags
No related merge requests found
...@@ -75,6 +75,9 @@ public: ...@@ -75,6 +75,9 @@ public:
itkSetMacro(FieldName, std::string); itkSetMacro(FieldName, std::string);
itkGetMacro(FieldName, std::string); itkGetMacro(FieldName, std::string);
itkSetMacro(Use8Connected, bool);
itkGetMacro(Use8Connected, bool);
const OGRDataSourceObjectType * GetOutput(); const OGRDataSourceObjectType * GetOutput();
protected: protected:
...@@ -96,6 +99,7 @@ private: ...@@ -96,6 +99,7 @@ private:
void operator =(const Self&); //purposely not implemented void operator =(const Self&); //purposely not implemented
std::string m_FieldName; std::string m_FieldName;
bool m_Use8Connected;
}; };
......
...@@ -80,7 +80,7 @@ private: ...@@ -80,7 +80,7 @@ private:
template <class TInputImage> template <class TInputImage>
LabelImageToOGRDataSourceFilter<TInputImage> LabelImageToOGRDataSourceFilter<TInputImage>
::LabelImageToOGRDataSourceFilter() : m_FieldName("DN") ::LabelImageToOGRDataSourceFilter() : m_FieldName("DN"), m_Use8Connected(false)
{ {
this->SetNumberOfRequiredInputs(1); this->SetNumberOfRequiredInputs(1);
this->SetNumberOfRequiredOutputs(1); this->SetNumberOfRequiredOutputs(1);
...@@ -229,7 +229,17 @@ LabelImageToOGRDataSourceFilter<TInputImage> ...@@ -229,7 +229,17 @@ LabelImageToOGRDataSourceFilter<TInputImage>
outputLayer->CreateField(&field, true); outputLayer->CreateField(&field, true);
//Call GDALPolygonize() //Call GDALPolygonize()
GDALPolygonize(dataset->GetRasterBand(1), NULL, outputLayer, 0, NULL, NULL, NULL); char ** options;
options = NULL;
char * option[1];
if (m_Use8Connected == true)
{
std::string opt("8CONNECTED:8");
option[0] = const_cast<char *>(opt.c_str());
options=option;
}
GDALPolygonize(dataset->GetRasterBand(1), NULL, outputLayer, 0, options, NULL, NULL);
OGRDataSourceObjectType * decoratedOutput = OGRDataSourceObjectType * decoratedOutput =
......
...@@ -82,6 +82,8 @@ public: ...@@ -82,6 +82,8 @@ public:
itkSetMacro(FieldName, std::string); itkSetMacro(FieldName, std::string);
itkGetMacro(FieldName, std::string); itkGetMacro(FieldName, std::string);
itkSetMacro(Use8Connected, bool);
itkGetMacro(Use8Connected, bool);
protected: protected:
LabelImageToVectorDataFilter(); LabelImageToVectorDataFilter();
...@@ -97,6 +99,7 @@ private: ...@@ -97,6 +99,7 @@ private:
void operator =(const Self&); //purposely not implemented void operator =(const Self&); //purposely not implemented
std::string m_FieldName; std::string m_FieldName;
bool m_Use8Connected;
}; };
......
...@@ -36,7 +36,7 @@ namespace otb ...@@ -36,7 +36,7 @@ namespace otb
template <class TInputImage, class TPrecision> template <class TInputImage, class TPrecision>
LabelImageToVectorDataFilter<TInputImage, TPrecision> LabelImageToVectorDataFilter<TInputImage, TPrecision>
::LabelImageToVectorDataFilter() : m_FieldName("DN") ::LabelImageToVectorDataFilter() : m_FieldName("DN"), m_Use8Connected(false)
{ {
this->SetNumberOfRequiredInputs(1); this->SetNumberOfRequiredInputs(1);
this->SetNumberOfRequiredOutputs(1); this->SetNumberOfRequiredOutputs(1);
...@@ -159,13 +159,23 @@ LabelImageToVectorDataFilter<TInputImage, TPrecision> ...@@ -159,13 +159,23 @@ LabelImageToVectorDataFilter<TInputImage, TPrecision>
OGRSFDriver * ogrDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(driverName); OGRSFDriver * ogrDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(driverName);
OGRDataSource * dataSource = ogrDriver->CreateDataSource("Shape",NULL); OGRDataSource * dataSource = ogrDriver->CreateDataSource("Shape",NULL);
OGRLayer * outputLayer = dataSource->CreateLayer("toto",NULL,wkbMultiPolygon,NULL); OGRLayer * outputLayer = dataSource->CreateLayer("layer",NULL,wkbMultiPolygon,NULL);
OGRFieldDefn field(m_FieldName.c_str(),OFTInteger); OGRFieldDefn field(m_FieldName.c_str(),OFTInteger);
outputLayer->CreateField(&field, true); outputLayer->CreateField(&field, true);
//Call GDALPolygonize() //Call GDALPolygonize()
GDALPolygonize(dataset->GetRasterBand(1), NULL, outputLayer, 0, NULL, NULL, NULL); char ** options;
options = NULL;
char * option[1];
if (m_Use8Connected == true)
{
std::string opt("8CONNECTED:8");
option[0] = const_cast<char *>(opt.c_str());
options=option;
}
GDALPolygonize(dataset->GetRasterBand(1), NULL, outputLayer, 0, options, NULL, NULL);
/** Convert OGR layer into VectorData */ /** Convert OGR layer into VectorData */
OGRFeatureDefn * dfn = outputLayer->GetLayerDefn(); OGRFeatureDefn * dfn = outputLayer->GetLayerDefn();
......
...@@ -135,15 +135,15 @@ public: ...@@ -135,15 +135,15 @@ public:
itkGetObjectMacro(SegmentationFilter, SegmentationFilterType); itkGetObjectMacro(SegmentationFilter, SegmentationFilterType);
/*itkSetMacro(FieldName, std::string);
itkGetMacro(FieldName, std::string); */
void SetStartLabel(const LabelPixelType & label) void SetStartLabel(const LabelPixelType & label)
{ {
m_StartLabel = label; m_StartLabel = label;
m_TileMaxLabel = label; m_TileMaxLabel = label;
} }
itkGetMacro(StartLabel, LabelPixelType); itkGetMacro(StartLabel, LabelPixelType);
itkSetMacro(Use8Connected, bool);
itkGetMacro(Use8Connected, bool);
protected: protected:
PersistentStreamingLabelImageToOGRDataFilter(); PersistentStreamingLabelImageToOGRDataFilter();
...@@ -164,6 +164,7 @@ private: ...@@ -164,6 +164,7 @@ private:
typename SegmentationFilterType::Pointer m_SegmentationFilter; typename SegmentationFilterType::Pointer m_SegmentationFilter;
unsigned int m_TileNumber; unsigned int m_TileNumber;
bool m_Use8Connected;
}; };
...@@ -235,6 +236,16 @@ public: ...@@ -235,6 +236,16 @@ public:
this->GetFilter()->Initialize(); this->GetFilter()->Initialize();
} }
void SetUse8Connected(bool flag)
{
this->GetFilter()->SetUse8Connected(flag);
}
const bool GetUse8Connected()
{
return this->GetFilter()->GetUse8Connected();
}
protected: protected:
/** Constructor */ /** Constructor */
StreamingVectorizedSegmentationOGR() {} StreamingVectorizedSegmentationOGR() {}
......
...@@ -33,7 +33,7 @@ namespace otb ...@@ -33,7 +33,7 @@ namespace otb
template <class TImageType, class TSegmentationFilter> template <class TImageType, class TSegmentationFilter>
PersistentStreamingLabelImageToOGRDataFilter<TImageType, TSegmentationFilter> PersistentStreamingLabelImageToOGRDataFilter<TImageType, TSegmentationFilter>
::PersistentStreamingLabelImageToOGRDataFilter() : m_TileMaxLabel(0), m_StartLabel(0) ::PersistentStreamingLabelImageToOGRDataFilter() : m_TileMaxLabel(0), m_StartLabel(0), m_Use8Connected(false)
{ {
m_SegmentationFilter = SegmentationFilterType::New(); m_SegmentationFilter = SegmentationFilterType::New();
m_TileNumber = 1; m_TileNumber = 1;
...@@ -85,6 +85,8 @@ PersistentStreamingLabelImageToOGRDataFilter<TImageType, TSegmentationFilter> ...@@ -85,6 +85,8 @@ PersistentStreamingLabelImageToOGRDataFilter<TImageType, TSegmentationFilter>
extract->SetExtractionRegion( this->GetInput()->GetBufferedRegion() ); extract->SetExtractionRegion( this->GetInput()->GetBufferedRegion() );
extract->Update(); extract->Update();
//std::cout<< "extract region " << extract->GetOutput()->GetLargestPossibleRegion()<<std::endl;
chrono.Stop(); chrono.Stop();
//std::cout<< "extract took " << chrono.GetTotal() << " sec"<<std::endl; //std::cout<< "extract took " << chrono.GetTotal() << " sec"<<std::endl;
...@@ -106,11 +108,13 @@ PersistentStreamingLabelImageToOGRDataFilter<TImageType, TSegmentationFilter> ...@@ -106,11 +108,13 @@ PersistentStreamingLabelImageToOGRDataFilter<TImageType, TSegmentationFilter>
chrono1.Stop(); chrono1.Stop();
std::cout<< "segmentation took " << chrono1.GetTotal() << " sec"<<std::endl; std::cout<< "segmentation took " << chrono1.GetTotal() << " sec"<<std::endl;
itk::TimeProbe chrono2; itk::TimeProbe chrono2;
chrono2.Start(); chrono2.Start();
labelImageToOGRDataFilter->SetInput(dynamic_cast<LabelImageType *>(m_SegmentationFilter->GetOutputs().at(labelImageIndex).GetPointer())); labelImageToOGRDataFilter->SetInput(dynamic_cast<LabelImageType *>(m_SegmentationFilter->GetOutputs().at(labelImageIndex).GetPointer()));
labelImageToOGRDataFilter->SetFieldName(this->GetFieldName()); labelImageToOGRDataFilter->SetFieldName(this->GetFieldName());
labelImageToOGRDataFilter->SetUse8Connected(m_Use8Connected);
labelImageToOGRDataFilter->Update(); labelImageToOGRDataFilter->Update();
chrono2.Stop(); chrono2.Stop();
......
...@@ -50,6 +50,7 @@ int otbLabelImageToVectorDataFilter(int argc, char * argv[]) ...@@ -50,6 +50,7 @@ int otbLabelImageToVectorDataFilter(int argc, char * argv[])
reader->SetFileName(infname); reader->SetFileName(infname);
filter->SetInput(reader->GetOutput()); filter->SetInput(reader->GetOutput());
filter->SetUse8Connected(false);
writer->SetFileName(outfname); writer->SetFileName(outfname);
writer->SetInput(filter->GetOutput()); writer->SetInput(filter->GetOutput());
......
...@@ -126,6 +126,7 @@ int otbStreamingVectorizedSegmentationOGR(int argc, char * argv[]) ...@@ -126,6 +126,7 @@ int otbStreamingVectorizedSegmentationOGR(int argc, char * argv[])
filter->GetStreamer()->SetTileDimensionTiledStreaming(atoi(argv[3])); filter->GetStreamer()->SetTileDimensionTiledStreaming(atoi(argv[3]));
filter->SetFieldName(fieldName); filter->SetFieldName(fieldName);
filter->SetStartLabel(1); filter->SetStartLabel(1);
filter->SetUse8Connected(true);
filter->GetSegmentationFilter()->SetSpatialRadius(5); filter->GetSegmentationFilter()->SetSpatialRadius(5);
filter->GetSegmentationFilter()->SetRangeRadius(15); filter->GetSegmentationFilter()->SetRangeRadius(15);
filter->GetSegmentationFilter()->SetMinimumRegionSize(100); filter->GetSegmentationFilter()->SetMinimumRegionSize(100);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment