diff --git a/Code/OBIA/otbLabelImageToOGRDataSourceFilter.h b/Code/OBIA/otbLabelImageToOGRDataSourceFilter.h index 36887dbced61849388d30a96c24af2bbef3ce5ad..7f8f3d955998fde14c9f0341d6b1e19a6ff167f0 100644 --- a/Code/OBIA/otbLabelImageToOGRDataSourceFilter.h +++ b/Code/OBIA/otbLabelImageToOGRDataSourceFilter.h @@ -75,6 +75,9 @@ public: itkSetMacro(FieldName, std::string); itkGetMacro(FieldName, std::string); + itkSetMacro(Use8Connected, bool); + itkGetMacro(Use8Connected, bool); + const OGRDataSourceObjectType * GetOutput(); protected: @@ -96,6 +99,7 @@ private: void operator =(const Self&); //purposely not implemented std::string m_FieldName; + bool m_Use8Connected; }; diff --git a/Code/OBIA/otbLabelImageToOGRDataSourceFilter.txx b/Code/OBIA/otbLabelImageToOGRDataSourceFilter.txx index 97f45930042bb45310d42d31e93ae73527a334f4..63fa535d61d9560e93bff94417da6b62b3fdfbd9 100644 --- a/Code/OBIA/otbLabelImageToOGRDataSourceFilter.txx +++ b/Code/OBIA/otbLabelImageToOGRDataSourceFilter.txx @@ -80,7 +80,7 @@ private: template <class TInputImage> LabelImageToOGRDataSourceFilter<TInputImage> -::LabelImageToOGRDataSourceFilter() : m_FieldName("DN") +::LabelImageToOGRDataSourceFilter() : m_FieldName("DN"), m_Use8Connected(false) { this->SetNumberOfRequiredInputs(1); this->SetNumberOfRequiredOutputs(1); @@ -229,7 +229,17 @@ LabelImageToOGRDataSourceFilter<TInputImage> outputLayer->CreateField(&field, true); //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 = diff --git a/Code/OBIA/otbLabelImageToVectorDataFilter.h b/Code/OBIA/otbLabelImageToVectorDataFilter.h index 50d7fd1740b622c1bd5c3ff71a35bde6c9ffa0f4..ddb36c00ba3106f4b496e2a4ccf1878f5fa57249 100644 --- a/Code/OBIA/otbLabelImageToVectorDataFilter.h +++ b/Code/OBIA/otbLabelImageToVectorDataFilter.h @@ -82,6 +82,8 @@ public: itkSetMacro(FieldName, std::string); itkGetMacro(FieldName, std::string); + itkSetMacro(Use8Connected, bool); + itkGetMacro(Use8Connected, bool); protected: LabelImageToVectorDataFilter(); @@ -97,6 +99,7 @@ private: void operator =(const Self&); //purposely not implemented std::string m_FieldName; + bool m_Use8Connected; }; diff --git a/Code/OBIA/otbLabelImageToVectorDataFilter.txx b/Code/OBIA/otbLabelImageToVectorDataFilter.txx index 8a380f2c0511f80734bd34c2b0a9ab01de1776b4..5f5e30bf3ea7a05aa5e660e7a5bb81fcc4f58f24 100644 --- a/Code/OBIA/otbLabelImageToVectorDataFilter.txx +++ b/Code/OBIA/otbLabelImageToVectorDataFilter.txx @@ -36,7 +36,7 @@ namespace otb template <class TInputImage, class TPrecision> LabelImageToVectorDataFilter<TInputImage, TPrecision> -::LabelImageToVectorDataFilter() : m_FieldName("DN") +::LabelImageToVectorDataFilter() : m_FieldName("DN"), m_Use8Connected(false) { this->SetNumberOfRequiredInputs(1); this->SetNumberOfRequiredOutputs(1); @@ -159,13 +159,23 @@ LabelImageToVectorDataFilter<TInputImage, TPrecision> OGRSFDriver * ogrDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(driverName); 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); outputLayer->CreateField(&field, true); //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 */ OGRFeatureDefn * dfn = outputLayer->GetLayerDefn(); diff --git a/Code/OBIA/otbStreamingVectorizedSegmentationOGR.h b/Code/OBIA/otbStreamingVectorizedSegmentationOGR.h index 3887ed624c11bc1a86a4c9c57fcf71a3e88d289d..69da1b6f93dc96a6a31f46779cf822baa0d2c6c7 100644 --- a/Code/OBIA/otbStreamingVectorizedSegmentationOGR.h +++ b/Code/OBIA/otbStreamingVectorizedSegmentationOGR.h @@ -135,15 +135,15 @@ public: 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); + + itkSetMacro(Use8Connected, bool); + itkGetMacro(Use8Connected, bool); protected: PersistentStreamingLabelImageToOGRDataFilter(); @@ -164,6 +164,7 @@ private: typename SegmentationFilterType::Pointer m_SegmentationFilter; unsigned int m_TileNumber; + bool m_Use8Connected; }; @@ -235,6 +236,16 @@ public: this->GetFilter()->Initialize(); } + void SetUse8Connected(bool flag) + { + this->GetFilter()->SetUse8Connected(flag); + } + + const bool GetUse8Connected() + { + return this->GetFilter()->GetUse8Connected(); + } + protected: /** Constructor */ StreamingVectorizedSegmentationOGR() {} diff --git a/Code/OBIA/otbStreamingVectorizedSegmentationOGR.txx b/Code/OBIA/otbStreamingVectorizedSegmentationOGR.txx index fae0a52ba4192a6c14bdfc995df600f61e241d60..100e18762ccc6c35302ecaf6272e4fa547e17f8d 100644 --- a/Code/OBIA/otbStreamingVectorizedSegmentationOGR.txx +++ b/Code/OBIA/otbStreamingVectorizedSegmentationOGR.txx @@ -33,7 +33,7 @@ namespace otb template <class TImageType, class 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_TileNumber = 1; @@ -85,6 +85,8 @@ PersistentStreamingLabelImageToOGRDataFilter<TImageType, TSegmentationFilter> extract->SetExtractionRegion( this->GetInput()->GetBufferedRegion() ); extract->Update(); + //std::cout<< "extract region " << extract->GetOutput()->GetLargestPossibleRegion()<<std::endl; + chrono.Stop(); //std::cout<< "extract took " << chrono.GetTotal() << " sec"<<std::endl; @@ -106,11 +108,13 @@ PersistentStreamingLabelImageToOGRDataFilter<TImageType, TSegmentationFilter> chrono1.Stop(); std::cout<< "segmentation took " << chrono1.GetTotal() << " sec"<<std::endl; + itk::TimeProbe chrono2; chrono2.Start(); labelImageToOGRDataFilter->SetInput(dynamic_cast<LabelImageType *>(m_SegmentationFilter->GetOutputs().at(labelImageIndex).GetPointer())); labelImageToOGRDataFilter->SetFieldName(this->GetFieldName()); + labelImageToOGRDataFilter->SetUse8Connected(m_Use8Connected); labelImageToOGRDataFilter->Update(); chrono2.Stop(); diff --git a/Testing/Code/OBIA/otbLabelImageToVectorDataFilter.cxx b/Testing/Code/OBIA/otbLabelImageToVectorDataFilter.cxx index b522a7f0d3c07e26c084b8d61ae6813e575203d9..7478798357cc3bfd111731f24458df3189afbdee 100644 --- a/Testing/Code/OBIA/otbLabelImageToVectorDataFilter.cxx +++ b/Testing/Code/OBIA/otbLabelImageToVectorDataFilter.cxx @@ -50,6 +50,7 @@ int otbLabelImageToVectorDataFilter(int argc, char * argv[]) reader->SetFileName(infname); filter->SetInput(reader->GetOutput()); + filter->SetUse8Connected(false); writer->SetFileName(outfname); writer->SetInput(filter->GetOutput()); diff --git a/Testing/Code/OBIA/otbStreamingVectorizedSegmentationOGR.cxx b/Testing/Code/OBIA/otbStreamingVectorizedSegmentationOGR.cxx index 2b0892c0bc48d5d23802b7f580192adf59273193..c3d1ce7fc3cf0ec4be282a987accbe885ab2f72a 100644 --- a/Testing/Code/OBIA/otbStreamingVectorizedSegmentationOGR.cxx +++ b/Testing/Code/OBIA/otbStreamingVectorizedSegmentationOGR.cxx @@ -126,6 +126,7 @@ int otbStreamingVectorizedSegmentationOGR(int argc, char * argv[]) filter->GetStreamer()->SetTileDimensionTiledStreaming(atoi(argv[3])); filter->SetFieldName(fieldName); filter->SetStartLabel(1); + filter->SetUse8Connected(true); filter->GetSegmentationFilter()->SetSpatialRadius(5); filter->GetSegmentationFilter()->SetRangeRadius(15); filter->GetSegmentationFilter()->SetMinimumRegionSize(100);